ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Basic Wall Maze POJ - 2935简单bfs

2021-01-24 14:59:00  阅读:180  来源: 互联网

标签:pre end sta Wall mo 2935 int Basic include


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 6
2 6
0 0 1 0
1 5 1 6
1 5 3 5
0 0
Sample Output
NEEESWW
题目在bfs板子的基础上加了三面墙上去,注意处理一下就好了

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
const int inf=0x3f3f3f3f;
#define ll long long
#define ull unsigned long long
#define mm(w,v) memset(w,v,sizeof(w))
#define f(x,y,z) for(int x=(y),_=(z);x<_;++x)
const int modn=1e9+7;
const int MAXN=1000000;
using namespace std;
struct node {
	int x,y;
};
int mo[7][7][4];
node pre[7][7];
int ope[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//下,上,右,左 
int stx,sty,edx,edy;
queue<node> que;
stack<char> sta;
void bfs() {
	node a;
	a.x=stx;
	a.y=sty;
	pre[stx][sty]=(node){-2,-2};
	que.push(a);
	while(que.size()) {
		node tp=que.front();
		que.pop();
		if(tp.x==edx&&tp.y==edy) {
			break;
		}
		else {
			f(i,0,4) {
				if(mo[tp.x][tp.y][i]) {
					int dx=tp.x+ope[i][0];
					int dy=tp.y+ope[i][1];
					if(pre[dx][dy].x==-1) {
						que.push((node){dx,dy});
						pre[dx][dy]=tp;
					}
				}
			}
		}
	}
//	f(i,0,6) {
//		f(j,0,6) {
//			printf("(%d,%d) ",pre[i][j].x,pre[i][j].y);
//		}
//		printf("\n");
//	}
	node st=(node) {edx,edy};
	while(st.x!=-1&&st.x!=-2) {
		int nx=st.x;
		int ny=st.y;
		int px=pre[nx][ny].x;
		int py=pre[nx][ny].y;
		if(nx==px) {
			if(ny>py) {
				sta.push('E');
			}
			else {
				sta.push('W');
			}
		}
		else {
			if(nx>px) {
				sta.push('S');
			}
			else {
				sta.push('N');
			}
		}
		st=pre[nx][ny];
	}
	sta.pop();
	while(sta.size()) {
		printf("%c",sta.top());
		sta.pop();
	}
	printf("\n");
}
void log() {
	char mv[4][3]={"下","上","右","左"}; 
	f(i,0,6) {
		f(j,0,6) {
			printf("(");
			f(k,0,4) {
				if(mo[i][j][k])
					printf("%s,",mv[k]);
				else {
					printf("  ,");
				}
			}
			printf(") ");
		}
		printf("\n");
	}
}
void solve() {
	while(que.size()) {
		que.pop();
	}
	while(sta.size()) {
		sta.pop();
	}
	f(i,0,6) {
		f(j,0,6) {
			pre[i][j]=(node){-1,-1};
			f(k,0,4) {
				mo[i][j][k]=1;//可以到 
			}
		}
	} 
	f(i,0,6) {
		mo[0][i][1]=0;
		mo[5][i][0]=0;
		mo[i][0][3]=0;
		mo[i][5][2]=0;
	}
	f(i,0,3) {
		int wsx,wsy,wex,wey;
		scanf("%d%d%d%d",&wsx,&wsy,&wex,&wey);
		if(wsx==wex) {//竖墙 
			if(wsx-1>=0)  {
				f(i,wsy,wey) {
					mo[i][wsx-1][2]=0;
					mo[i][wsx][3]=0;
				}
			}
		}else {//横墙 
			if(wsy-1>=0) {
				f(i,wsx,wex) {
					mo[wsy-1][i][0]=0;
					mo[wsy][i][1]=0;
				}
			}		
		}
	}
//	log();
	bfs();
}
int main(void) {
	while(scanf("%d%d",&sty,&stx)&&(sty!=0&&stx!=0)) {
		scanf("%d%d",&edy,&edx);
		stx--;
		sty--;
		edx--;
		edy--;
		solve();
	}
}


标签:pre,end,sta,Wall,mo,2935,int,Basic,include
来源: https://blog.csdn.net/ljw0925/article/details/113090512

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有