hdu 1728 逃离迷宫

题目链接:

题目大意:迷宫之类的题大多数都能使用BFS或者DFS吧,不过这道题比较多陷阱和技巧,首先,每一种走法经过的弯可能是不同的,虽然都是经过同一个点,但是到达这个点时经过的弯路可能就不同了。应该有点DP的味道吧,BFS+DP可以算出来。这需要开多一个二维数组来保存这个点最小的转弯数,到达终点后判断这个点的转弯数是否小于或等于给予的限定就可以了。这应该是属于UCS查找吧,是BFS的升级版,就是加了个进队列的条件。

代码:

#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<queue>
using namespace std;
#define INF 100000000
const int maxn=110;
int q[4][2]={-1,0,1,0,0,-1,0,1};
bool flag[maxn][maxn];
int n,m,k,x1,y,x2,y2;
struct node
{
	int x,y,step;
};
char s[maxn][maxn];
int bfs()
{
	int i,j;
	memset(flag,false,sizeof(flag));
	queue<node>Q;
	node S;
	S.x=x1-1;
	S.y=y-1;
	S.step=-1;
	flag[S.x][S.y]=true;
	Q.push(S);
	while(!Q.empty())
	{
		node go,st;
		go=Q.front();
		Q.pop();
		if(go.x==x2-1&&go.y==y2-1&&go.step<=k)
		return 1;
		for(i=0;i<4;i++)
		{
			st.x=q[i][0]+go.x;
			st.y=q[i][1]+go.y;
			st.step=go.step+1;
			if(st.step>k)
				continue;
			while(st.x>=0&&st.x<m&&st.y>=0&&st.y<n&&s[st.x][st.y]=='.')
			{
				if(!flag[st.x][st.y])
				{
					flag[st.x][st.y]=true;
					Q.push(st);
				}
				st.x=st.x+q[i][0];
				st.y=st.y+q[i][1];
				if(st.x==x2-1&&st.y==y2-1&&st.step<=k)
					return 1;
			}
		}
	}
	return 0;
}


int main(void)
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		for(i=0;i<m;i++)
				scanf("%s",s[i]);
		scanf("%d%d%d%d%d",&k,&y,&x1,&y2,&x2);
		if(x1==x2&&y==y2)
		{
			printf("yes\n");
			continue;
		}
		bfs();
		if(bfs()==1)
			printf("yes\n");
		else
			printf("no\n");
	}
	system("Pause");
	return 0;

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值