ann8497

bomb enemy samsung

Aug 14th, 2019
1,750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. /*
  2. You’ll be given a grid as below:
  3.  
  4.                        0 1 0 2 0 --> Non highlighted part
  5.                        0 2 2 2 1
  6.                        0 2 1 1 1
  7.                        1 0 1 0 0
  8.                        0 0 1 2 2
  9.                        1 1 0 0 1
  10.                        x x S x x  -->highlighted yellow
  11.  In the grid above,    
  12. 1: This cell has a coin.
  13. 2: This cell has an enemy.
  14. 0: It contains nothing.
  15. The highlighted(yellow) zone is the control zone. S is a spaceship that we need to control so that we can get maximum coins.
  16. Now, S’s initial position will be at the center and we can only move it right or left by one cell or do not move. At each time, the non-highlighted part of the grid will move down by one unit.
  17. We can also use a bomb but only once. If we use that, all the enemies in the 5×5 region above the control zone will be killed.
  18. If we use a bomb at the very beginning, the grid will look like this:
  19.  
  20. 0 1 0 2 0  --> Non highlighted part
  21. 0 0 0 0 1
  22. 0 0 1 1 1
  23. 1 0 1 0 0
  24. 0 0 1 0 0
  25. 1 1 0 0 1
  26. x x S x x  --> highlighted yellow
  27.  As soon as, the spaceship encounters an enemy or the entire grid has come down, the game ends.    
  28. For example,    
  29. At the very first instance, if we want to collect a coin we should move left **( coins=1)**. This is because when the grid comes down by 1 unit we have a coin on the second position and by moving left we can collect that coin. Next, we should move right to collect another coin **( coins=2)** .
  30. After this, remain at the same position **( coins=4)**.
  31. This is the current situation after collecting 4 coins.
  32. 0 1 0 2 0                0 1 0 0 0
  33. 0 2 2 2 1 -->after using 0 0 0 0 1
  34. x x S x x -->bomb        x x S x x
  35. Now, we can use the bomb to get out of this situation. After this, we can collect at most 1 coin. So maximum coins=5.
  36. */
  37.  
  38.  
  39. #include <iostream>
  40. using namespace std;
  41.  
  42. int a[100][100];
  43. int n;
  44.  
  45. bool valid(int r, int c){
  46.   if(r<n && r>=0 && c<5 && c>=0)return true;
  47.   return false;
  48. }
  49.  
  50. int solve(int r, int c, int power, int effect){
  51.  
  52.   // base case
  53.   if(r<0)return 0;
  54.  
  55.   // recursive case
  56.   int ans = 0;
  57.   int p = 0;
  58.   for(int i = -1; i <=1; i++){
  59.  
  60.     int y = c+i;
  61.     int x = r-1;
  62.    
  63.     if(valid(x,y)){
  64.       // enemy
  65.       if(a[x][y] == 2){
  66.          if(power == 0 && effect >  0){
  67.             p = solve(x,y,power,effect -1);
  68.          }
  69.          if(power == 1){
  70.            p = solve(x,y,power-1,5);
  71.          }
  72.       }
  73.       // not enemy
  74.       if(a[x][y] == 1 ||a[x][y] == 0){
  75.         if(power == 0)
  76.         p = solve(x,y,power,effect-1);
  77.         else
  78.         p = solve(x,y,power,5);
  79.       }
  80.     }
  81.     ans = max(ans , p);
  82.   }
  83.    if(a[r][c] == 1)ans++;
  84.    return ans;
  85. }
  86.  
  87. int main() {
  88.    
  89.     cin>>n;
  90.     for(int i = 0; i<100; i++)for(int j =0; j<100; j++)a[i][j] = 0;
  91.     for(int i =0; i<n; i++){
  92.       for(int j = 0; j<5; j++){
  93.         cin>>a[i][j];
  94.       }
  95.     }
  96.    
  97.     cout<<solve(n,2,1,0)<<endl;
  98.     return 0;
  99. }
Add Comment
Please, Sign In to add comment