HDU 1728 逃离迷宫(BFS)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

跟上一篇文章一样,但是这次转弯次数是给出的,问是否可以到达终点。

坑点1:

5 5
.....
.*.*.
.....
.*.*.
.....
1 1 1 4 3
5 5
.....
.*.*.
.....
.*.*.
.....
1 1 1 3 4
坑点2:

终点和起点坐标一样所以直接输出yes???


代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
char G[maxn][maxn];
int dir[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int sx, sy, ex, ey, n, m, k, cnt[maxn][maxn];
bool vst[maxn][maxn];

struct node
{
	int step;
	int x, y;
};

queue <node> q;

bool Check(int x, int y, int step)
{
	if(x > 0 && x <= m && y > 0 && y <= n && G[x][y] != '*' && ((!vst[x][y]) || (cnt[x][y] >= step)))
		return 1;
	return 0;
}

void add(int x, int y, int d, int step)
{
	node tmp;
	x += dir[d][0];
	y += dir[d][1];
	while(Check(x, y, step))
	{
		vst[x][y] = 1;
		tmp.x = x;
		tmp.y = y;
		tmp.step = step;
		cnt[x][y] = step; 
		q.push(tmp);
		x += dir[d][0];
		y += dir[d][1];
	}
}

bool bfs()
{
	while(!q.empty())
		q.pop();
	
	node now, next;
	vst[sx][sy] = 1;
	for(int i = 0; i < 4; i++)
		add(sx, sy, i, 0);
	while(!q.empty())
	{
		now = q.front();
		q.pop();
		if(now.step > k)
			return 0;
//		cout << now.x << " " << now.y << " " << now.step << endl;
		if(now.x == ex && now.y == ey)
			return 1;
		next.step = now.step + 1;
		for(int i = 0; i < 4; i++)
		{
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			if(Check(next.x, next.y, next.step))
				add(now.x, now.y, i, next.step);
		}
	}
	return 0;
}

int main()
{
//	ios::sync_with_stdio(false); 
	int T;
	cin >> T;
	while(T--)
	{
		memset(vst, 0, sizeof(vst));
		memset(cnt, 0, sizeof(cnt));
		scanf("%d%d", &m, &n);
		for(int i = 1; i <= m; i++)
			scanf("%s", &G[i][1]);
			
//		for(int i = 1; i <= m; i++)
//		{
//			for(int j = 1; j <= n; j++)
//				cout << G[i][j];
//			cout << endl;
//		}
		scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
		if(sx == ex && sy == ey)
			printf("yes\n");
		else if(bfs())
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
} 



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值