o2优化?
查看原帖
o2优化?
661913
liwenxi1145144444楼主2022/10/22 22:18
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int n,m,a,b;
int x,y;
int f[502][502];
int go[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x,y,time;
};
queue<node> q;
bool vis[502][502];
int bfs(){
	while(!q.empty()){
		node tmp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int xx=tmp.x+go[i][0];
			int yy=tmp.y+go[i][1];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!vis[xx][yy]){
				vis[xx][yy]=true;
				f[xx][yy]=tmp.time+1;
				q.push((node){xx,yy,tmp.time+1});
			}
		}
	}
}
int main(){
	cin>>n>>m>>a>>b;
	for(int i=1;i<=a;i++){
		cin>>x>>y;
		q.push((node){x,y,0});
		f[x][y]=0;
		vis[x][y]=true;
	}
	bfs();
	for(int i=1;i<=b;i++){
		cin>>x>>y;
		cout<<f[x][y]<<"\n";
	}
	return 0;
}

此代码不开o2AC,开o2全RE,为什么?

2022/10/22 22:18
加载中...