水管工游戏
#include<stdio.h> int map[11][11]; int book[11][11]; int top; int f=0,n,m; struct hh { int x; int y; }hh[100]; void gjx(int x,int y,int jinshuikou) { if(x==n && y==m+1) { f=1; for(int l=1;l<=top;l++) printf("<%d,%d>",hh[l].x,hh[l].y); return; } if(x<1 || y<1 || x>n || y>m) return; if(book[x][y]==1) return; book[x][y]=1; top++; hh[top].x=x; hh[top].y=y; if(jinshuikou==1) { if(map[x][y]>=5 && map[x][y]<=6) gjx(x,y+1,1); if(map[x][y]>=1 && map[x][y]<=4) { gjx(x+1,y,2); gjx(x-1,y,4); } book[x][y]=0; } if(jinshuikou==2) { if(map[x][y]>=5 && map[x][y]<=6) gjx(x+1,y,2); if(map[x][y]>=1 && map[x][y]<=4) { gjx(x,y+1,1); gjx(x,y-1,3); } book[x][y]=0; } if(jinshuikou==3) { if(map[x][y]>=5 && map[x][y]<=6) gjx(x-1,y,3); if(map[x][y]>=1 && map[x][y]<=4) { gjx(x-1,y,4); gjx(x+1,y,2); } book[x][y]=0; } if(jinshuikou==4) { if(map[x][y]>=5 && map[x][y]<=6) gjx(x,y-1,4); if(map[x][y]>=1 && map[x][y]<=4) { gjx(x,y+1,1); gjx(x,y-1,3); } book[x][y]=0; } } int main() { int i,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&map[i][j]); gjx(1,1,1); if(f==0) printf("impossible"); return 0; }