fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define R 4
  6. #define C 4
  7. #define MIN -1000000000
  8.  
  9. static int grid[R][C] = {
  10. {0,23,20,-32},
  11. {13,14,44,-44},
  12. {23,19,41,9},
  13. {46,27,20,0}
  14. };
  15.  
  16. static int memo[R][C] = {
  17. {MIN,MIN,MIN,MIN},
  18. {MIN,MIN,MIN,MIN},
  19. {MIN,MIN,MIN,MIN},
  20. {MIN,MIN,MIN,MIN}
  21. };
  22.  
  23. int solve(int r, int c) {
  24. if(memo[r][c] != MIN) return memo[r][c];
  25. int res = grid[r][c];
  26. int a = 0, b = 0;
  27. if (r+1 != R) a= solve(r+1, c);
  28. if (c+1 != C) b= solve(r, c+1);
  29. res = max(res+a, res+b);
  30. return memo[r][c] = res;
  31. }
  32.  
  33. int main() {
  34. int add[R][C] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
  35. for (int r = 0 ; r != R ; r++) {
  36. for (int c = 0 ; c != C ; c++) {
  37. if (grid[r][c] < 0) {
  38. for (int dr = -1; dr <= 1 ; dr++) {
  39. for (int dc = -1 ; dc <= 1 ; dc++) {
  40. if ((!dr || !dc) && r+dr >= 0 && r+dr < R && c+dc >= 0 && c+dc < C) {
  41. add[r+dr][c+dc] += grid[r][c];
  42. }
  43. }
  44. }
  45. grid[r][c] = MIN;
  46. }
  47. }
  48. }
  49. for (int r = 0 ; r != R ; r++) {
  50. for (int c = 0 ; c != C ; c++) {
  51. grid[r][c] += add[r][c];
  52. }
  53. }
  54. cout << solve(0, 0) << endl;
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
129