結果
問題 | No.157 2つの空洞 |
ユーザー |
![]() |
提出日時 | 2016-03-23 14:40:25 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,465 bytes |
コンパイル時間 | 911 ms |
コンパイル使用メモリ | 95,492 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-01 21:25:39 |
合計ジャッジ時間 | 1,869 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 16 |
ソースコード
/* -*- coding: utf-8 -*- * * 157.cc: No.157 2つの空洞 - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_W = 20; const int MAX_H = 20; const int INF = 1 << 30; const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1}; /* typedef */ typedef pair<int,int> pii; typedef vector<pii> vpii; typedef queue<pii> qpii; /* global variables */ bool flds[MAX_H][MAX_W]; int hs[MAX_H][MAX_W], dists[MAX_H][MAX_W]; /* subroutines */ /* main */ int main() { int w, h; cin >> w >> h; for (int y = 0; y < h; y++) { string s; cin >> s; for (int x = 0; x < w; x++) flds[y][x] = (s[x] == '.'); } memset(hs, -1, sizeof(hs)); int id = 0; vpii hvs[2]; for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) if (flds[y][x] && hs[y][x] < 0) { hs[y][x] = id; hvs[id].push_back(pii(x, y)); qpii q; q.push(pii(x, y)); while (! q.empty()) { pii u = q.front(); q.pop(); int &ux = u.first, &uy = u.second; for (int di = 0; di < 4; di++) { int vx = ux + dxs[di], vy = uy + dys[di]; if (vx >= 0 && vx < w && vy >= 0 && vy < h && flds[vy][vx] && hs[vy][vx] < 0) { hs[vy][vx] = id; hvs[id].push_back(pii(vx, vy)); q.push(pii(vx, vy)); } } } id++; } for (int y = 0; false && y < h; y++) { for (int x = 0; x < w; x++) printf("%2d", hs[y][x]); putchar('\n'); } for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) dists[y][x] = INF; qpii q; for (int i = 0; i < hvs[0].size(); i++) { int &ux = hvs[0][i].first, &uy = hvs[0][i].second; dists[uy][ux] = 0; q.push(hvs[0][i]); } int mind = INF; while (! q.empty()) { pii u = q.front(); q.pop(); int &ux = u.first, &uy = u.second; int &ud = dists[uy][ux]; if (hs[uy][ux] == 1) { mind = ud; break; } int vd = ud + 1; for (int di = 0; di < 4; di++) { int vx = ux + dxs[di], vy = uy + dys[di]; if (vx >= 0 && vx < w && vy >= 0 && vy < h && (! flds[vy][vx] || hs[vy][vx] == 1) && dists[vy][vx] > vd) { dists[vy][vx] = vd; q.push(pii(vx, vy)); } } } printf("%d\n", mind - 1); return 0; }