A Dicey Problem
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 762 |
|
Accepted: 264 |
Description
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, but those with a solution are guaranteed to have only one unique solution. For each input maze, either a solution or a message indicating no solution is possible will be sent to the output.
Input
The input file begins with a line containing a string of no more than 20 non-blank characters that names the first maze. The next line contains six integers delimited by single spaces. These integers are, in order, the number of rows in the maze (an integer from 1 to 10, call this value R), the number of columns in the maze (an integer from 1 to 10, call this value C), the starting row, the starting column, the number that should be on top of the die at the starting position, and finally the number that should be facing you on the die at the starting position. The next R lines contain C integers each, again delimited by single spaces. This R * C array of integers defines the maze. A value of zero indicates an empty location in the maze (such as the two empty squares in the center column of the maze in Figure 3), and a value of -1 indicates a wild square. This input sequence is repeated for each maze in the input. An input line containing only the word "END" (without the quotes) as the name of the maze marks the end of the input.
Output
The output should contain the name of each maze followed by its solution or the string "No Solution Possible" (without the quotes). All lines in the output file except for the maze names should be indented exactly two spaces. Maze names should start in the leftmost column. Solutions should be output as a comma-delimited sequence of the consecutive positions traversed in the solution, starting and ending with the same square (the starting square as specified in the input). Positions should be specified as ordered pairs enclosed in parentheses. The solution should list 9 positions per line (with the exception of the last line of the solution for which there may not be a full 9 positions to list), and no spaces should be present within or between positions.
Sample Input
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
Sample Output
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
题意:滚动骰子 使之从出发点出发后滚一圈后再回到原点
思路:将骰子的状态存下来后 就很好用bfs了 也很好判重了 不多解释了
感想:这题WA了我一天 最后再小峰峰的带领下才知道是哪里错了 感谢小峰峰
ps:这题还是1999年world final 哦 虽然有点水 不过还是在world final 上签到了喔 感觉蛮不错
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define maxn 15
using namespace std;
int n,m,ans;
int sx,sy,top,face;
char s[30];
int mp[maxn][maxn];
int vis[maxn][maxn][7][7]; // x y u f *开始数组又开小了 我XX
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int leri[7][7][2]; // 根据 前 上 推左右数组
int ansnum,num,cnt;
struct die
{
int x,y;
int pre; // 存前一个骰子的下标 方便递归输出
int u,d,l,r,f,t; // 上 下 左 右 前 后
} cur,now,q[10006];
bool isok(int xx,int yy,int uu,int ff,int dd)
{
int i,j,x1,y1;
x1=xx+dx[dd];
y1=yy+dy[dd];
if((mp[x1][y1]==-1||mp[x1][y1]==uu)&&!vis[x1][y1][cur.f][cur.u]) return true ;
return false ;
}
void getcur(int dd) // 模拟骰子转动
{
int i,j;
cur=now;
cur.x=now.x+dx[dd];
cur.y=now.y+dy[dd];
cur.pre=num;
switch(dd)
{
case 0:
cur.u=now.f;
cur.d=now.t;
cur.f=now.d;
cur.t=now.u;
break ;
case 1:
cur.u=now.t;
cur.d=now.f;
cur.f=now.u;
cur.t=now.d;
break ;
case 2:
cur.u=now.r;
cur.d=now.l;
cur.l=now.u;
cur.r=now.d;
break ;
case 3:
cur.u=now.l;
cur.d=now.r;
cur.l=now.d;
cur.r=now.u;
break ;
}
}
bool bfs()
{
int i,j,xxc=-1;
int head=0,tail=-1;
int nu,nd,nl,nr,nf,nt,nx,ny;
memset(vis,0,sizeof(vis));
cur.x=sx;
cur.y=sy;
cur.pre=-1;
cur.u=top;
cur.f=face;
cur.d=7-top;
cur.t=7-face;
cur.l=leri[face][top][0];
cur.r=leri[face][top][1];
// vis[sx][sy][face][top]=1; // 我 X…X WA了一天 原来是这儿错了
q[++tail]=cur; // 这里一定不能标记 因为最后是要回到原点的
while(head<=tail)
{
xxc++;
now=q[head];
num=head;
head++;
nx=now.x;
ny=now.y;
nu=now.u;
nf=now.f;
// printf("x:%d y:%d u:%d d:%d l:%d r:%d f:%d t:%d\n",nx,ny,nu,now.d,now.l,now.r,nf,now.t);
if(xxc&&nx==sx&&ny==sy)
{
ansnum=num;
return true ;
}
for(i=0; i<4; i++)
{
getcur(i);
if(isok(nx,ny,nu,nf,i))
{
vis[nx+dx[i]][ny+dy[i]][cur.f][cur.u]=1;
q[++tail]=cur;
}
}
}
return false ;
}
void output(int k) // 递归输出答案
{
int i,j;
if(k==-1) return ;
else output(q[k].pre);
if(cnt%9==0) printf(" ");
if(cnt%9==8)
{
if(k!=ansnum) printf("(%d,%d),\n",q[k].x,q[k].y);
else printf("(%d,%d)\n",q[k].x,q[k].y);
}
else
{
if(k!=ansnum) printf("(%d,%d),",q[k].x,q[k].y);
else printf("(%d,%d)\n",q[k].x,q[k].y);
}
cnt++;
}
int main()
{
int i,j;
memset(leri,0,sizeof(leri));
leri[1][2][0]=3; // 将leri打表 能快速根据前、上求出左右
leri[1][3][0]=5;
leri[1][4][0]=2;
leri[1][5][0]=4;
leri[2][1][0]=4;
leri[2][3][0]=1;
leri[2][4][0]=6;
leri[2][6][0]=3;
leri[3][1][0]=2;
leri[3][2][0]=6;
leri[3][5][0]=1;
leri[3][6][0]=5;
leri[4][1][0]=5;
leri[4][2][0]=1;
leri[4][5][0]=6;
leri[4][6][0]=2;
leri[5][1][0]=3;
leri[5][3][0]=6;
leri[5][4][0]=1;
leri[5][6][0]=4;
leri[6][2][0]=4;
leri[6][3][0]=2;
leri[6][4][0]=5;
leri[6][5][0]=3;
for(i=1; i<=6; i++)
{
for(j=1; j<=6; j++)
{
if(leri[i][j][0]) leri[i][j][1]=7-leri[i][j][0];
}
}
while(scanf("%s",s))
{
if(strcmp(s,"END")==0) break ;
scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&top,&face);
memset(mp,0,sizeof(mp));
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&mp[i][j]);
}
}
printf("%s\n",s);
if(bfs())
{
cnt=0;
output(ansnum);
}
else printf(" No Solution Possible\n");
}
return 0;
}