Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- const int N = 6;
- static void PrintMatrix(int[,] matrix)
- {
- for (int row = 0; row < N; row++)
- {
- for (int col = 0; col < N; col++)
- {
- if (matrix[row,col] < 10)
- {
- Console.Write(matrix[row,col] + " ");
- }
- else
- {
- Console.Write(matrix[row, col] + " ");
- }
- }
- Console.WriteLine();
- }
- }
- static void Main(string[] args)
- {
- int[,] SpiralMatrix = new int[N, N];
- int rowStart = 0, rowend = N,
- colStart = 0, colEnd = N;
- int row = row = rowStart, col = colStart;
- string direction = "right";
- for (int i = 1; i <= N * N; i++)
- {
- if (direction == "down")
- {
- SpiralMatrix[row, col] = i;
- row++;
- if (row == rowend)
- {
- row--;
- direction = "left";
- colEnd--;
- col = colEnd - 1;
- }
- }
- else if (direction == "up")
- {
- SpiralMatrix[row, col] = i;
- row--;
- if (row < rowStart)
- {
- row++;
- direction = "right";
- colStart++;
- col = colStart;
- }
- }
- else if (direction == "right")
- {
- SpiralMatrix[row, col] = i;
- col++;
- if (col == colEnd)
- {
- col--;
- direction = "down";
- rowStart++;
- row = rowStart;
- }
- }
- else if (direction == "left")
- {
- SpiralMatrix[row, col] = i;
- col--;
- if (col < colStart)
- {
- col++;
- direction = "up";
- rowend--;
- row = rowend - 1;
- }
- }
- }
- PrintMatrix(SpiralMatrix);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement