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 SpiralMatrix
- {
- class MatrixSpiral
- {
- static void Main(string[] args)
- {
- Console.Write("Enter number: ");
- 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";
- col++;
- row++;
- }
- matrix[row, col] = i;
- if (direction == "right")
- {
- col++;
- }
- if (direction == "down")
- {
- row++;
- }
- if (direction == "left")
- {
- col--;
- }
- if (direction == "up")
- {
- row--;
- }
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- Console.Write("{0,4}", matrix[i, j]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment