Advertisement
nyker

Untitled

Apr 8th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. const int N = 6;
  6. static void PrintMatrix(int[,] matrix)
  7. {
  8. for (int row = 0; row < N; row++)
  9. {
  10. for (int col = 0; col < N; col++)
  11. {
  12. if (matrix[row,col] < 10)
  13. {
  14. Console.Write(matrix[row,col] + " ");
  15. }
  16. else
  17. {
  18. Console.Write(matrix[row, col] + " ");
  19. }
  20. }
  21. Console.WriteLine();
  22. }
  23. }
  24.  
  25. static void Main(string[] args)
  26. {
  27. int[,] SpiralMatrix = new int[N, N];
  28. int rowStart = 0, rowend = N,
  29. colStart = 0, colEnd = N;
  30. int row = row = rowStart, col = colStart;
  31. string direction = "right";
  32. for (int i = 1; i <= N * N; i++)
  33. {
  34. if (direction == "down")
  35. {
  36. SpiralMatrix[row, col] = i;
  37. row++;
  38. if (row == rowend)
  39. {
  40. row--;
  41. direction = "left";
  42. colEnd--;
  43. col = colEnd - 1;
  44. }
  45. }
  46. else if (direction == "up")
  47. {
  48. SpiralMatrix[row, col] = i;
  49. row--;
  50. if (row < rowStart)
  51. {
  52. row++;
  53. direction = "right";
  54. colStart++;
  55. col = colStart;
  56. }
  57. }
  58. else if (direction == "right")
  59. {
  60. SpiralMatrix[row, col] = i;
  61. col++;
  62. if (col == colEnd)
  63. {
  64. col--;
  65. direction = "down";
  66. rowStart++;
  67. row = rowStart;
  68. }
  69. }
  70. else if (direction == "left")
  71. {
  72. SpiralMatrix[row, col] = i;
  73. col--;
  74. if (col < colStart)
  75. {
  76. col++;
  77. direction = "up";
  78. rowend--;
  79. row = rowend - 1;
  80. }
  81. }
  82. }
  83. PrintMatrix(SpiralMatrix);
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement