Title: Roll The Dice Game, the dice on the top of the number of the same as in the box or the number in the lattice is-1 when you can roll over the lattice, find a way from the beginning to the beginning of the path.
The main topic: simple BFS, the state of the transfer of a little more careful.
The code is as follows;
# include<iostream># include<cstdio># include<map># include<string># include<queue># include<cstring># include<algorithm>using namespace std;struct node{int x,y,s,t; String px,py; Node (int _x,int _y,int _s,int _t,string _px,string _py): X (_x), Y (_y), S (_s), T (_t), PX (_px), py (_py) {} bool operator < (c Onst Node &a) const {return t>a.t; }};int Left[50],right[50],vis[10][10][50],mp[10][10],r,c;int Behind[6]={6,5,4,3,2,1};int d[4][2]={{-1,0},{1,0},{0 , -1},{0,1}};string name;void init () {left[9]=4,right[9]=3; left[12]=3,right[12]=4; left[10]=2,right[10]=5; left[11]=5,right[11]=2; left[15]=3,right[15]=4; left[20]=4,right[20]=3; left[18]=1,right[18]=6; Left[17]=6,right[17]=1; left[22]=5,right[22]=2; left[27]=2,right[27]=5; left[23]=1,right[23]=6; Left[26]=6,right[26]=1; left[29]=2,right[29]=5; left[34]=5,right[34]=2; Left[30]=6,right[30]=1; left[33]=1,right[33]=6; Left[36]=4, right[36]=3; left[41]=3,right[41]=4; left[38]=1,right[38]=6; Left[39]=6,right[39]=1; left[44]=3,right[44]=4; left[47]=4,right[47]=3; left[46]=2,right[46]=5; left[45]=5,right[45]=2;} bool OK (int x,int y) {return x>=0&&x<r&&y>=0&&y<c;} void BFs (int sx,int sy,int ss) {priority_queue<node>q; memset (vis,0,sizeof (VIS)); String Path= ""; Q.push (Node (sx,sy,ss,0,path+ (char) (sx+ ' a '), path+ (char) (sy+ ' a ')); while (!q.empty ()) {Node u=q.top (); Q.pop (); if (u.t&&u.x==sx&&u.y==sy) {int l=u.px.size (); for (int i=0;i<l;++i) {if (i==0) printf (""); printf ("(%d,%d)", u.px[i]-' a ' +1,u.py[i]-' a ' + 1); if (I==L-1) printf ("\ n"); else if (i%9==8) printf (", \ n"); else printf (","); } return; } int Dd[4]={u.s%7,behind[u.s%7-1],right[u.s],left[u.s]}; for (int i=0;i<4;++i) {int nx=u.x+d[i][0],ny=u.y+d[i][1]; if (OK (nx,ny) && (mp[nx][ny]== (U.S/7) | | Mp[nx][ny]==-1)) {int k=u.s%7; if (i==0) k=behind[u.s/7-1]; if (i==1) K=U.S/7; if (!vis[nx][ny][dd[i]*7+k]) {vis[nx][ny][dd[i]*7+k]=1; Q.push (Node (nx,ny,dd[i]*7+k,u.t+1,u.px+ (char) (nx+ ' a '), u.py+ (char) (ny+ ' a ')); }}}} printf ("No solution possible\n");} int main () {//freopen ("UVA-810 A dicey Problem.txt", "R", stdin); int sx,sy,st,sf; Init (); while (Cin>>name) {if (name== "END") is break; scanf ("%d%d%d%d%d%d", &R,&C,&SX,&SY,&ST,&SF); for (int i=0;i<r;++i) for (int j=0;j<c;++j) scanf ("%d", &mp[i][j]); cout<<name<<endl; BFS (SX-1,SY-1,ST*7+SF); } return 0;}
UVA-810 A Dicey Problem (BFS)