Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
825 B
0
Indexable
Never
#include<iostream>
using namespace std;
int map[50][5];
int cau,x,y,N;
int cnt,maxx;
int dy[3]={-1,0,1};
void backtrack(int x,int y,int cnt,int cau){
	if(x==0){
		if(cnt>maxx){
			maxx=cnt;
		}
		return;
	}
	
	for(int i=0;i<3;i++){
		int xx=x-1;
		int yy=y+dy[i];
		if(xx>=0&&xx<N&&yy>=0&&yy<5){
			if(map[xx][yy]==1){
				backtrack(xx,yy,cnt+1,cau);
			}
			else if(map[xx][yy]==2){
				if(cau!=0){
					backtrack(xx,yy,cnt,0);
				}
			}
			else if(map[xx][yy]==0){
				backtrack(xx,yy,cnt,cau);
			}
		}
	}
}
int main(){
	//freopen("input.txt","r",stdin);
	int T;
	cin>>T;
	for(int tc=1;tc<=T;tc++){
		
		cin>>N;
		for(int i=0;i<N;i++){
			for(int j=0;j<5;j++){
				cin>>map[i][j];
			}
		}
		maxx=-1;
		backtrack(N,2,0,1);
		cout<<"#"<<tc<<" "<<maxx<<endl;
	}
	return 0;
}