poj_2935 Basic Wall Maze
来源:互联网 发布:非农数据图 编辑:程序博客网 时间:2024/06/05 04:54
Description
In this problem you have to solve a very simple maze consisting of:
- a 6 by 6 grid of unit squares
- 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
- one start and one end marker
A maze may look like this:
You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is not allowed to leave the grid.
Input
The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third, fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.
You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that the sample input specifies the maze from the picture above.
The last test case is followed by a line containing two zeros.
Output
For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).
There can be more than one shortest path, in this case you can print any of them.
Sample Input
1 62 60 0 1 01 5 1 61 5 3 50 0
Sample Output
NEEESWW
Source
#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>using namespace std;int dirx[5]={0,1,0,-1},diry[5]={1,0,-1,0};int visit[40],g[40][40];;char dir[5]={'N','S','W','E'};struct road{ int p,d;}r[40];void bfs(int s1,int e1,int s2,int e2){ queue<int>q; int dis[40],i; memset(dis,0,sizeof(dis)); for(i=0;i<40;i++) r[i].p=0; while(!q.empty()) q.pop(); int t=s1+(e1-1)*6; int aim=s2+(e2-1)*6; q.push(t); visit[t]=1; int ca=0; while(!q.empty()) {// printf("ca=%d\n",++ca); int reach; int h=q.front(); q.pop(); int hx=h%6; int hy=h/6; if(hx==0) hx=6; else hy++; for(i=0;i<4;i++) { int u=hx+dirx[i]; int v=hy+diry[i]; reach=0; if(u>0 && u<7 && v>0 && v<7) { int t=u+(v-1)*6; if(!visit[t] && !g[h][t]) { visit[t]=1; r[t].p=h; q.push(t); if(dirx[i]==0 && diry[i]==1) r[t].d=1; else if(dirx[i]==0 && diry[i]==-1) r[t].d=0; else if(dirx[i]==1 && diry[i]==0) r[t].d=3; else if(dirx[i]==-1 && diry[i]==0) r[t].d=2; if(aim==t) {reach=1; break;} } } } if(reach) break; } return;}void print(int k){// printf("k=%d en\n",k); if(r[k].p==0) return; print(r[k].p); int u=r[k].d; printf("%c",dir[u]);}int main(){ freopen("in.txt","r",stdin); int i,j,n,m,sx,sy,ex,ey; while(1) { scanf("%d%d",&sx,&sy); if(!sx && !sy) break;// printf("%d %d\n",sx,sy); scanf("%d%d",&ex,&ey); memset(g,0,sizeof(g)); for(i=0;i<3;i++) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==x2 && x1>0 && x1<6) { for(j=y1+1;j<=y2;j++) { int u=(j-1)*6; int k1=u+x1; int k2=u+x1+1; g[k1][k2]=1; g[k2][k1]=1; } } else if(y1==y2 && y1>0 && y1<6) { int u=(y1-1)*6; for(j=x1+1;j<=x2;j++) { int k1=u+j; int k2=u+j+6; g[k1][k2]=1; g[k2][k1]=1; } } } memset(visit,0,sizeof(visit)); bfs(sx,sy,ex,ey); print(ex+(ey-1)*6); printf("\n"); } return 0;}
- poj_2935 Basic Wall Maze
- poj2935 Basic Wall Maze
- poj2935 Basic Wall Maze
- Basic Wall Maze
- POJ-2935-Basic Wall Maze
- POJ:2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- POJ-2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- POJ 2935 Basic Wall Maze
- POJ2935 Basic Wall Maze(BFS)
- POJ-2935 Basic Wall Maze
- poj 2935 Basic Wall Maze
- HDU 1484 Basic wall maze
- POJ2935 Basic Wall Maze bfs+模拟
- POJ 2935 Basic Wall Maze BFS
- poj 2935 Basic Wall Maze bfs
- 一个小小清单,八月前完成-------Day67
- MPI调用C++程序
- POJ-1118-简单暴力题
- Windows Installer XML (WiX) 使用记录 (一)
- bupt暑假排位赛(1)
- poj_2935 Basic Wall Maze
- JspSmartUpload 实现下载
- 《Python基础教程》第20章学习笔记
- 【多级树形菜单-dialog自定义动画弹出方式-手势监听】dialog自定义动画
- Java集合源码学习(1)_架构
- httpd: Could not reliably determine the server's fully qualified domain name
- 【学习笔记】struts-tag与jstl的异同
- 黑马程序员-java方法
- How to Write Good Requirements