Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace P01.Fill_the_matrix
- {
- class Program
- {
- static void Main(string[] args)
- {
- int number = int.Parse(Console.ReadLine());
- string letter = Console.ReadLine();
- int[,] matrix = new int[number, number];
- if (letter=="a")
- {
- int digit = 1;
- for (int r = 0; r < matrix.GetLength(0); r++)
- {
- matrix[r, 0] = digit;
- digit++;
- }
- for (int r = 0; r < matrix.GetLength(0); r++)
- {
- for (int c = 1; c < matrix.GetLength(1); c++)
- {
- matrix[r, c] = matrix[r, c - 1] + 4;
- }
- }
- }
- else if (letter=="b")
- {
- int digit = 1;
- for (int c = 0; c < matrix.GetLength(1); c++)
- {
- if (c%2==0)
- {
- for (int r = 0; r < matrix.GetLength(0); r++)
- {
- matrix[r, c] = digit;
- digit++;
- }
- }
- else
- {
- for (int r = matrix.GetLength(0)-1; r >=0; r--)
- {
- matrix[r, c] = digit;
- digit++;
- }
- }
- }
- }
- else if (letter=="c")
- {
- int row=number-1;
- int col=0;
- int index = 1;
- int startRowDiagonal=col;
- int startColDiagonal=row;
- while (index<=matrix.Length)
- {
- matrix[row, col] = index;
- if (col==0)
- {
- startRowDiagonal = row;
- startColDiagonal = 0;
- }
- else if (row==0)
- {
- startColDiagonal = col;
- startRowDiagonal = 0;
- }
- row++;
- col++;
- index++;
- if (col > number - 1)
- {
- row = 0;
- col = startColDiagonal + 1;
- }
- else if (row>number-1)
- {
- row = startRowDiagonal-1;
- col = 0;
- }
- }
- }
- else if (letter == "d")
- {
- //string[] directions = { "down", "right", "up", "left" };
- int row = 0;
- int col = 0;
- int index = 1;
- int nextRow = row;
- int nextCol = col;
- string direction = "down";
- string nextDirection = String.Empty;
- while (index<=matrix.Length)
- {
- row = nextRow;
- col = nextCol;
- matrix[row, col] = index;
- index++;
- if (direction=="down")
- {
- nextRow++;
- nextDirection = "right";
- }
- else if (direction=="right")
- {
- nextCol++;
- nextDirection = "up";
- }
- else if (direction=="up")
- {
- nextRow--;
- nextDirection = "left";
- }
- else if (direction=="left")
- {
- nextCol--;
- nextDirection = "down";
- }
- if (nextRow>number-1 || nextRow<0 || nextCol>number-1 || nextCol< 0 || matrix[nextRow, nextCol] != 0)
- {
- direction = nextDirection;
- if (direction == "down")
- {
- nextRow++;
- nextCol++;
- nextDirection = "right";
- }
- else if (direction == "right")
- {
- nextCol++;
- nextRow--;
- nextDirection = "up";
- }
- else if (direction == "up")
- {
- nextRow--;
- nextCol--;
- nextDirection = "left";
- }
- else if (direction == "left")
- {
- nextCol--;
- nextRow++;
- nextDirection = "down";
- }
- }
- }
- }
- for (int r = 0; r < matrix.GetLength(0); r++)
- {
- for (int c = 0; c < matrix.GetLength(1); c++)
- {
- Console.Write($"{matrix[r, c]} ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement