萌新求助
查看原帖
萌新求助
1026187
lys_99楼主2024/5/23 21:22
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,y,step;
}lz[100009];
queue<node> q;
int mapp[1009][1009];
int vis[1009][1009],n,m,sx,sy,fx,fy,s,a,b;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int main(){
    cin>>n>>m>>a>>b;
    for(int i=1;i<=a;i++){
        cin>>sx>>sy;
        mapp[sx][sy]=0;
        node t;
		t.x=sx;
		t.y=sy;
		t.step=0;
        q.push(t);
    }
	for(int i=1;i<=b;i++){
	    cin>>lz[i].x>>lz[i].y;
	}
	while(!q.empty()){
		node now=q.front(); 
		q.pop();
		for(int i=0;i<=3;i++){
			node New=now;
			New.x=now.x+dx[i];
			New.y=now.y+dy[i];
			New.step=now.step+1;
			if(New.x<=n&&New.x>=1&&New.y<=m&&New.y>=1&&vis[New.x][New.y]!=1){
				q.push(New);			
				vis[New.x][New.y]=1;
				mapp[New.x][New.y]=New.step;
			} 
		} 
	} 
	for(int i=1;i<=b;i++){
		cout<<mapp[lz[i].x][lz[i].y]<<endl;
	}
	return 0;
}//10wa,ε=(´ο`*)))唉
2024/5/23 21:22
加载中...