提交时间:2024-01-23 21:00:54
运行 ID: 125686
#include<stdio.h> #include<iostream> using namespace std; int chess[4][4]; int c=33; void build() { char c; int i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) { cin>>c; if(c=='w') chess[i][j]=0; else chess[i][j]=1; } } void turn(int x,int y) { if(x>=0&&x<=3&&y>=0&&y<=3) chess[x][y]=!chess[x][y]; } void flip(int s) { int i=s/4; int j=s%4; turn(i,j); turn(i+1,j); turn(i,j+1); turn(i-1,j); turn(i,j-1); } int complete() { int i,j,s1=0; for(i=0;i<4;i++) for(j=0;j<4;j++) s1+=chess[i][j]; if(s1%16) return 0; else return 1; } void dfs(int s,int b) { if(complete()) { if(c>b) c=b; return; } if(s>=16) return; dfs(s+1,b); flip(s); dfs(s+1,b+1); flip(s); } int main() { build(); dfs(0,0); if(c==33) printf("Impossible\n"); else printf("%d\n",c); return 0; }