题目分析:
注意 1.一个变换 用4次 ,相当于没有变换,用一个number[i]数组记录每个变换次数
2.确定的变换 序列,与其变换顺序,没有关系(没注意这所以一直写不对)
/* ID:wconvey PROG:clocks LANG:C++ */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int maze[5][5]; int path[1000],pa[1000],number[10]; int ans; bool success() { int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum+=maze[i][j]; if(sum==12*9) return true; else return false; } void copy(int k) { for(int i=1;i<=k;i++) pa[i]=path[i]; } void move(int k) { if(k==1) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; } else if(k==2) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; } else if(k==3) { if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; } else if(k==4) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; } else if(k==5) { if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; } else if(k==6) { if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } else if(k==7) { if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; } else if(k==8) { if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } else { if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } } void back(int k) { if(k==1) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; } else if(k==2) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; } else if(k==3) { if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; } else if(k==4) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; } else if(k==5) { if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; } else if(k==6) { if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } else if(k==7) { if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; } else if(k==8) { if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } else { if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } } void dfs(int k) { if(success()) { if(k-1<ans) { ans=k-1; copy(ans); } return ; } if(k-1<ans) { if(k==1) { for(int i=1;i<=9;i++) { move(i); path[k]=i; number[i]++; dfs(k+1); back(i); number[i]--; } } else { for(int i=path[k-1];i<=9;i++) { if(number[i]==3) continue; move(i); path[k]=i; number[i]++; dfs(k+1); back(i); number[i]--; } } } /* for(int i=1;i<=9;i++) { if(k>=4) { if(path[k-3]==path[k-2] && path[k-2]==path[k-1]) { if(i>path[k-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } else { if(i>=path[k-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } } else { if(i>path[i-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } }*/ } int main() { freopen("clocks.in","r",stdin); freopen("clocks.out","w",stdout); for(int i=0;i<3;i++) for(int j=0;j<3;j++) scanf("%d",&maze[i][j]); ans=1000; memset(number,0,sizeof(number)); dfs(1); for(int i=1;i<ans;i++) printf("%d ",pa[i]); printf("%d\n",pa[ans]); //system("pause"); return 0; } /* #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int maze[5][5]; int path[100],pa[100]; int ans,n; bool success() { int sum=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) sum+=maze[i][j]; if(sum==12*9) return true; else return false; } void copy(int k) { for(int i=1;i<=k;i++) pa[i]=path[i]; } void move(int k) { if(k==1) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; } else if(k==2) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; } else if(k==3) { if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; } else if(k==4) { if(maze[0][0]!=12) maze[0][0]+=3; else maze[0][0]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; } else if(k==5) { if(maze[0][1]!=12) maze[0][1]+=3; else maze[0][1]=3; if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; } else if(k==6) { if(maze[0][2]!=12) maze[0][2]+=3; else maze[0][2]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } else if(k==7) { if(maze[1][0]!=12) maze[1][0]+=3; else maze[1][0]=3; if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; } else if(k==8) { if(maze[2][0]!=12) maze[2][0]+=3; else maze[2][0]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } else { if(maze[1][1]!=12) maze[1][1]+=3; else maze[1][1]=3; if(maze[1][2]!=12) maze[1][2]+=3; else maze[1][2]=3; if(maze[2][1]!=12) maze[2][1]+=3; else maze[2][1]=3; if(maze[2][2]!=12) maze[2][2]+=3; else maze[2][2]=3; } } void back(int k) { if(k==1) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; } else if(k==2) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; } else if(k==3) { if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; } else if(k==4) { if(maze[0][0]!=3) maze[0][0]-=3; else maze[0][0]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; } else if(k==5) { if(maze[0][1]!=3) maze[0][1]-=3; else maze[0][1]=12; if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; } else if(k==6) { if(maze[0][2]!=3) maze[0][2]-=3; else maze[0][2]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } else if(k==7) { if(maze[1][0]!=3) maze[1][0]-=3; else maze[1][0]=12; if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; } else if(k==8) { if(maze[2][0]!=3) maze[2][0]-=3; else maze[2][0]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } else { if(maze[1][1]!=3) maze[1][1]-=3; else maze[1][1]=12; if(maze[1][2]!=3) maze[1][2]-=3; else maze[1][2]=12; if(maze[2][1]!=3) maze[2][1]-=3; else maze[2][1]=12; if(maze[2][2]!=3) maze[2][2]-=3; else maze[2][2]=12; } } void dfs(int k) { //printf("%d**",k); if(k>ans) return ; if(success()) { if(k-1<ans) { ans=k-1; copy(ans); } if(k-1==ans) { if(path[1]<pa[1]) copy(ans); } printf("%d***",ans); return ; } for(int i=1;i<=9;i++) { if(k>=4) { if(path[k-3]==path[k-2] && path[k-2]==path[k-1]) { if(i>path[k-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } else { if(i>=path[k-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } } else { if(i>path[i-1]) { move(i); path[k]=i; dfs(k+1); back(i); } } } } void put() { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) printf("%d ",maze[i][j]); printf("\n"); } printf("\n\n"); } int main() { for(int i=0;i<3;i++) for(int j=0;j<3;j++) scanf("%d",&maze[i][j]); ans=27; dfs(1); for(int i=1;i<=ans;i++) printf("%d ",pa[i]); printf("%d\n",pa[ans]); system("pause"); return 0; }*/