题目地址
AC代码
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int list[4][4];
int ans = pow(2,16);
int boo = 0;
void flip(int x,int y){
list[x][y] ^= 1;
if(x+1<4)list[x+1][y] ^= 1;
if(x-1>=0)list[x-1][y] ^= 1;
if(y-1>=0)list[x][y-1] ^= 1;
if(y+1<4)list[x][y+1] ^= 1;
}
int judge(){
int x,y;
x=y=0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if(list[i][j]==1)x++;
else y++;
}
}
if(x!=0&&y!=0)return 0;
else return 1;
}
void dfs(int x,int y,int n){
if(judge()){
ans = min(ans,n);
boo =1;
return ;
}
if(x>=4||y>=4)return ;
int nx = (x+1)%4;
int ny = y+(x+1)/4;
flip(x,y);
dfs(nx,ny,n+1);
flip(x,y);
dfs(nx, ny, n);
return ;
}
int main() {
for (int i = 0; i <4; ++i) {
for (int j = 0; j < 4; ++j) {
char a;
cin>>a;
if(a=='w')list[i][j]=1;
}
}
dfs(0,0,0);
if(boo)cout<<ans;
else cout<<"Impossible";
return 0;
}
思路和题解
都说这道题直接搜索,但是我就是想不出来这个搜索的路径是什么样的,感谢POJ上某位仁兄的题解,让我学到了这种操作
int nx = (x+1)%4;
int ny = y+(x+1)/4;
完美解决了我不知道搜索路径的问题(滑稽)
其他的地方基本就是大同小异了(看着和别人的相似度挺高的也没办法,因为思路就是这样//菜还是你菜)