WHAT WHY 啊~~~~
查看原帖
WHAT WHY 啊~~~~
975630
woyaoAKIOI0924PTY楼主2024/11/28 20:14
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=25;
struct Node{
	int x,y,step;
};
char ss[N][N];
int n,dx[]={1,0,-1,0},dy[]={0,1,0,-1};
bool vis[N][N];
int sx,sy,tx,ty;
bool check(int x,int y)
{
	if(x<1||x>n||y<1||y>n)
		return 1;
	if(ss[x][y]=='#'||vis[x][y])
		return 1;
	return 0;
}
int bfs()
{
	queue<Node>q;
	vis[sx][sy]=1;
	q.push({sx,sy,0});
	while(q.size())
	{
		Node t=q.front();
		q.pop();
		if(t.x==tx&&t.y==ty)
			return t.step;
		for(int i=0;i<4;++i)
		{
			int px=t.x+dx[i],py=t.y+dy[i];
			if(check(px,py))	
				continue;
			vis[px][py]=true;
			q.push({px,py,t.step+1}); 
		}
	}
	return -1;
}
signed main()
{
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=n;++j)
		{
			cin>>ss[i][j];
			vis[i][j]=0;	 
		}
	}
	cout<<bfs()<<"\n";
	return 0;
}

2024/11/28 20:14
加载中...