Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
超长代码附上测试数据bwbw wwww bbwb bwwb Impossible bwwb bbwb bwwb bwww 4 wwww wwww wwww wwww 0 bbbb bbbb bbbb bbbb 0 bbbb bwbb bbbb bbbb Impossible bwbb bwbb bwbb bbbb Impossible bwbb wwwb bwbb bbbb 1 wwww wwwb wwbb wwwb 1 wwww wwww wwwb wwbb 1 wbwb bwbw wbwb bwbw Impossible bbbb bwwb bwwb bbbb 4 bwwb wbbw wbbw bwwb 4 bbww bbww wwbb wwbb Impossible bbwb bbbw wwbb wwwb Impossible wwwb wwbw wbww wwbw Impossible bbbb wwww wwbb wbbb Impossible bwwb wbwb wbbb wbbb 4 bwbb bwbb bwbw bbbw 5 wbwb bbbb bbww wbbb 6 bbwb bbbb wbwb bbbb 5 #include<iostream> #include<cstring> using namespace std; int matrix[4][4]; string str[16]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; int Flip(string str,int color){ int total(0); int a[4][4]; bool flag(true); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ a[i][j] = matrix[i][j]; } } for(int i=0;i<4;i++){ int tmp = str[i]-'0'; if(tmp==1){ total++; a[0][i] = 1-tmp; if(i==0){ a[0][1] = 1 - a[0][1]; a[1][0] = 1 - a[1][0]; }else if(i==3){ a[0][2] = 1 - a[0][2]; a[1][3] = 1 - a[1][3]; }else{ a[0][i-1] = 1 - a[0][i-1]; a[0][i+1] = 1 - a[0][i+1]; a[1][i] = 1 - a[1][i]; } } } for(int i=1;i<3;i++){ for(int j=0;j<4;j++){ if(a[i-1][j]==color){ total++; a[i][j] = 1 - a[i][j]; a[i+1][j] = 1 - a[i+1][j]; a[i-1][j] = 1 - a[i-1][j]; if(j>0) a[i][j-1] = 1 - a[i][j-1]; if(j<3) a[i][j+1] = 1 - a[i][j+1]; } } } for(int i=0;i<4;i++){ if(i==0){ if(a[2][0]==color){ total++; a[3][0] = 1 - a[3][0]; a[3][1] = 1 - a[3][1]; } } if(i==2||i==1){ if(a[3][i-1]!=a[2][i]){ flag = false; total = 0; break; }else{ if(a[3][i-1]==color){ total++; a[3][i] = 1 - a[3][i]; a[3][i-1] = 1 - a[3][i-1]; a[3][i+1] = 1 - a[3][i+1]; a[2][i] = 1 - a[2][i]; } } } if(i==3){ if(a[3][2]!=a[3][3]||a[3][2]!=a[2][3]||a[2][3]!=a[3][3]){ flag = false; total=0; break; }else{ if(a[3][2]==color){ total++; } } } } if(!flag){ total==0; } return total; } int main(){ int test(0); for(int i=0;i<4;i++){ string st; cin>>st; for(int j=0;j<4;j++){ if(st[j]=='b'){ matrix[i][j]=1; test++; }else{ matrix[i][j]=0; } } } if(test==0||test==16){ cout<<0<<endl; return 0; } int min_num(16); string tmp; for(int i=0;i<16;i++){ tmp = str[i]; int num1 = Flip(tmp,0); int num2 = Flip(tmp,1); if(num1!=0){ if(num1<min_num){ min_num = num1; } } if(num2!=0){ if(num2<min_num){ min_num = num2; } } } if(min_num==0||min_num==16) cout<<"Impossible"<<endl; else cout<<min_num<<endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator