結果
| 問題 |
No.2897 2集合間距離
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-09-20 21:46:26 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1,089 ms / 3,500 ms |
| コード長 | 1,098 bytes |
| コンパイル時間 | 18,205 ms |
| コンパイル使用メモリ | 405,132 KB |
| 実行使用メモリ | 227,060 KB |
| 最終ジャッジ日時 | 2024-09-20 21:47:13 |
| 合計ジャッジ時間 | 38,306 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 |
ソースコード
use std::collections::VecDeque;
use proconio::input;
const UPPER_LIMIT: usize = 3000;
const DIFFS: [(usize, usize); 4] = [(!0, 0), (0, !0), (0, 1), (1, 0)];
fn main() {
input! {
n: usize,
xy: [(usize, usize); n],
m: usize,
zw: [(usize, usize); m],
}
let mut grid: Vec<Vec<Option<usize>>> = vec![vec![None; UPPER_LIMIT]; UPPER_LIMIT];
let mut queue: VecDeque<_> = xy.iter().map(|&coord| (coord, 0_usize)).collect();
while let Some(((x, y), dist)) = queue.pop_front() {
if grid[x][y].is_some() {
continue;
}
grid[x][y] = Some(dist);
let adj_coords = DIFFS.iter().filter_map(|&(dx, dy)| {
let adj_x = x.wrapping_add(dx);
let adj_y = y.wrapping_add(dy);
if adj_x < UPPER_LIMIT && adj_y < UPPER_LIMIT {
Some(((adj_x, adj_y), dist + 1))
} else {
None
}
});
queue.extend(adj_coords);
}
let ans = zw.iter().map(|&(z, w)| grid[z][w].unwrap()).min().unwrap();
println!("{}", ans);
}