Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         int size = int.Parse(Console.ReadLine());
  10.  
  11.         char[,] matrix = new char[size, size];
  12.         matrix = PopulateMatrix(matrix, size);
  13.  
  14.         int [] coordsToRemove;
  15.         int numsRemoved = 0;
  16.         while (true)
  17.         {
  18.             /*
  19.             Issue with code:
  20.  
  21.             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.
  22.             Let's say for example, this is the input:
  23.  
  24.             3
  25.             KKK
  26.             KKK
  27.             KKK
  28.  
  29.             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
  30.  
  31.             0KK
  32.             KKK
  33.             KKK
  34.  
  35.             For the next cycle, the same issue repeats, [0,1] has two pairs which is the maximum
  36.  
  37.             00K
  38.             KKK
  39.             KKK
  40.  
  41.             Again, for the next cycle, the same issue repeats, [0,2] has two pairs which is the maximum
  42.  
  43.             000
  44.             KKK
  45.             KKK
  46.  
  47.             The remaining relations are at max, 1
  48.             As such, the first instance of this occurs at [1,0]
  49.  
  50.             000
  51.             0KK
  52.             KKK
  53.  
  54.             At this point, only one knight can move.
  55.             Removing the last knight.
  56.  
  57.             000
  58.             0K0
  59.             KKK
  60.  
  61.             And this is how we end up with 5 instead of 4.
  62.             The program does not account for the possibility of anything but relations mattering.
  63.             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.
  64.             This is very resource intensive however.
  65.  
  66.             How to solve the issue with 4 moves?
  67.            
  68.             0K0
  69.             KKK
  70.             0K0
  71.            
  72.             As such, this answer should not be correct
  73.              */
  74.  
  75.             coordsToRemove = FindMostMoves(matrix, numsRemoved);
  76.             matrix[coordsToRemove[0], coordsToRemove[1]] = '0';
  77.  
  78.             //Restart loop with the changes made
  79.             numsRemoved++;
  80.            
  81.             //Console.WriteLine("Debug: Iteration - " + numsRemoved);
  82.             PrintMatrix(matrix);
  83.         }
  84.     }
  85.  
  86.  
  87.  
  88.     static char[,] PopulateMatrix(char[,] matrix, int size)
  89.     {
  90.         char[] userInput;
  91.  
  92.         for (int row = 0; row < size; row++)
  93.         {
  94.             userInput = Console.ReadLine().ToCharArray();
  95.             for (int col = 0; col < size; col++)
  96.             {
  97.                 matrix[row, col] = userInput[col];
  98.             }
  99.         }
  100.  
  101.         return matrix;
  102.     }
  103.  
  104.  
  105.     static void PrintMatrix(char[,] matrix)
  106.     {
  107.         for (int row = 0; row < matrix.GetLength(0); row++)
  108.         {
  109.             for (int col = 0; col < matrix.GetLength(1); col++)
  110.             {
  111.                 Console.Write(matrix[row, col] + " ");
  112.             }
  113.             Console.WriteLine();
  114.         }
  115.     }
  116.  
  117.     static int[] FindMostMoves(char[,] matrix, int numsRemoved)
  118.     {
  119.         int[] coordinatesWithMostMoves = new int[2];
  120.         int mostMoves = 0, currMoves;
  121.         for (int row=0; row<matrix.GetLength(0); row++)
  122.         {
  123.             for(int col=0; col<matrix.GetLength(1); col++)
  124.             {
  125.                 //Only launch functions if the current coordinate refers to a knight
  126.                 if (matrix[row, col] == 'K')
  127.                 {
  128.                     currMoves = FindNumOfMoves(matrix, row, col);
  129.                     if (currMoves > mostMoves)
  130.                     {
  131.                         mostMoves = currMoves;
  132.                         coordinatesWithMostMoves = new int[] { row, col };
  133.                     }
  134.                 }
  135.             }
  136.         }
  137.  
  138.         //If no matches at all, quit the program entirely
  139.         if (mostMoves != 0)
  140.         {
  141.             return coordinatesWithMostMoves;
  142.         } else
  143.         {
  144.             Console.WriteLine(numsRemoved);
  145.             Environment.Exit(0);
  146.             return coordinatesWithMostMoves;
  147.         }
  148.     }
  149.  
  150.     static int FindNumOfMoves(char[,] matrix, int row, int col)
  151.     {
  152.         int counter = 0;
  153.         int indexOfLastCol = matrix.GetLength(1) - 1;
  154.  
  155.         if (row >= 1)
  156.         {
  157.             //Check moves - 1 Up & 2 Left AND 1 Up & 2 Right
  158.             if (col >= 2)
  159.             {
  160.                 if (matrix[row - 1, col - 2] == 'K')
  161.                 {
  162.                     counter++;
  163.                 }
  164.             }
  165.  
  166.            
  167.             if (col <= indexOfLastCol-2)
  168.             {
  169.                 if (matrix[row - 1, col + 2] == 'K')
  170.                 {
  171.                     counter++;
  172.                 }
  173.                
  174.             }
  175.  
  176.             //Check moves - 2 Up & Left AND 2 Up & Right
  177.             if (row >= 2)
  178.             {
  179.                 if (col >= 1)
  180.                 {
  181.                     if (matrix[row - 2, col - 1] == 'K')
  182.                     {
  183.                         counter++;
  184.                     }
  185.                 }
  186.                 if (col <= indexOfLastCol-1)
  187.                 {
  188.                     if (matrix[row - 2, col + 1] == 'K')
  189.                     {
  190.                         counter++;
  191.                     }
  192.                 }  
  193.             }
  194.         }
  195.  
  196.         //Check moves down
  197.         if (row <= indexOfLastCol-1)
  198.         {
  199.             //Check moves - 1 Down & 2 Left AND 1 Down & 2 Right
  200.             if (col >= 2)
  201.             {
  202.                 if (matrix[row + 1, col - 2] == 'K')
  203.                 {
  204.                     counter++;
  205.                 }
  206.             }
  207.             if (col <= indexOfLastCol-2)
  208.             {
  209.                 if (matrix[row + 1, col + 2] == 'K')
  210.                 {
  211.                     counter++;
  212.                 }
  213.             }
  214.            
  215.             //Now to check moves 2 Down
  216.            
  217.            
  218.             if (row <= indexOfLastCol - 2) {
  219.                 //Check moves - 2 Down & 1 Left AND 2 Down & 1 Right
  220.                 if (col >= 1)
  221.                 {
  222.                     if (matrix[row + 2, col - 1] == 'K')
  223.                     {
  224.                         counter++;
  225.                     }
  226.                 }
  227.                 if (col <= indexOfLastCol-1)
  228.                     {
  229.                     if (matrix[row + 2, col + 1] == 'K')
  230.                     {
  231.                         counter++;
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.        
  237.         //Console.WriteLine($"Debug: Counter for [{row},{col}] = {counter}");
  238.         return counter;
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement