Pagini recente » Cod sursa (job #3347704) | Cod sursa (job #3326427) | Cod sursa (job #3349844) | Cod sursa (job #3328878) | Cod sursa (job #3328200)
#include <fstream>
using namespace std;
ifstream cin("perle.in");
ofstream cout("perle.out");
#define MAXN 10000
int v[MAXN];
int n,pos;
int check(char type){
if(pos==n){
return 0;
}
if(type=='A'){
pos++;
return 1;
}else if(type=='B'){
//B->2B
if(v[pos]==2){
pos++;
return check('B');
}
//B->1A3AC
if(v[pos]==1){
pos++;
if(!check('A')){
return 0;
}
if(pos==n||v[pos]!=3){
return 0;
}
pos++;
return (check('A')&&check('C'));
}
}else{
//C->2
if(v[pos]==2){
pos++;
return 1;
}
//C->12A
if(v[pos]==1){
pos++;
if(pos==n||v[pos]!=2){
return 0;
}
pos++;
return check('A');
}
//C->3BC
if(v[pos]==3){
pos++;
return (check('B')&&check('C'));
}
}
return 0;
}
int main(){
int t,i;
cin>>t;
while(t--){
cin>>n;
for(i=0;i<n;i++){
cin>>v[i];
}
pos=0;
if(check('A')&&pos==n){
cout<<1<<"\n";
continue;
}
pos=0;
if(check('B')&&pos==n){
cout<<1<<"\n";
continue;
}
pos=0;
if(check('C')&&pos==n){
cout<<1<<"\n";
continue;
}
cout<<0<<"\n";
}
return 0;
}