A Dicey Problem (Uva 810 & poj 1872 bfs)
2015-08-19 16:43
330 查看
Language: Default A Dicey Problem
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 Source World Finals 1999 |
思路:骰子两个面就可以确定状态,开一个四维数组标记状态。注意以下代码死活在poj交C++过不了,G++可过,Uva也可过。写得很难看。。
代码:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define mod 1000000009 const int maxn = 1005; const int MAXN = 2005; const int MAXM = 1000010; const int N = 1005; int Right[7][7]; char str[15]; int mp[25][25]; int n,m,sx,sy,up,fron,cnt; int dir[4][2]={1,0,0,1,-1,0,0,-1}; bool vis[7][7][25][25]; struct Node { int x,y; int up,fro; int step,pre; }que[MAXM]; bool isok(int x,int y) { return x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!=0; } void out(int x,int sum) { if (que[x].pre==-1) { printf(" (%d,%d),",que[x].x,que[x].y); cnt++; return ; } out(que[x].pre,sum); if (cnt%9==0) printf(" "); printf("(%d,%d)",que[x].x,que[x].y); cnt++; if (cnt!=sum&&cnt%9!=0) printf(","); else { if (cnt!=sum) printf(","); printf("\n"); } } bool bfs() { cnt=0; int front=0,rear=0; Node st,now; memset(vis,false,sizeof(vis)); st.x=sx,st.y=sy,st.up=up,st.fro=fron,st.step=0,st.pre=-1; que[rear++]=st; while (front<rear) { st=que[front]; if (st.step>0&&st.x==sx&&st.y==sy) { out(front,st.step+1); return true; } for (int i=0;i<4;i++) { now.x=st.x+dir[i][0]; now.y=st.y+dir[i][1]; if (isok(now.x,now.y)) { if (i==0&&(mp[now.x][now.y]==-1||mp[now.x][now.y]==st.up)) { now.fro=st.up;now.up=7-st.fro; if (vis[now.up][now.fro][now.x][now.y]) continue; now.step=st.step+1; vis[now.up][now.fro][now.x][now.y]=true; now.pre=front; que[rear++]=now; } if (i==1&&(mp[now.x][now.y]==-1||mp[now.x][now.y]==st.up)) { now.fro=st.fro;now.up=7-Right[st.up][st.fro]; if (vis[now.up][now.fro][now.x][now.y]) continue; now.step=st.step+1; vis[now.up][now.fro][now.x][now.y]=true; now.pre=front; que[rear++]=now; } if (i==2&&(mp[now.x][now.y]==-1||mp[now.x][now.y]==st.up)) { now.fro=7-st.up;now.up=st.fro; if (vis[now.up][now.fro][now.x][now.y]) continue; now.step=st.step+1; vis[now.up][now.fro][now.x][now.y]=true; now.pre=front; que[rear++]=now; } if (i==3&&(mp[now.x][now.y]==-1||mp[now.x][now.y]==st.up)) { now.fro=st.fro;now.up=Right[st.up][st.fro]; if (vis[now.up][now.fro][now.x][now.y]) continue; now.step=st.step+1; vis[now.up][now.fro][now.x][now.y]=true; now.pre=front; que[rear++]=now; } } } front++; } return false; } int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin); #endif Right[1][2]=3;Right[1][3]=5;Right[1][4]=2;Right[1][5]=4; Right[2][1]=4;Right[2][3]=1;Right[2][6]=3;Right[2][4]=6; Right[3][1]=2;Right[3][5]=1;Right[3][6]=5;Right[3][2]=6; Right[4][1]=5;Right[4][2]=1;Right[4][6]=2;Right[4][5]=6; Right[5][1]=3;Right[5][4]=1;Right[5][6]=4;Right[5][3]=6; Right[6][2]=4;Right[6][3]=2;Right[6][5]=3;Right[6][4]=5; int i,j; while (scanf("%s",str)) { if (strcmp(str,"END")==0) break; scanf("%d %d %d %d %d %d",&n,&m,&sx,&sy,&up,&fron); for (i=1;i<=n;i++) for (j=1;j<=m;j++) scanf("%d",&mp[i][j]); printf("%s\n",str); if (!bfs()) printf(" No Solution Possible\n"); } return 0; }
相关文章推荐
- 阅读方法
- 线段树(求矩形并,交,并减交的面积)
- UML类图实例
- 【Java学习笔记】向量类的使用
- 黑马程序员---java基础之集合Set
- Android笔记(二)LogCat
- 大约Android 了解权限管理
- Games:取石子游戏(POJ 1067)
- NSDictionary的几种遍历方法
- javascript DOM 节点 第18节
- 数据结构之链表C语言实现以及使用场景分析
- poj1001
- 希尔排序
- 14Lync2013升级到SkypeForBusiness2015--移除Lync2013前后端服务器
- Introduction to Java Programming编程题5.13<数列求和>
- 分享:Android系统的常用权限整理
- MySQL零碎知识整理
- centos6.5下安装mysql,远程访问
- 14. CSS 列表
- Linux进程间通信 共享内存+信号量+简单例子