結果

問題 No.2897 2集合間距離
ユーザー vjudge1
提出日時 2024-09-25 15:28:32
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 244 ms / 3,500 ms
コード長 910 bytes
コンパイル時間 2,767 ms
コンパイル使用メモリ 164,020 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2024-09-25 15:28:38
合計ジャッジ時間 5,944 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAXN = 200001, MAXV = 1000, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};

int n, m, dist[MAXV][MAXV], ans = int(1e9);
bool vis[MAXV][MAXV];

int main() {
  //ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  queue<pii> que;
  for(int i = 1, x, y; i <= n; ++i) {
    cin >> x >> y;
    vis[x][y] = 1;
    dist[x][y] = 0;
    que.push({x, y});
  }
  for(; !que.empty(); ) {
    auto [x, y] = que.front();
    que.pop();
    for(int d : {0, 1, 2, 3}) {
      int nx = x + dx[d], ny = y + dy[d];
      if(nx >= 0 && nx < 1000 && ny >= 0 && ny < 1000 && !vis[nx][ny]) {
        vis[nx][ny] = 1;
        dist[nx][ny] = dist[x][y] + 1;
        que.push({nx, ny});
      }
    }
  }
  cin >> m;
  for(int i = 1, x, y; i <= m; ++i) {
    cin >> x >> y;
    ans = min(ans, dist[x][y]);
  }
  cout << ans;
  return 0;
}
0