SHOW:
|
|
- or go back to the newest paste.
| 1 | - | using System; |
| 1 | + | using System; |
| 2 | - | using System.Collections.Generic; |
| 2 | + | using System.Collections.Generic; |
| 3 | - | using System.Linq; |
| 3 | + | using System.Linq; |
| 4 | - | using System.Text; |
| 4 | + | using System.Text; |
| 5 | - | |
| 5 | + | |
| 6 | - | class Program |
| 6 | + | class Program |
| 7 | - | { |
| 7 | + | {
|
| 8 | - | static void Main() |
| 8 | + | static void Main() |
| 9 | - | { |
| 9 | + | {
|
| 10 | - | int[] dimensions = StringToIntArray(Console.ReadLine()); |
| 10 | + | int[] dimensions = StringToIntArray(Console.ReadLine()); |
| 11 | - | int rows = dimensions[0]; |
| 11 | + | int rows = dimensions[0]; |
| 12 | - | int cols = dimensions[1]; |
| 12 | + | int cols = dimensions[1]; |
| 13 | - | List<List<Cell>> matrix = InitializeMatrix(rows, cols); |
| 13 | + | List<List<Cell>> matrix = InitializeMatrix(rows, cols); |
| 14 | - | string input = Console.ReadLine(); |
| 14 | + | string input = Console.ReadLine(); |
| 15 | - | while (!input.Equals("Nuke it from orbit")) |
| 15 | + | while (!input.Equals("Nuke it from orbit"))
|
| 16 | - | { |
| 16 | + | {
|
| 17 | - | int[] destroyData = StringToIntArray(input); |
| 17 | + | int[] destroyData = StringToIntArray(input); |
| 18 | - | int[] coords = new int[] { destroyData[0], destroyData[1] }; |
| 18 | + | int[] coords = new int[] { destroyData[0], destroyData[1] };
|
| 19 | - | int radius = destroyData[2]; |
| 19 | + | int radius = destroyData[2]; |
| 20 | - | |
| 20 | + | |
| 21 | - | for (int i = 0; i < matrix.Count; i++) |
| 21 | + | for (int i = 0; i < matrix.Count; i++) |
| 22 | - | { |
| 22 | + | {
|
| 23 | - | for (int j = 0; j < matrix[i].Count; j++) |
| 23 | + | for (int j = 0; j < matrix[i].Count; j++) |
| 24 | - | { |
| 24 | + | {
|
| 25 | - | var cell = matrix[i][j]; |
| 25 | + | var cell = matrix[i][j]; |
| 26 | - | int distance = 0; |
| 26 | + | int distance = 0; |
| 27 | - | bool eligibleForDestruction = cell.Row == coords[0] || cell.Col == coords[1];//same row or col |
| 27 | + | bool eligibleForDestruction = cell.Row == coords[0] || cell.Col == coords[1];//same row or col |
| 28 | - | if (eligibleForDestruction) |
| 28 | + | if (eligibleForDestruction) |
| 29 | - | { |
| 29 | + | {
|
| 30 | - | if (cell.Row == coords[0]) |
| 30 | + | if (cell.Row == coords[0]) |
| 31 | - | { |
| 31 | + | {
|
| 32 | - | distance = Math.Abs(cell.Col - coords[1]); |
| 32 | + | distance = Math.Abs(cell.Col - coords[1]); |
| 33 | - | } |
| 33 | + | } |
| 34 | - | else // j == coords[1] |
| 34 | + | else // j == coords[1] |
| 35 | - | { |
| 35 | + | {
|
| 36 | - | distance = Math.Abs(cell.Row - coords[0]); |
| 36 | + | distance = Math.Abs(cell.Row - coords[0]); |
| 37 | - | } |
| 37 | + | } |
| 38 | - | |
| 38 | + | |
| 39 | - | if (distance <= radius) |
| 39 | + | if (distance <= radius) |
| 40 | - | { |
| 40 | + | {
|
| 41 | - | matrix[i].Remove(cell); |
| 41 | + | matrix[i].Remove(cell); |
| 42 | - | if (cell.Row == coords[0]) // next cell remains at same index |
| 42 | + | if (cell.Row == coords[0]) // next cell remains at same index |
| 43 | - | { |
| 43 | + | {
|
| 44 | - | j--; |
| 44 | + | j--; |
| 45 | - | } |
| 45 | + | } |
| 46 | - | } |
| 46 | + | } |
| 47 | - | } |
| 47 | + | } |
| 48 | - | } |
| 48 | + | } |
| 49 | - | } |
| 49 | + | } |
| 50 | - | |
| 50 | + | |
| 51 | - | ClearUp(matrix); |
| 51 | + | ClearUp(matrix); |
| 52 | - | input = Console.ReadLine(); |
| 52 | + | input = Console.ReadLine(); |
| 53 | - | } |
| 53 | + | } |
| 54 | - | |
| 54 | + | |
| 55 | - | PrintMatrix(matrix); |
| 55 | + | PrintMatrix(matrix); |
| 56 | - | } |
| 56 | + | } |
| 57 | - | |
| 57 | + | |
| 58 | - | private static int[] StringToIntArray(string text) |
| 58 | + | private static int[] StringToIntArray(string text) |
| 59 | - | { |
| 59 | + | {
|
| 60 | - | return text |
| 60 | + | return text |
| 61 | - | .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) |
| 61 | + | .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
| 62 | - | .Select(int.Parse) |
| 62 | + | .Select(int.Parse) |
| 63 | - | .ToArray(); |
| 63 | + | .ToArray(); |
| 64 | - | } |
| 64 | + | } |
| 65 | - | |
| 65 | + | |
| 66 | - | private static List<List<Cell>> InitializeMatrix(int rows, int cols) |
| 66 | + | private static List<List<Cell>> InitializeMatrix(int rows, int cols) |
| 67 | - | { |
| 67 | + | {
|
| 68 | - | var matrix = new List<List<Cell>>(rows); |
| 68 | + | var matrix = new List<List<Cell>>(rows); |
| 69 | - | for (int i = 0; i < rows; i++) |
| 69 | + | for (int i = 0; i < rows; i++) |
| 70 | - | { |
| 70 | + | {
|
| 71 | - | matrix.Add(new List<Cell>(cols)); |
| 71 | + | matrix.Add(new List<Cell>(cols)); |
| 72 | - | for (int j = 0; j < cols; j++) |
| 72 | + | for (int j = 0; j < cols; j++) |
| 73 | - | { |
| 73 | + | {
|
| 74 | - | matrix[i].Add(new Cell(i, j, i * cols + j + 1)); |
| 74 | + | matrix[i].Add(new Cell(i, j, i * cols + j + 1)); |
| 75 | - | } |
| 75 | + | } |
| 76 | - | } |
| 76 | + | } |
| 77 | - | |
| 77 | + | |
| 78 | - | return matrix; |
| 78 | + | return matrix; |
| 79 | - | } |
| 79 | + | } |
| 80 | - | |
| 80 | + | |
| 81 | - | private static void ClearUp(List<List<Cell>> matrix) // remove empty lines and update cell coordinates |
| 81 | + | private static void ClearUp(List<List<Cell>> matrix) // remove empty lines and update cell coordinates |
| 82 | - | { |
| 82 | + | {
|
| 83 | - | int row = 0; |
| 83 | + | int row = 0; |
| 84 | - | while (row < matrix.Count) |
| 84 | + | while (row < matrix.Count) |
| 85 | - | { |
| 85 | + | {
|
| 86 | - | if (matrix[row].Count == 0) |
| 86 | + | if (matrix[row].Count == 0) |
| 87 | - | { |
| 87 | + | {
|
| 88 | - | matrix.RemoveAt(row); |
| 88 | + | matrix.RemoveAt(row); |
| 89 | - | } |
| 89 | + | } |
| 90 | - | else |
| 90 | + | else |
| 91 | - | { |
| 91 | + | {
|
| 92 | - | int col = 0; |
| 92 | + | int col = 0; |
| 93 | - | while (col < matrix[row].Count) |
| 93 | + | while (col < matrix[row].Count) |
| 94 | - | { |
| 94 | + | {
|
| 95 | - | matrix[row][col].Row = row; |
| 95 | + | matrix[row][col].Row = row; |
| 96 | - | matrix[row][col].Col = col++; |
| 96 | + | matrix[row][col].Col = col++; |
| 97 | - | } |
| 97 | + | } |
| 98 | - | row++; |
| 98 | + | row++; |
| 99 | - | } |
| 99 | + | } |
| 100 | - | } |
| 100 | + | } |
| 101 | - | } |
| 101 | + | } |
| 102 | - | |
| 102 | + | |
| 103 | - | private static void PrintMatrix(List<List<Cell>> matrix) |
| 103 | + | private static void PrintMatrix(List<List<Cell>> matrix) |
| 104 | - | { |
| 104 | + | {
|
| 105 | - | foreach (var row in matrix) |
| 105 | + | foreach (var row in matrix) |
| 106 | - | { |
| 106 | + | {
|
| 107 | - | foreach (var cell in row) |
| 107 | + | foreach (var cell in row) |
| 108 | - | { |
| 108 | + | {
|
| 109 | - | Console.Write($"{cell.Value} "); |
| 109 | + | Console.Write($"{cell.Value} ");
|
| 110 | - | } |
| 110 | + | } |
| 111 | - | Console.WriteLine(); |
| 111 | + | Console.WriteLine(); |
| 112 | - | } |
| 112 | + | } |
| 113 | - | } |
| 113 | + | } |
| 114 | } | |
| 115 | - | |
| 115 | + | |
| 116 | - | class Cell |
| 116 | + | class Cell |
| 117 | - | { |
| 117 | + | {
|
| 118 | - | public int Row { get; set; } |
| 118 | + | public int Row { get; set; }
|
| 119 | - | public int Col { get; set; } |
| 119 | + | public int Col { get; set; }
|
| 120 | - | public int Value { get; set; } |
| 120 | + | public int Value { get; set; }
|
| 121 | - | |
| 121 | + | |
| 122 | - | public Cell(int row, int col, int value) |
| 122 | + | public Cell(int row, int col, int value) |
| 123 | - | { |
| 123 | + | {
|
| 124 | - | this.Row = row; |
| 124 | + | this.Row = row; |
| 125 | - | this.Col = col; |
| 125 | + | this.Col = col; |
| 126 | - | this.Value = value; |
| 126 | + | this.Value = value; |
| 127 | - | } |
| 127 | + | } |
| 128 | } |