結果

問題 No.157 2つの空洞
ユーザー nenuonnenuon
提出日時 2017-07-28 01:59:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,341 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 94,804 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 09:13:02
合計ジャッジ時間 1,557 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0