【YBTOJ】走迷宫

34 篇文章 0 订阅
18 篇文章 0 订阅

在这里插入图片描述

思路:

这道题是广搜模板题

c o d e code code

#include<iostream>
#include<cstdio>
using namespace std;
int n;
int sx, sy, tx, ty;
int a[2000][2000];
int f[2000000][3];
int dx[4]={1, 0, -1, 0};
int dy[4]={0, 1, 0, -1};
bool v[2000][2000];
void bfs()
{
	f[1][1]=sx;
	f[1][2]=sy;
	f[1][0]=0;
	v[sx][sy]=1;
	int hd=0, tl=1;
	while(hd<tl)
	{
		hd++;
		for(int i=0; i<4; i++)
		{
			int xx=f[hd][1]+dx[i],
			    yy=f[hd][2]+dy[i];
			if(a[xx][yy]==1||xx<1||xx>n||yy<1||yy>n||v[xx][yy]==1)
				continue;
			tl++;
			f[tl][1]=xx;
			f[tl][2]=yy;
			f[tl][0]=f[hd][0]+1;
			v[xx][yy]=1;
			if(xx==tx&&yy==ty)
			{
				printf("%d", f[tl][0]);
				return;
			}
		}
	}
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			scanf("%1d", &a[i][j]);
	scanf("%d%d%d%d", &sx,&sy,&tx,&ty);
	bfs();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值