http://acm.timus.ru/problem.aspx?space=1&num=1122
简单搜索
难题练思维 水题练细心 我的细心还不够呀 一个小地方写错了 一直过不了 真想杀了自己 以后要多注意呀
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<queue> #include<map> #include<stack> #include<algorithm> #include<cmath> using namespace std; //#pragma comment(linker,"/STACK:1000000000,1000000000") #define LL long long const int INF=0x3f3f3f3f; const int N=15; int graph[N][N]; int turn[N]; int x[9]={-1,-1,-1,0,0,0,1,1,1}; int y[9]={-1,0,1,-1,0,1,-1,0,1}; int ans; int Tturn(int i,int j,int k) { for(int w=0;w<9;++w) { if(turn[w]==0) continue; int l1=i+x[w]; int l2=j+y[w]; if(l1>=1&&l1<=4&&l2>=1&&l2<=4) { if(graph[l1][l2]) {--k;graph[l1][l2]=0;} else {++k;graph[l1][l2]=1;} } } return k; } void dfs(int i,int j,int k,int num) { if(num>=ans) return ; if(i==5) { if(k==0||k==16) { if(num<ans) ans=num; } return ; } int l1,l2; if(j==4) {l1=i+1;l2=1;} else {l1=i;l2=j+1;} dfs(l1,l2,k,num); k=Tturn(i,j,k); dfs(l1,l2,k,num+1); Tturn(i,j,k); } int input() { char c; int k=0; for(int i=1;i<=4;++i) for(int j=1;j<=4;++j) { cin>>c; if(c=='W') {graph[i][j]=1;++k;} else graph[i][j]=0; } for(int i=0;i<9;++i) { cin>>c; if(c=='1') turn[i]=1; else turn[i]=0; } return k; } int main() { //freopen("data.txt","r",stdin); int k=input(); ans=INF; dfs(1,1,k,0); if(ans==INF) printf("Impossible\n"); else printf("%d\n",ans); return 0; }