Advertisement
GPipkov

Spiral Matrix

Aug 29th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SpiralMatrix
  8. {
  9.     class SpiralMatrix
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.            
  15.             int[,] matrix = new int[n,n];
  16.             string direction = "right";
  17.             int currentRow = 0;
  18.             int currentCol = 0;
  19.            
  20.             for (int i = 1; i <= n * n; i++)
  21.             {
  22.                
  23.                
  24.                 if (direction == "right" && (currentCol >= n || matrix[currentRow, currentCol] != 0))
  25.                 {
  26.                     currentRow++;
  27.                     currentCol--;
  28.                     direction = "down";
  29.                 }
  30.                 else if (direction == "down" && (currentRow >= n || matrix[currentRow, currentCol] != 0))
  31.                 {
  32.                     currentRow--;
  33.                     currentCol--;
  34.                     direction = "left";
  35.                 }
  36.                 else if (direction == "left" && (currentCol < 0 || matrix[currentRow, currentCol] != 0))
  37.                 {
  38.                     currentRow--;
  39.                     currentCol++;
  40.                     direction = "up";
  41.                 }
  42.                 else if (direction == "up" && (currentRow < 0 || matrix[currentRow, currentCol] != 0))
  43.                 {
  44.                     currentRow++;
  45.                     currentCol++;
  46.                     direction = "right";
  47.                 }
  48.                 matrix[currentRow, currentCol] = i;
  49.                
  50.                
  51.                 if (direction == "right")
  52.                 {
  53.                     currentCol++;
  54.                 }
  55.                 else if (direction == "down")
  56.                 {
  57.                     currentRow++;
  58.                 }
  59.                 else if (direction == "left")
  60.                 {
  61.                     currentCol--;
  62.                 }
  63.                 else if (direction == "up")
  64.                 {
  65.                     currentRow--;
  66.                 }
  67.             }
  68.             for (int i = 0; i < n; i++)
  69.             {
  70.                 for (int j = 0; j < n; j++)
  71.                 {
  72.                     Console.Write(matrix[i,j] + "     ");
  73.                 }
  74.                 Console.WriteLine();
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement