Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char type = char.Parse(Console.ReadLine());
- var arr = new int[n, n];
- switch (type)
- {
- case 'a': printA(arr); break;
- case 'b': printB(arr); break;
- case 'c': printC(arr); break;
- case 'd': printD(arr); break;
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++) Console.Write(arr[i, j]+(j+1<n?" ":""));
- Console.WriteLine();
- }
- }
- static void printA(int[,] arr)
- {
- int counter = 1;
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++) arr[j, i] = counter++;
- }
- }
- static void printB(int[,] arr)
- {
- int counter = 1;
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- if (i % 2 == 0) arr[j, i] = counter++;
- else { arr[arr.GetLength(1) -1 - j, i] = counter++; }
- }
- }
- }
- static void printC(int[,] arr)
- {
- int number = arr.GetLength(0), counter = 1;
- for (int i = number-1, x = 1; i >= 0; i--, x++)
- {
- for (int j = 0; j < x; j++)
- {
- arr[i+j, j] = counter++;
- }
- }
- for (int i = 1; i < number; i++)
- {
- for (int j = 0; j+i < number; j++)
- {
- arr[j, i+j] = counter++;
- }
- }
- }
- static void printD(int[,] matrix)
- {
- int x = 0, y = 0, direction = 2, counter = 1;
- int number = matrix.GetLength(0);
- while (matrix[y, x] == 0)
- {
- matrix[y, x] = counter++;
- switch (direction)
- {
- case 2:
- if (y + 1 < number && matrix[y+1, x] == 0) y++;
- else { direction = 1; x++; };
- break;
- case 1:
- if (x + 1 < number && matrix[y, x+1] == 0) x++;
- else { direction = 3; y--; };
- break;
- case 4:
- if (x - 1 >= 0 && matrix[y, x - 1] == 0) x--;
- else { direction = 2; y++; };
- break;
- case 3:
- if (y - 1 >= 0 && matrix[y - 1, x] == 0) y--;
- else { direction = 4; x--; };
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement