Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _19.SpiralMatrix
- {
- class SpiralMatrix
- {
- static void Main()
- {
- Console.Write("Enter a number for n: ");
- int n = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, n];
- int row = 0;
- int col = 0;
- string direction = "right";
- int maxRotations = n * n;
- for (int i = 1; i <= maxRotations; i++)
- {
- if ((direction == "right") && (col > n - 1 || matrix[row, col] != 0))
- {
- direction = "down";
- col--;
- row++;
- }
- if ((direction == "down") && (row > n - 1 || matrix[row, col] != 0))
- {
- direction = "left";
- row--;
- col--;
- }
- if ((direction == "left") && (col < 0 || matrix[row, col] != 0))
- {
- direction = "up";
- col++;
- row--;
- }
- if ((direction == "up") && (row < 0 || matrix[row, col] != 0) )
- {
- direction = "right";
- row++;
- col++;
- }
- matrix[row, col] = i;
- if (direction == "right")
- {
- col++;
- }
- if (direction == "down")
- {
- row++;
- }
- if (direction == "left")
- {
- col--;
- }
- if (direction == "up")
- {
- row--;
- }
- }
- for (int r = 0; r < n; r++)
- {
- for (int c = 0; c < n; c++)
- {
- Console.Write("{0, 4}", matrix[r, c]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement