lelouche29

collect_max_coins_Samsung

Sep 20th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n;
  5. int mat[15][15];
  6. int ans;
  7.  
  8. bool valid(int x, int y){
  9.     if(x>=0 && x<n && y>=0 && y<5)
  10.         return true;
  11.     return false;
  12. }
  13.  
  14. void solve(int r, int c, int power, bool magic, int score){
  15.     //getting max every time
  16.     ans=max(ans,score);
  17.  
  18.     //base case
  19.     if(r==0){
  20.         ans=max(ans,score);
  21.         return;
  22.     }
  23.     int dy[]={-1,0,1};
  24.     for(int i=0; i<3; i++){
  25.         int x = r-1;
  26.         int y = c+dy[i];
  27.  
  28.         if(valid(x,y)){
  29.             if(mat[x][y]==2){
  30.                 if(magic)
  31.                     solve(x,y,5,0,score);
  32.                
  33.                 if(score>0)
  34.                     solve(x,y,power-1,magic,score-1);
  35.  
  36.                 if(power>0)
  37.                     solve(x,y,power-1,0,score);
  38.  
  39.                 if(score==0 && power<=0 && magic==0){
  40.                     ans=max(ans,score);
  41.                     return;
  42.                 }
  43.             }
  44.             if(mat[x][y]==1) solve(x,y,power-1,magic,score+1);
  45.             if(mat[x][y]==0) solve(x,y,power-1,magic,score);
  46.         }
  47.     }
  48. }
  49.  
  50. int main() {
  51.     int t;
  52.     cin>>t;
  53.     while(t--){
  54.         cin>>n;
  55.  
  56.         for(int i=0; i<n; i++)
  57.             for(int j=0; j<5; j++)
  58.                 cin>>mat[i][j];
  59.  
  60.         ans=-1;
  61.         solve(n,2,0,1,0);
  62.         if(ans==0) cout<<-1<<endl;
  63.         else cout<<ans<<endl;
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment