Advertisement
kevinqli

SCTF - Cookies

Mar 1st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. ifstream fin("cookies.txt");
  6.  
  7. bool counted[50][10][10] = {0};
  8. bool win[50][10][10];
  9.  
  10. // returns true if player 1 wins under the given m, n conditions, false otherwise
  11. // p = number of cookies previous player took, 0 if there is no previous player
  12. bool rec(int m, int n, int p) {
  13.     if(counted[m][n][p]) return win[m][n][p];
  14.     if(m == 1 && p == 1) return false;
  15.     bool flag = false;
  16.     for(int i = min(n, m); i >= 1; i--) {
  17.         if(i == p) continue;
  18.         if(!rec(m-i, n, i)) {
  19.             flag = true;
  20.             break;
  21.         }
  22.     }
  23.     counted[m][n][p] = true;
  24.     win[m][n][p] = flag;
  25.     return flag;
  26. }
  27.  
  28. int main() {
  29.     int counter = 0;
  30.     int N;
  31.     int m, n;
  32.     fin >> N;
  33.     for(int i = 1; i <= N; i++) {
  34.         fin >> m >> n;
  35.         if(rec(m, n, 0)) counter++;
  36.     }
  37.     cout << counter << endl;
  38.     system("pause");
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement