結果

問題 No.157 2つの空洞
ユーザー koba-e964koba-e964
提出日時 2015-06-05 23:42:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,912 bytes
コンパイル時間 771 ms
コンパイル使用メモリ 93,840 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 19:28:11
合計ジャッジ時間 1,706 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:5: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   ff(x, y);
   ~~^~~~~~

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;
const int N = 30;
string s[30];

int first[N][N] = {0};
int dp[N][N] = {0};
int dx[4] = {1,0,-1,0}, dy[4] = {0, 1, 0, -1};

int h, w;
int read(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w) {
    return 0;
  }
  return s[x][y] == '.';
}

void ff(int x, int y) {
  if (read(x, y) == 0) {
    return;
  }
  first[x][y] = 1;
  REP (i, 0, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (read(nx, ny) && first[nx][ny] == 0) {
      ff(nx, ny);
    }
  }
}

int main(void){
  cin >> w >> h;
  int x = -1,y;
  REP(i, 0, h) {
    cin >> s[i];
    REP(j, 0, w) {
      if (x == -1 && s[i][j] == '.') {
	x = i, y = j;
      }
    }
    fill_n(dp[i], w, -1);
  }
  ff(x, y);
  queue<pair<PI, int> > que;
  REP(i,0, h) {
    REP(j, 0, w) {
      if (first[i][j]) {
	que.push(pair<PI,int>(PI(i, j), 0));
      }
    }
  }
  int mi = 1000000;
  while (! que.empty()) {
    pair<PI, int> pi = que.front(); que.pop();
    PI p = pi.first;
    if (dp[p.first][p.second] >= 0) {
      continue;
    }
    dp[p.first][p.second] = pi.second;
    if (read(p.first, p.second)&& pi.second) {
      mi = min(mi, pi.second);
    }
    REP(i, 0, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      int nv = pi.second + 1;
      que.push(pair<PI, int>(PI(nx,ny), nv));
    }
  }
  cout << mi - 1 << endl;
}
0