Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int arr[15][5];
- int b[5][5];
- const int INF=1e9;
- void oprn(int row)
- {
- for(int i=row;i>max(0,row-5);i--)
- {
- for(int j=0;j<5;j++)
- {
- if(arr[i][j]==2)
- {
- b[row-i][j]=2;
- arr[i][j]=0;
- }
- }
- }
- }
- void revoprn(int row)
- {
- for(int i=row;i>max(0,row-5);i--)
- {
- for(int j=0;j<5;j++)
- {
- if(b[row-i][j]==2)
- {
- arr[i][j]=2;
- }
- }
- }
- }
- int rec(int pos,int coins,int row)
- {
- if(pos<0||pos>4||coins<0)
- return -INF;
- if(arr[row][pos]==2)
- coins-=1;
- else if(arr[row][pos]==1)
- coins+=1;
- if(row==0)
- return coins;
- return max(rec(pos,coins,row-1),max(rec(pos-1,coins,row-1),rec(pos+1,coins,row-1)));
- }
- void solve()
- {
- int n;
- cin>>n;
- for(int i=0;i<n;i++)
- for(int j=0;j<5;j++)
- cin>>arr[i][j];
- int mxcoin=-1;
- for(int i=n-1;i>3;i--)
- {
- oprn(i);
- int coins=max(rec(2,0,n-1),max(rec(1,0,n-1),rec(3,0,n-1)));
- mxcoin=max(mxcoin,coins);
- revoprn(i);
- }
- cout<<mxcoin<<endl;
- return;
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement