nikolaj

knight

May 10th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <algorithm>
  4. #include <cstring>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. const int n = 8;
  10. const int NAN = -239;
  11. const int INF = 1e9;
  12. int c[n][n];
  13. int dp[n][n][2];
  14.  
  15. int solve(int x, int y, int f) {
  16.     if (x >= n || y >= n) return f ? -INF : INF;
  17.     int &res = dp[x][y][f];
  18.     if (res != NAN) return res;
  19.     if (x == n - 1 && y == n - 1) return res = f ? c[x][y] : -c[x][y];
  20.     if (f) {
  21.         res = c[x][y] + min(solve(x + 1, y, f ^ 1), min(solve(x, y + 1, f ^ 1), solve(x + 1, y + 1, f ^ 1)));
  22.     } else {
  23.         res = -c[x][y] + max(solve(x + 1, y, f ^ 1), max(solve(x, y + 1, f ^ 1), solve(x + 1, y + 1, f ^ 1)));
  24.     }
  25.     return res;
  26. }
  27.  
  28. int main() {
  29.     for (int i = 0; i < n; i++) {
  30.         for (int j = 0; j < n; j++) {
  31.             scanf("%d", &c[n - 1 - i][j]);
  32.         }
  33.     }
  34.     assert(c[0][0] == 0);
  35.     for (int i = 0; i < n; i++)
  36.         for (int j = 0; j < n; j++)
  37.             for (int l = 0; l < 2; l++)
  38.                 dp[i][j][l] = NAN;
  39.     printf("%d\n", solve(0, 0, 0));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment