HDU 1728 逃离迷宫 BFS

66 篇文章 0 订阅
49 篇文章 0 订阅

看题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1728

这题的x1,x2对应列,y1, y2对应行 。。很邪恶

还要注意下标应该从1开始。。。。一时傻了调了半天。

还有前后坐标一样的时候。。。。贡献一次WA

同样的还是BFS的应用。

只不过是用一个step来标记目前转过的弯,每条路一路走到底。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=100+10;
int n,m,k;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
const int dx[]={0,1,0,-1};
const int dy[]={-1,0,1,0};

struct site
{
	int x,y;
	int step;
	site(int i=-1,int j=-1,int s=-1){x=i;y=j;step=s;}
}start,en;

bool bfs()
{
	if(start.x==en.x && start.y==en.y)
		return true;

	memset(vis,0,sizeof(vis));

	vis[start.x][start.y]=true;
	start.step=-1;

	queue<site> q;
	q.push(start);

	while(!q.empty())
	{
		site temp=q.front();
		q.pop();

		for(int i=0;i<4;i++)
		{
			int nx=temp.x+dx[i];
			int ny=temp.y+dy[i];
			while(nx >=1 && ny >=1 && nx<=m && ny<=n && maze[nx][ny]=='.')
			{
				if(vis[nx][ny]==false)
				{
					q.push(site(nx,ny,temp.step+1));
					vis[nx][ny]=true;
					if(nx==en.x && ny==en.y && temp.step+1<=k)				
						return true;

				}
				nx=nx+dx[i];
				ny=ny+dy[i];
			}
		}	
	}
	return false;
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		for(int i=1;i<=m;i++)
			scanf("%s",maze[i]+1);

		scanf("%d%d%d%d%d",&k,&start.y,&start.x,&en.y,&en.x);

		if(bfs())
			printf("yes\n");
		else 
			printf("no\n");

	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值