title: Coin Flipping Counting Game C++ Data Structure！
body: <p>Here is the question. In the game, there is a rectangular grid of coins, with heads =1 and tails = 0. The game has one simple rule: the player cannot flip a single coin, instead, the player can choose one row (or one column) and flip all the coins in that row (or that column) simultaneously. The objective of the game is to find out a strategy to flip the coins so that the number of head coins is maximized. The first input value is row >> then column >> and the coins</p>
  
  <pre><code>Sample inputs:
  5 4
  1010
  0101
  1010
  1010
  1010 //Sample output of this: 20
  5 4
  0010
  1101
  0110
  0110
  1011 //Sample output of this: 17
  </code></pre>
  
  <p>I finished my code using the method of counting the '0' and '1', if zero is more, switch it. This method only pass the simple test case, but when it goes to the hard one, it failed because there are some cases require twitting more than once. I cannot think of another better way of dealing with it. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  bool ConeIsMore(vector &lt;vector&lt;char&gt; &gt; table, int size, int j) {
      int countzero = 0;
      int countone = 0;
      for (int i = 0; i &lt; size; i++) {
          (table[i][j] == '0')?++countzero: ++countone;
      }
      if (countone &gt;= countzero) {
          return true;
      }
      return false;
  }
  
  bool RoneIsMore(vector &lt;vector&lt;char&gt; &gt; table, int size, int i) {
      int countzero = 0;
      int countone = 0;
      for (int j = 0; j &lt; size; j++) {
          (table[i][j] == '0') ? ++countzero : ++countone;
      }
      if (countone &gt;= countzero) {
          return true;
      }
      return false;
  }
  
  int main() {
      //Initialise row and column
      int row; 
      int column;
      while (cin &gt;&gt; row &gt;&gt; column) {
          //Initiallise 2D vector
          vector &lt;vector&lt;char&gt; &gt; table(row, vector&lt;char&gt;(column));
  
          //get each digit of number and store it into number
          for (int i = 0; i &lt; row; i++) {
              for (int j = 0; j &lt; column; j++) {
                  cin &gt;&gt; table[i][j];
              }
          }
  
          //check for column
          for (int j = 0; j &lt; column; j++) {
              if (!ConeIsMore(table, row, j)) {
                  for (int i = 0; i &lt; row; i++) {
                      (table[i][j] == '0') ? table[i][j] = '1' : table[i][j] = '0';
                  }
              }
          }
  
          //check for row
          for (int j = 0; j &lt; row; j++) {
              if (!RoneIsMore(table, column, j)) {
                  for (int i = 0; i &lt; column; i++) {
                      (table[j][i] == '0') ? table[j][i] = '1' : table[j][i] = '0';
                  }
              }
          }
  
          //Count One in the table
          int ans = 0;
          for (int i = 0; i &lt; row; i++) {
              for (int j = 0; j &lt; column; j++) {
                  (table[i][j] == '1') ? (ans++) : (ans = ans);
              }
          }
          cout &lt;&lt; ans &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>When I study through the test cases, I fount out there are some requires checking various time which makes me feel my method is not a good one. Can anyone suggest a better way in dealing with it? Thank you so much.</p>
  
  <p>The followings are harder test cases:</p>
  
  <pre><code>5 4
  0010
  1101
  0110
  0110
  1011 //17
  
  5 4
  0110
  1111
  0101
  0110
  0100  //16
  
  5 4
  0110
  1001
  0011
  1110
  1000 //16
  
  5 4
  1100
  0001
  1111
  0101
  1010 //16
  
  5 4
  0101
  0110
  1001
  1000
  0011 //16
  
  5 4
  0111
  1100
  0100
  1000
  1011 //16
  
  5 4
  1101
  1110
  0111
  1011
  0111 //15
  
  5 4
  1100
  1001
  0110
  1001
  1000 //17
  </code></pre>