Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- public class Program
- {
- public static void Main()
- {
- int size = int.Parse(Console.ReadLine());
- char[,] matrix = new char[size, size];
- matrix = PopulateMatrix(matrix, size);
- int [] coordsToRemove;
- int numsRemoved = 0;
- while (true)
- {
- /*
- Issue with code:
- As there may be multiple blocks with the same amount of relations, my program currently only takes the first one with the most dependencies into account.
- Let's say for example, this is the input:
- 3
- KKK
- KKK
- KKK
- As the first K has 2 relations and this is the most there can be in the current matrix, this is chosen to be removed for the next cycle
- 0KK
- KKK
- KKK
- For the next cycle, the same issue repeats, [0,1] has two pairs which is the maximum
- 00K
- KKK
- KKK
- Again, for the next cycle, the same issue repeats, [0,2] has two pairs which is the maximum
- 000
- KKK
- KKK
- The remaining relations are at max, 1
- As such, the first instance of this occurs at [1,0]
- 000
- 0KK
- KKK
- At this point, only one knight can move.
- Removing the last knight.
- 000
- 0K0
- KKK
- And this is how we end up with 5 instead of 4.
- The program does not account for the possibility of anything but relations mattering.
- As such, in case that the amount of relations is the same, it might be best to create a loop for both circumstances, and then compare which one takes less moves to perform to the end.
- This is very resource intensive however.
- How to solve the issue with 4 moves?
- 0K0
- KKK
- 0K0
- As such, this answer should not be correct
- */
- coordsToRemove = FindMostMoves(matrix, numsRemoved);
- matrix[coordsToRemove[0], coordsToRemove[1]] = '0';
- //Restart loop with the changes made
- numsRemoved++;
- //Console.WriteLine("Debug: Iteration - " + numsRemoved);
- PrintMatrix(matrix);
- }
- }
- static char[,] PopulateMatrix(char[,] matrix, int size)
- {
- char[] userInput;
- for (int row = 0; row < size; row++)
- {
- userInput = Console.ReadLine().ToCharArray();
- for (int col = 0; col < size; col++)
- {
- matrix[row, col] = userInput[col];
- }
- }
- return matrix;
- }
- static void PrintMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- static int[] FindMostMoves(char[,] matrix, int numsRemoved)
- {
- int[] coordinatesWithMostMoves = new int[2];
- int mostMoves = 0, currMoves;
- for (int row=0; row<matrix.GetLength(0); row++)
- {
- for(int col=0; col<matrix.GetLength(1); col++)
- {
- //Only launch functions if the current coordinate refers to a knight
- if (matrix[row, col] == 'K')
- {
- currMoves = FindNumOfMoves(matrix, row, col);
- if (currMoves > mostMoves)
- {
- mostMoves = currMoves;
- coordinatesWithMostMoves = new int[] { row, col };
- }
- }
- }
- }
- //If no matches at all, quit the program entirely
- if (mostMoves != 0)
- {
- return coordinatesWithMostMoves;
- } else
- {
- Console.WriteLine(numsRemoved);
- Environment.Exit(0);
- return coordinatesWithMostMoves;
- }
- }
- static int FindNumOfMoves(char[,] matrix, int row, int col)
- {
- int counter = 0;
- int indexOfLastCol = matrix.GetLength(1) - 1;
- if (row >= 1)
- {
- //Check moves - 1 Up & 2 Left AND 1 Up & 2 Right
- if (col >= 2)
- {
- if (matrix[row - 1, col - 2] == 'K')
- {
- counter++;
- }
- }
- if (col <= indexOfLastCol-2)
- {
- if (matrix[row - 1, col + 2] == 'K')
- {
- counter++;
- }
- }
- //Check moves - 2 Up & Left AND 2 Up & Right
- if (row >= 2)
- {
- if (col >= 1)
- {
- if (matrix[row - 2, col - 1] == 'K')
- {
- counter++;
- }
- }
- if (col <= indexOfLastCol-1)
- {
- if (matrix[row - 2, col + 1] == 'K')
- {
- counter++;
- }
- }
- }
- }
- //Check moves down
- if (row <= indexOfLastCol-1)
- {
- //Check moves - 1 Down & 2 Left AND 1 Down & 2 Right
- if (col >= 2)
- {
- if (matrix[row + 1, col - 2] == 'K')
- {
- counter++;
- }
- }
- if (col <= indexOfLastCol-2)
- {
- if (matrix[row + 1, col + 2] == 'K')
- {
- counter++;
- }
- }
- //Now to check moves 2 Down
- if (row <= indexOfLastCol - 2) {
- //Check moves - 2 Down & 1 Left AND 2 Down & 1 Right
- if (col >= 1)
- {
- if (matrix[row + 2, col - 1] == 'K')
- {
- counter++;
- }
- }
- if (col <= indexOfLastCol-1)
- {
- if (matrix[row + 2, col + 1] == 'K')
- {
- counter++;
- }
- }
- }
- }
- //Console.WriteLine($"Debug: Counter for [{row},{col}] = {counter}");
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement