bfs90求调
查看原帖
bfs90求调
1054021
114_Hzh_AK_NOIP_514楼主2024/6/30 22:12
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,step;
}t,u;
queue <node> q;
int n,m;
char mapp[100][100];
bool book[100][100];
int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void bfs(int x,int y)
{
	book[x][y]=1;
	t.x=x;
	t.y=y;
	t.step=0;
	q.push(t);
	while(!q.empty())
	{
		t=q.front();
		q.pop();
		if(mapp[t.x][t.y]=='B')
		{
			cout << t.step-1;
			return;
		}
		for(int i=0;i<4;i++)
		{
			int nx=t.x+d[i][0];
			int ny=t.y+d[i][1];
			if(nx>10 || nx<1 || ny>10 || ny<1) continue;
			if(book[nx][ny]==0 && mapp[nx][ny]!='R')
			{
				u.x=nx;
				u.y=ny;
				u.step=t.step+1;
				q.push(u);
			}
		}
	}
}
int main()
{
	for(int i=1;i<=10;i++)
	{
		for(int j=1;j<=10;j++)
		{
			cin >> mapp[i][j];
			if(mapp[i][j]=='L') n=i,m=j;
		}
	}
	bfs(n,m);
	return 0;
}
2024/6/30 22:12
加载中...