結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
nenuon
|
| 提出日時 | 2017-07-28 01:59:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,341 bytes |
| コンパイル時間 | 874 ms |
| コンパイル使用メモリ | 94,444 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-10 02:30:23 |
| 合計ジャッジ時間 | 1,695 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
vector<string> C;
int W,H;
bool visited[20][20];
int gr[20][20];
int g;
void dfs(int y, int x) {
if(y < 0 || y >= H || x < 0 || x >= W) return;
if(visited[y][x]) return;
visited[y][x] = true;
if(C[y][x] == '#') return;
gr[y][x] = g;
dfs(y+1, x);
dfs(y-1, x);
dfs(y, x+1);
dfs(y, x-1);
return;
}
int main()
{
cin >> W >> H;
FOR(y,0,H) {
FOR(x,0,W) {
visited[y][x] = false;
gr[y][x] = -1;
}
}
FOR(i,0,H) {
string s;
cin >> s;
C.push_back(s);
}
g = 0;
FOR(y,0,H) {
FOR(x,0,W) {
if(!visited[y][x]) {
g++;
dfs(y,x);
}
}
}
int ans = 1000;
FOR(y1,0,H) {
FOR(x1,0,W) {
FOR(y2,0,H) {
FOR(x2,0,W) {
if(y1 == y2 && x1 == x2) continue;
if(C[y1][x1] == '#' || C[y2][x2] == '#') continue;
if(gr[y1][x1] != gr[y2][x2]) {
ans = min(ans, abs(y1-y2) + abs(x1-x2));
}
}
}
}
}
cout << ans - 1 << endl;
return 0;
}
nenuon