結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
hotpepsi
|
| 提出日時 | 2015-09-23 23:27:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 995 bytes |
| コンパイル時間 | 681 ms |
| コンパイル使用メモリ | 60,760 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-19 08:57:39 |
| 合計ジャッジ時間 | 1,840 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
using namespace std;
int W, H;
string b[32];
void fill(int x, int y) {
b[y][x] = '-';
int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
for (int d = 0; d < 4; ++d) {
int nx = x + dx[d], ny = y + dy[d];
if (nx >= 0 && nx < W && ny >= 0 && ny < H) {
if (b[ny][nx] == '.') {
fill(nx, ny);
}
}
}
}
int main(int argc, char *argv[])
{
string s;
getline(cin, s);
stringstream ss(s);
ss >> W >> H;
for (int i = 0; i < H; ++i) {
getline(cin, b[i]);
}
for (int i = 0; i < H; ++i) {
int pos = b[i].find('.');
if (pos >= 0) {
fill(pos, i);
break;
}
}
int ans = 1 << 30;
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (b[i][j] == '-') {
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
if (b[y][x] == '.') {
ans = min(ans, abs(x - j) + abs(y - i) - 1);
}
}
}
}
}
}
cout << ans << endl;
return 0;
}
hotpepsi