結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 RE -
testcase_12 AC 1 ms
4,380 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,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:40:13: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if (s[x][y] != '.') {
             ^
main.cpp:54:14: note: ‘y’ was declared here
   int x = -1,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};

void ff(int x, int y) {
  if (s[x][y] != '.') {
    return;
  }
  first[x][y] = 1;
  REP (i, 0, 4) {
    if (first[x + dx[i]][y + dy[i]] == 0) {
      ff(x + dx[i], y + dy[i]);
    }
  }
}

int main(void){
  int w,h;
  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 (s[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