結果

問題 No.157 2つの空洞
ユーザー tnakao0123tnakao0123
提出日時 2016-03-23 14:40:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,465 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 94,324 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 21:00:20
合計ジャッジ時間 1,781 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 157.cc: No.157 2つの空洞 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 20;
const int MAX_H = 20;

const int INF = 1 << 30;
const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef queue<pii> qpii;

/* global variables */

bool flds[MAX_H][MAX_W];
int hs[MAX_H][MAX_W], dists[MAX_H][MAX_W];

/* subroutines */

/* main */

int main() {
  int w, h;
  cin >> w >> h;

  for (int y = 0; y < h; y++) {
    string s;
    cin >> s;
    for (int x = 0; x < w; x++) flds[y][x] = (s[x] == '.');
  }

  memset(hs, -1, sizeof(hs));
  int id = 0;
  vpii hvs[2];

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++)
      if (flds[y][x] && hs[y][x] < 0) {
	hs[y][x] = id;
	hvs[id].push_back(pii(x, y));
	qpii q;
	q.push(pii(x, y));

	while (! q.empty()) {
	  pii u = q.front(); q.pop();
	  int &ux = u.first, &uy = u.second;
	  for (int di = 0; di < 4; di++) {
	    int vx = ux + dxs[di], vy = uy + dys[di];
	    if (vx >= 0 && vx < w && vy >= 0 && vy < h &&
		flds[vy][vx] && hs[vy][vx] < 0) {
	      hs[vy][vx] = id;
	      hvs[id].push_back(pii(vx, vy));
	      q.push(pii(vx, vy));
	    }
	  }
	}

	id++;
      }
  
  for (int y = 0; false && y < h; y++) {
    for (int x = 0; x < w; x++) printf("%2d", hs[y][x]);
    putchar('\n');
  }

  for (int y = 0; y < h; y++)
    for (int x = 0; x < w; x++) dists[y][x] = INF;
  qpii q;

  for (int i = 0; i < hvs[0].size(); i++) {
    int &ux = hvs[0][i].first, &uy = hvs[0][i].second;
    dists[uy][ux] = 0;
    q.push(hvs[0][i]);
  }

  int mind = INF;

  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int &ux = u.first, &uy = u.second;
    int &ud = dists[uy][ux];

    if (hs[uy][ux] == 1) {
      mind = ud;
      break;
    }

    int vd = ud + 1;
    for (int di = 0; di < 4; di++) {
      int vx = ux + dxs[di], vy = uy + dys[di];
      if (vx >= 0 && vx < w && vy >= 0 && vy < h &&
	  (! flds[vy][vx] || hs[vy][vx] == 1) &&
	  dists[vy][vx] > vd) {
	dists[vy][vx] = vd;
	q.push(pii(vx, vy));
      }
    }
  }

  printf("%d\n", mind - 1);
  return 0;
}
0