結果
| 問題 |
No.2897 2集合間距離
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-20 22:20:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 123 ms / 3,500 ms |
| コード長 | 984 bytes |
| コンパイル時間 | 2,187 ms |
| コンパイル使用メモリ | 201,596 KB |
| 最終ジャッジ日時 | 2025-02-24 10:23:03 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr int MOD=998244353;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,l,r) for(int i=(l);i<(int)(r);i++)
#define all(v) v.begin(),v.end()
constexpr int MAX_X=1000;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
vector dist(MAX_X,vector(MAX_X,(int)1e9));
queue<array<int,2>>bfs;
rep(i,N){
int X,Y;
cin>>X>>Y;
dist[X][Y]=0;
bfs.push({X,Y});
}
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
auto bound_ok=[&](int x,int y){
return x>=0&&x<MAX_X&&y>=0&&y<MAX_X;
};
while(bfs.size()){
auto [x,y]=bfs.front();
bfs.pop();
rep(i,4){
int x2=x+dx[i],y2=y+dy[i];
if(bound_ok(x2,y2)&&dist[x2][y2]>dist[x][y]+1){
dist[x2][y2]=dist[x][y]+1;
bfs.push({x2,y2});
}
}
}
int ans=1e9;
int M;
cin>>M;
rep(i,M){
int X,Y;
cin>>X>>Y;
ans=min(ans,dist[X][Y]);
}
cout<<ans<<'\n';
}