Advertisement
dim4o

LineInverter

Nov 20th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class LineInverter
  5. {
  6.     static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.         char[][] matrix = new char[n][];
  10.         for (int i = 0; i < n; i++)
  11.         {
  12.             matrix[i] = Console.ReadLine().ToCharArray();
  13.         }
  14.         int minSteps = 0;
  15.  
  16.         while (true)
  17.         {
  18.             if (IsBlack(matrix) || minSteps > n * n)
  19.             {
  20.                 break;
  21.             }
  22.             var bestRow = FindBestRow(matrix);
  23.             var bestCol = FindBestCol(matrix);
  24.             if (bestRow.Item2 >= bestCol.Item2)
  25.             {
  26.                 InvertRow(bestRow.Item1, matrix);
  27.             }
  28.             else
  29.             {
  30.                 InvertCol(bestCol.Item1, matrix);
  31.             }
  32.             minSteps++;
  33.         }
  34.         Console.WriteLine(minSteps > n * n ? -1 : minSteps);
  35.     }
  36.  
  37.     private static bool IsBlack(char[][] matrix)
  38.     {
  39.         int blackCount = matrix.Sum(m => matrix.Where((t, col) => m[col] == 'B').Count());
  40.         return blackCount == matrix.Length*matrix.Length;
  41.     }
  42.  
  43.     private static Tuple<int, int> FindBestCol(char[][] matrix)
  44.     {
  45.         int bestCol = 0;
  46.         int bestCount = 0;
  47.         for (int col = 0; col < matrix.Length; col++)
  48.         {
  49.             int count = matrix.Count(t => t[col] == 'W');
  50.             if (count > bestCount)
  51.             {
  52.                 bestCount = count;
  53.                 bestCol = col;
  54.             }
  55.         }
  56.         return new Tuple<int, int>(bestCol, bestCount);
  57.     }
  58.  
  59.     private static Tuple<int, int> FindBestRow(char[][] matrix)
  60.     {
  61.         int bestRow = 0;
  62.         int bestCount = 0;
  63.         for (int row = 0; row < matrix.Length; row++)
  64.         {
  65.             if (matrix[row].ToList().Count(c => c == 'W') > bestCount)
  66.             {
  67.                 bestCount = matrix[row].ToList().Count(c => c == 'W');
  68.                 bestRow = row;
  69.             }
  70.         }
  71.         return new Tuple<int, int>(bestRow, bestCount);
  72.     }
  73.  
  74.     private static void InvertRow(int row, char[][] matrix)
  75.     {  
  76.         for (int col = 0; col < matrix.Length; col++)
  77.         {
  78.             matrix[row][col] = matrix[row][col] == 'W' ? 'B' : 'W';
  79.         }
  80.     }
  81.  
  82.     private static void InvertCol(int col, char[][] matrix)
  83.     {
  84.         for (int row = 0; row < matrix.Length; row++)
  85.         {
  86.             matrix[row][col] = matrix[row][col] == 'W' ? 'B' : 'W';
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement