实在不懂,为什么全WA
查看原帖
实在不懂,为什么全WA
684890
Rhss楼主2022/12/6 01:59
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> PLL;
const int N = 505;
int n,m,x,y;
int g[N][N];
bool vis[N][N];
vector<PLL> v;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
void bfs()
{
	queue<PLL> q;
	for (int i = 0 ; i < v.size() ; ++i){
		q.push(v[i]);
	}
	while(q.size()){
		auto top = q.front();
		q.pop();
		for (int i = 0 ; i <= 4 ; ++i){
			int rx = top.first + dx[i];
			int ry = top.second + dy[i];
			if(rx<1||ry<1||rx>n||ry>m) continue;
			if(vis[rx][ry]==true) continue;
			g[rx][ry] = g[top.first][top.second] + 1;
			vis[rx][ry]=true;
			q.push({rx,ry});
		}
	}
}
int main(){
	cin >> n >> m >> x >> y;
	for (int i = 1 ; i <= x ; ++i){
		int a,b;
		cin >> a >> b;
		v.push_back({a,b});		
	}
	bfs();
	for (int i = 1 ; i <= y ; ++i){
		int a,b;
		cin >> a >> b;
		cout << g[a][b] << endl;	
	}	
	return 0;
}
2022/12/6 01:59
加载中...