hdu1728逃离迷宫(bfs,拐弯问题)

逃离迷宫

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26150    Accepted Submission(s): 6357

Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?  

Input   第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,

  第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符’.’表示该位置为空地,字符’*’表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x
1, y
1, x
2, y
2 (1 ≤ k ≤ 10, 1 ≤ x
1, x
2 ≤ n, 1 ≤ y
1, y
2 ≤ m),其中k表示gloria最多能转的弯数,(x
1, y
1), (x
2, y
2)表示两个位置,其中x
1,x
2对应列,y
1, y
2对应行。

 

Output   每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。  

Sample Input

2 5 5 …** *.**. ….. ….. *…. 1 1 1 1 3 5 5 …** *.**. ….. ….. *…. 2 1 1 1 3  

Sample Output

no yes  

Source
“网新恩普杯”杭州电子科技大学程序设计邀请赛

很nb的一道题了

让我对bfs也有了新的理解。

我一直以为优先队列+bfs就把所有情况都处理了 所以一直写模版。

对于这道题而言,题意很容易懂。

需要注意的点就是:

  • 题目中的x1,x2是列,y1,y2是行。
  • 如何解决拐弯。其实也很简单定义一个方向的数组.dir[4][2]={1,0,-1,0,0,1,0,-1}。这样我们就能根据i来判断方向了。如果当前i和上一步的i不一致即可判断发生了拐弯。
  • 这一点是我也没有考虑到的。通常我们用的bfs入了优先队列肯定是当前花费代价的最少,所以也就能标记当前位置已经走过。而在这道题某点入了队列 ,不能简单的就标记该位置已经走过,因为还有个方向。如果走向该位置的方向不同,可以认为当前位置并没有走完。即用vis[x][y][dir]来记录走过的点。

下面就看代码

#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int m,n,k;
int st_x,st_y,ed_x,ed_y;
char map[105][105];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node
{
	int x,y;
	int cost;
	int dir;
	friend bool operator<(node a,node b)
	{
		return a.cost>b.cost;
	}
};
bool limit(int x,int y)
{
	if(x<0||y<0||x>=m||y>=n||map[x][y]=='*')
		return false;
	return true;
}
bool bfs()
{
	node node1;
	node1.x=st_x;
	node1.y=st_y;
	node1.cost=0;
	node1.dir=1;
	bool vis[105][105][4];
	memset(vis,false,sizeof(vis));
	priority_queue<node>s;
	s.push(node1);
	bool first=true;
	while(!s.empty())
	{
		node1=s.top();s.pop();
		if(node1.cost>k) break;//次序很重要  
		if(vis[node1.x][node1.y][node1.dir]) continue;
		vis[node1.x][node1.y][node1.dir]=true;
		if(node1.x==ed_x&&node1.y==ed_y) return true;
		for(int i=0;i<4;i++)
		{	
			int cur_x=node1.x+dir[i][0];
			int cur_y=node1.y+dir[i][1];
			if(!vis[cur_x][cur_y][i]&&limit(cur_x,cur_y))
			{
				node newnode;
				newnode.x=cur_x;newnode.y=cur_y;
				newnode.dir=i;
				//如果不是起点。 
				if(!(node1.x==st_x&&node1.y==st_y))
				{
					//如果当前方向和上次方向不同 转弯+1 
					if(i!=node1.dir) 
					newnode.cost=node1.cost+1;
					else
					newnode.cost=node1.cost;
				}
				else
				{
					newnode.cost=0;
				} 
				s.push(newnode);
			}
		}
		if(first)
		{
			for(int i=0;i<4;i++)
			vis[st_x][st_y][i]=true;
			first=false;
		}
	}
	return false;
}
int main()
{
	int t=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&m,&n);
		for(int i=0;i<m;i++)
		{
			scanf("%s",map[i]);
		}
		scanf("%d %d %d %d %d",&k,&st_y,&st_x,&ed_y,&ed_x);
		st_x--;
		st_y--;
		ed_x--;
		ed_y--;
		if(bfs())
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
} 

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/su20145104009/article/details/70144984
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞