蓝桥公费选拔 T214627 Cm4k3r返屋企

利用广度搜素
要注意题目给的样例数字是连在一起的,说明不能用二维整形存储
要二维字符串数组存储。
既然是字符串数组,那判断的时候就是要判断字符,不是数字。
另外,字符数组是从(0,0)开始算的,题目给的坐标是1,1开始算,所以可以把题目给的坐标-1,再存放在队列中

#include<bits/stdc++.h>
#include<queue>
using namespace std;


char a[1000][1000];
int vis[1000][1000];
int startx,starty,endx,endy;

int dx[] = {0,0,1,0,-1};
int dy[] = {0,1,0,-1,0};

struct weizhi
{
	int x;
	int y;
	int step;
}start,tmp,temp;

queue<weizhi> q;

int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		scanf("%s",a[i]);
	cin>>startx>>starty>>endx>>endy;
	startx -=1;
	starty -=1;
	endx -= 1;
	endy -= 1;
	start.x = startx;
	start.y = starty;
	start.step = 0;
	q.push(start); 
	vis[startx][starty] = 1;
	while(!q.empty())
	{
		tmp=q.front();
		if(tmp.x == endx && tmp.y == endy)
		{
			cout<<q.front().step;
			break;
		}
		for(int i=1;i<=4;i++)
		{
			int xx = tmp.x+dx[i];
			int yy = tmp.y+dy[i];
			if(a[xx][yy]== '0' && vis[xx][yy]==0 && xx>=0 && xx<n && yy>=0 && yy<n)
			{
				vis[xx][yy] = 1;
				temp.x = xx;
				temp.y = yy;
				temp.step = q.front().step+1;
				q.push(temp);
			}
		}
		q.pop();	
	}	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值