Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. int n,w,h;
  4.  
  5. char buf[100][102];
  6.  
  7. int dp[101][101][101];
  8.  
  9. int min(int a, int b){
  10.   return a < b ? a : b;
  11. }
  12.  
  13. int max(int a, int b){
  14.   return a > b ? a : b;
  15. }
  16.  
  17. #define neginf -10001
  18.  
  19. int scoretbl[] = {
  20.   neginf, neginf, 0, 0, 0, 0, 0, neginf, neginf, 0, 0, neginf, neginf, 0, 2, 1, 0, 0, 1, 1, 0, 0, 0
  21. };
  22.  
  23.  
  24. static int solve(){
  25.   for(int x = 0; x < h; x++){
  26.     for(int y = 0; y < w; y++){
  27.       int z = x+y;
  28.       int lim = min(h, z+1);
  29.       for(int nx = z >= w ? z - w + 1: 0; nx < lim; nx++){
  30.     int ny = z-nx;
  31.     int ans = scoretbl[buf[x][y]+buf[nx][ny]-70+(x == nx)];
  32.     if(ans < 0){
  33.       dp[x+1][y+1][nx+1] = -1;
  34.       continue;
  35.     }
  36.     dp[x+1][y+1][nx+1] = max(max(max(dp[x+1][y][nx+1], dp[x+1][y][nx]), dp[x][y+1][nx+1]), dp[x][y+1][nx])+ans;
  37.       }
  38.     }
  39.   }
  40. }
  41.  
  42.  
  43.  
  44. int main(){
  45.   scanf("%d\n", &n);
  46.   while(n--){
  47.     scanf("%d %d\n", &w, &h);
  48.     for(int i = 0; i<h; i++){
  49.       scanf("%s", buf[i]);
  50.     }
  51.     solve();
  52.     printf("%d\n", dp[h][w][h]);
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement