0分求调
查看原帖
0分求调
1368616
CNCCCNCC楼主2024/9/5 21:06
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PLL;
queue<PLL>q; 
int fx1[10]={0,0,1,0,-1},n,m,d[501][501];
int fx2[10]={0,1,0,-1,0};
void bfs(){
	

	d[1][1]=0;
	while(q.size()!=0){
		PLL t=q.front();
		q.pop();
		for(int i=1;i<=4;i++){
			int a=t.first+fx1[i],b=t.second+fx2[i];
		
			if(a>=1&&b>=1&&a<=n&&b<=m&&d[a][b]==0){
				q.push({a,b});
				d[a][b]=d[t.first][t.second]+1;
			}
		}
	} 
} 
int main(){
	cin>>n>>m;
	int a1,b1;
	cin>>a1>>b1;
	for(int i=1;i<=a1;i++){
		int x,y;
		cin>>x>>y;
		q.push({x,y});
	}
	bfs();
	for(int i=1;i<=b1;i++){
		int x,y;
		cin>>x>>y;
		cout<<d[x][y]<<endl;
	}
	return 0;
}
2024/9/5 21:06
加载中...