Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int mat[15][15];
- int b[15][15];
- void detonate(int row){
- for(int i=row; i >(row-5); i--){
- for(int j=0; j<5; j++){
- b[row-i][j]=0;
- if(i>=0 && mat[i][j]==2){
- b[row-i][j]=2;
- mat[i][j]=0;
- }
- }
- }
- }
- void undetonate(int row){
- for (int i = row; i > row - 5; i--){
- for (int j = 0; j < 5; j++){
- if (i >= 0 && b[row - i][j] == 2)
- mat[i][j] = 2;
- }
- }
- }
- void solve(int cur_row, int cur_col, int points,int &maxPoints){
- //base condition
- if(cur_col <0 || cur_col>4) return;
- if(mat[cur_row-1][cur_col]==1) points+=1;
- else if(mat[cur_row-1][cur_col]==2) points-=1;
- if(cur_row==0) {
- maxPoints=max(maxPoints,points);
- return;
- }
- else{
- solve(cur_row -1, cur_col +1, points, maxPoints);
- solve(cur_row -1, cur_col -1, points, maxPoints);
- solve(cur_row -1, cur_col, points, maxPoints);
- }
- }
- int main() {
- int t;
- cin>>t;
- while(t--){
- int n;
- cin>>n;
- for(int i=0; i<n; i++)
- for(int j=0; j<5; j++)
- cin>>mat[i][j];
- int maxPoints=-1;
- for(int i=n-1; i>=0; i--){
- int points=-1;
- detonate(i);
- solve(n,2,0,points);
- maxPoints= max(points,maxPoints);
- undetonate(i);
- }
- int ans=maxPoints <0 ? -1 : maxPoints;
- cout<<ans<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment