SuitNdtie

1000s

Apr 19th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int moveI[256];
  5. int moveJ[256];
  6.  
  7. int abs(int x){
  8.     return (x < 0 ? x * -1 : x);
  9. }
  10. int max(int a,int b){
  11.     return (a > b ? a : b);
  12. }
  13. char str[110];
  14. int n;
  15. /*int dp[101][101][101][101];
  16. int cal(int index,int willcut,int I,int J)
  17. {
  18.     if(willcut < 0)return -2e9;
  19.     if(index == n){
  20.         if(willcut != 0)return -2e9;
  21.         return (abs(I) + abs(J))*2;
  22.     }
  23.     if(dp[index][willcut][I][J] != -1)return dp[index][willcut][I][J];
  24.     return dp[index][willcut][I][J] = max(cal(index+1,willcut,I + moveI[str[index]] , J + moveJ[str[index]]),cal(index+1,willcut-1,I,J));
  25. }*/
  26. int posI = 0 , posJ = 0;
  27. int k;
  28. int cal(int index,int cut,int I,int J){
  29.     if(cut == k){
  30.     //  printf("Test (%d,%d) - (%d,%d) = (%d,%d) -> cal : %d\n",posI,posJ,I,J,posI - I,posJ - J,(abs(posI - I) + abs(posJ - J))*2);
  31.         return (abs(posI - I) + abs(posJ - J))*2;  
  32.     }
  33.     if(index == n)return -2e9;
  34.     return max(cal(index+1,cut+1,I + moveI[str[index]] , J + moveJ[str[index]]),cal(index+1,cut,I,J)); 
  35. }
  36. int dp[2][110][210][210];
  37. int main()
  38. {
  39.     moveI['N'] = 1;moveI['S'] = -1;
  40.     moveJ['E'] = 1;moveJ['W'] = -1;
  41.    
  42.     scanf("%s",str);
  43.     n = strlen(str);
  44.     for(int i=0; i < n ; i++){
  45.         posI += moveI[str[i]];
  46.         posJ += moveJ[str[i]];
  47.     }
  48.    
  49.     scanf("%d",&k);
  50.    
  51.     for(int index = 0 ; index < 2 ; index ++){
  52.         for(int cut = k ; cut <= k ; cut ++){
  53.             for(int I = -100 ; I <= 100 ; I ++){
  54.                 for(int J = -100 ; J <= 100 ; J ++){
  55.                     dp[index][cut][I+101][J+101] = (abs(posI - I) + abs(posJ - J))*2;
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     for(int index = n-1 ; index >= 0 ; index --){
  61.         for(int cut = k-1 ; cut >= 0 ; cut --){
  62.             for(int I = -100 ; I <= 100 ; I ++){
  63.                 for(int J = -100 ; J <= 100 ; J ++){
  64.                     dp[index%2][cut][I+101][J+101] = max(dp[(index+1)%2][(cut+1)][I + moveI[str[index]] + 101][J + moveJ[str[index]] + 101],dp[(index+1)%2][cut][I+101][J+101]);
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     printf("%d",dp[0][0][101][101]);
  70. //  printf("%d",cal(0,0,0,0));
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment