Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace fillTheMatrix
- {
- class MainClass
- {
- static void Main(string[] args)
- {
- int size = int.Parse(Console.ReadLine());
- string point = Console.ReadLine();
- var sum = 1;
- int[,] matrix = new int[size, size];
- // first part
- /*
- for (int col = 0; col < size; col++)
- {
- for (int row = 0; row < size; row++)
- {
- matrix[row, col] = sum;
- sum++;
- }
- }
- */
- //second part
- /*
- for (int col = 0; col < size; col++)
- {
- int checker = col % 2;
- if (checker == 0)
- {
- for (int row = 0; row < size; row++)
- {
- matrix[row, col] = sum;
- sum++;
- }
- }
- else
- {
- for (int row = size - 1; row >= 0; row--)
- {
- matrix[row, col] = sum;
- sum++;
- }
- }
- }
- */
- //third part
- /*
- for (int i = size - 1; i >= 0; i--)
- {
- int row = i;
- int col = 0;
- while (true)
- {
- matrix[row, col] = sum;
- sum++;
- if (row == size - 1)
- {
- break;
- }
- else
- {
- row++;
- col++;
- }
- }
- }
- for (int i = 1; i < size; i++)
- {
- int row = i;
- int col = 0;
- while (true)
- {
- matrix[col, row] = sum;
- sum++;
- if (row == size - 1)
- {
- break;
- }
- else
- {
- row++;
- col++;
- }
- }
- }
- */
- string direction = "down";
- int row = 0;
- int col = 0;
- for (int i = 0; i < size * size; i++)
- {
- if (direction == "down" && matrix[row, col] == 0)
- {
- matrix[row, col] = sum;
- if (row == size - 1)
- {
- direction = "right";
- }
- }
- if (direction == "right")
- {
- matrix[row, col] = sum;
- if (col == size-1)
- {
- direction = "up";
- }
- }
- if (direction =="up")
- {
- matrix[row, col] = sum;
- if (row <= 0)
- {
- direction = "left";
- }
- }
- if (direction =="left" && matrix[row,col] == 0)
- {
- matrix[row, col] = sum;
- if (col <= 0)
- {
- direction = "down";
- }
- }
- sum++;
- switch (direction)
- {
- case "down":
- row++;
- break;
- case "right":
- col++;
- break;
- case "up":
- row--;
- break;
- case "left":
- col--;
- break;
- default:
- break;
- }
- Print(matrix);
- }
- }
- static void Print(int[,] matrixToPrint)
- {
- for (int row = 0; row < matrixToPrint.GetLength(0); row++)
- {
- for (int col = 0; col < matrixToPrint.GetLength(1); col++)
- {
- Console.Write(matrixToPrint[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement