1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| #include<cstdio> #include<cstring> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=12; const int ro1[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; const int ro2[7][7]={ {0,0,0,0,0,0,0}, {0,0,4,2,5,3,0}, {0,3,0,6,1,0,4}, {0,5,1,0,0,6,2}, {0,2,6,0,0,1,5}, {0,4,0,1,6,0,3}, {0,0,3,5,2,4,0} }; char s[25]; int r,c,x_sta,y_sta,u,f,g[maxn][maxn]; int vis[maxn][maxn][7][7]; struct Node{ int x,y,u,f; Node(int x=0,int y=0,int u=0,int f=0):x(x),y(y),u(u),f(f){} }; Node state[maxn][maxn][7][7]; bool read(){ if(scanf("%s",s)==EOF||!strcmp(s,"END")) return false; memset(g,0,sizeof(g)); scanf("%d%d%d%d%d%d",&r,&c,&x_sta,&y_sta,&u,&f); for(int i=1;i<=r;++i) for(int j=1;j<=c;++j) scanf("%d",&g[i][j]); return true; } Node rotate(Node pre,int dis){ Node cur=pre; cur.x+=ro1[dis][0],cur.y+=ro1[dis][1]; if(dis==0){cur.f=7-cur.f;swap(cur.u,cur.f);} if(dis==1){cur.u=7-cur.u;swap(cur.u,cur.f);} if(dis==2){cur.u=ro2[cur.u][cur.f];} if(dis==3){cur.u=7-ro2[cur.u][cur.f];} return cur; } bool match(Node cur,int i){ if(g[cur.x+ro1[i][0]][cur.y+ro1[i][1]]==-1) return true; if(g[cur.x+ro1[i][0]][cur.y+ro1[i][1]]==cur.u) return true; return false; } bool in(Node cur,int i){ return cur.x+ro1[i][0]>0&&cur.x+ro1[i][0]<=r&&cur.y+ro1[i][1]>0&&cur.y+ro1[i][1]<=c; } void print_ans(Node a){ stack<Node> Nodes; Nodes.push(Node(x_sta,y_sta,u,f)); while(1){ Nodes.push(a); if(!vis[a.x][a.y][a.u][a.f]) break; a=state[a.x][a.y][a.u][a.f]; } int cnt=0; while(1){ if(cnt%9==0) printf("\n "); printf("(%d,%d)",Nodes.top().x,Nodes.top().y); Nodes.pop(); if(!Nodes.empty()) printf(","); else{ printf("\n"); break; } cnt++; } return; } void solve(){ printf("%s",s); memset(vis,-1,sizeof(vis)); memset(state,0,sizeof(state)); queue<Node> q; Node sta(x_sta,y_sta,u,f); vis[x_sta][y_sta][u][f]=0; q.push(sta); while(!q.empty()){ Node a=q.front(); q.pop(); for(int i=0;i<4;++i){ if(!match(a,i)||!in(a,i)) continue; Node b=rotate(a,i); if(b.x==x_sta&&b.y==y_sta){ print_ans(a); return; } if(vis[b.x][b.y][b.u][b.f]<0){ vis[b.x][b.y][b.u][b.f]=vis[a.x][a.y][a.u][a.f]+1; state[b.x][b.y][b.u][b.f]=a; q.push(b); } } } printf("\n No Solution Possible\n"); return; } int main(){ while(read()){ solve(); } return 0; }
|