Basic Wall Maze POJ - 2935简单bfs

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();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljw0925

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值