标签:style blog http io color os ar for sp
The three-by-three array in Figure 1 is a maze. A standard six-sided die is needed to traverse the maze (the layout of a standard six--sided die is shown in Figure 2). Each maze has an initial position and an initial die configuration. In Figure 1, the starting position is row 1, column 2--the ``2‘‘ in the top row of the maze--and the initial die configuration has the ``5‘‘ on top of the die and the ``1‘‘ facing the player (assume the player is viewing the maze from the bottom edge of the figure).
To move through the maze you must tip the die over on an edge to land on an adjacent square, effecting horizontal or vertical movement from one square to another. However, you can only move onto a square that contains the same number as the number displayed
on the top of the die before the move, or onto a ``wild‘‘ square which contains a star. Movement onto a wild square is always allowed regardless of the number currently displayed on the top of the die. The goal of the maze is to move the die off the starting
square and to then find a way back to that same square.
For example, at the beginning of the maze there are two possible moves. Since the 5 is on top of the die, it is possible to move down one square, and since the square to the left of the starting position is wild it is also possible to move left. If the first
move chosen is to move down, this brings the 6 to the top of the die and moves are now possible both to the right and down. If the first move chosen is instead to the left, this brings the 3 to the top of the die and no further moves are possible.
If we consider maze locations as ordered pairs of row and column numbers ( row, column) with row indexes starting at 1 for the top row and increasing
toward the bottom, and column indexes starting at 1 for the left column and increasing to the right, the solution to this simple example maze can be specified as: (1,2), (2,2), (2,3), (3,3), (3,2), (3,1), (2,1), (1,1), (1,2). A bit more challenging example
maze is shown in Figure 3.
The goal of this problem is to write a program to solve dice mazes. The input file will contain several mazes for which the program should search for solutions. Each maze will have either a unique solution or no solution at all. That is, each maze in the input
may or may not have a solution. For each input maze, either a solution or a message indicating no solution is possible will be sent to the output.
DICEMAZE1 3 3 1 2 5 1 -1 2 4 5 5 6 6 -1 -1 DICEMAZE2 4 7 2 6 3 6 6 4 6 0 2 6 4 1 2 -1 5 3 6 1 5 3 4 5 6 4 2 4 1 2 0 3 -1 6 DICEMAZE3 3 3 1 1 2 4 2 2 3 4 5 6 -1 -1 -1 END
DICEMAZE1 (1,2),(2,2),(2,3),(3,3),(3,2),(3,1),(2,1),(1,1),(1,2) DICEMAZE2 (2,6),(2,5),(2,4),(2,3),(2,2),(3,2),(4,2),(4,1),(3,1), (2,1),(2,2),(2,3),(2,4),(2,5),(1,5),(1,6),(1,7),(2,7), (3,7),(4,7),(4,6),(3,6),(2,6) DICEMAZE3 No Solution Possible
题意:给一个地图,R*C的,地图上每个点有相应的数字,如果骰子旁边的位置上的数字恰好等于当前骰子状态的上表面的点数或者旁边是空地,则骰子可以滚动到该位置。给你初始骰子的位置以及初始骰子的状态,求能否找到一条路径使骰子走一圈后又回到起点。凡神的输出方案好:点击打开链接
<pre name="code" class="cpp">#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) int mp[15][15]; int to[15][15]; int dr[][2]={-1,0,1,0,0,-1,0,1}; int vis[15][15][15][15]; int r,c,sx,sy; int u,f; typedef pair<int,int>pil; vector<pil>ans; struct node{ int x,y,u,f; int pre; }q[10005]; void solve(int &vu, int &vf, int d) { int t; if (d == 0) {t = vf; vf = 7 - vu; vu = t;} if (d == 1) {t= vu; vu = 7 - vf; vf = t;} if (d == 2) vu = 7 - to[vu][vf]; if (d == 3) vu = to[vu][vf]; } void print(int x) { if(x==-1) return ; print(q[x].pre); ans.push_back(make_pair(q[x].x,q[x].y)); } void BFS() { node st,ed; ans.clear(); int head=0,rear=0; st.x=sx;st.y=sy;st.u=u; st.f=f;st.pre=-1; CLEAR(vis,0); q[rear++]=st; while(head<rear) { st=q[head++]; for(int i=0;i<4;i++) { int xx=st.x+dr[i][0]; int yy=st.y+dr[i][1]; if(xx<0||xx>r||yy<0&&yy>c) continue; if(mp[xx][yy]!=-1&&st.u!=mp[xx][yy]) continue; if(xx==sx&&yy==sy) { print(head-1); ans.push_back(make_pair(sx,sy)); int s=ans.size(); for (int i = 0; i < s; i++) { if (i % 9 == 0) printf("\n "); printf("(%d,%d)%c", ans[i].first, ans[i].second, i == s - 1 ? '\n' : ','); } return ; } ed=st;ed.x=xx;ed.y=yy; solve(ed.u,ed.f,i); if(vis[xx][yy][ed.u][ed.f]) continue; vis[xx][yy][ed.u][ed.f]=1; ed.pre=head-1; q[rear++]=ed; } } printf("\n No Solution Possible\n"); } int main() { char str[20]; to[1][2] = 4; to[1][3] = 2; to[1][4] = 5; to[1][5] = 3; to[2][1] = 3; to[2][3] = 6; to[2][4] = 1; to[2][6] = 4; to[3][1] = 5; to[3][2] = 1; to[3][5] = 6; to[3][6] = 2; to[4][1] = 2; to[4][2] = 6; to[4][5] = 1; to[4][6] = 5; to[5][1] = 4; to[5][3] = 1; to[5][4] = 6; to[5][6] = 3; to[6][2] = 3; to[6][3] = 5; to[6][4] = 2; to[6][5] = 4; while(~scanf("%s",str)&&strcmp(str,"END")) { CLEAR(mp,0); scanf("%d%d%d%d%d%d",&r,&c,&sx,&sy,&u,&f); REPF(i,1,r) { REPF(j,1,c) scanf("%d",&mp[i][j]); } printf("%s",str); BFS(); } return 0; }
标签:style blog http io color os ar for sp
原文地址:http://blog.csdn.net/u013582254/article/details/40620537