結果

問題 No.402 最も海から遠い場所
ユーザー tnakao0123tnakao0123
提出日時 2016-07-25 01:16:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 572 ms / 3,000 ms
コード長 1,810 bytes
コンパイル時間 612 ms
コンパイル使用メモリ 90,412 KB
実行使用メモリ 112,452 KB
最終ジャッジ日時 2024-04-24 08:41:34
合計ジャッジ時間 3,822 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 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 1 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 15 ms
7,296 KB
testcase_16 AC 19 ms
8,448 KB
testcase_17 AC 231 ms
43,600 KB
testcase_18 AC 572 ms
42,380 KB
testcase_19 AC 415 ms
112,452 KB
testcase_20 AC 443 ms
38,912 KB
testcase_21 AC 389 ms
76,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 402.cc: No.402 最も海から遠い場所 - 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_H = 3000;
const int MAX_W = 3000;

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

/* typedef */

typedef pair<int,int> pii;

/* global variables */

int dists[MAX_H][MAX_W];

/* subroutines */

/* main */

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

  int maxd = 0;
  queue<pii> q;

  for (int y = 0; y < h; y++) {
    string s;
    cin >> s;
    for (int x = 0; x < w; x++) {
      if (s[x] == '.')
	dists[y][x] = 0, q.push(pii(x, y));
      else
	dists[y][x] = -1;
    }
  }

  for (int y = 0; y < h; y++) {
    if (dists[y][0] < 0)
      dists[y][0] = 1, q.push(pii(0, y)), maxd = 1;
    if (dists[y][w - 1] < 0)
      dists[y][w - 1] = 1, q.push(pii(w - 1, y)), maxd = 1;
  }
  for (int x = 0; x < w; x++) {
    if (dists[0][x] < 0)
      dists[0][x] = 1, q.push(pii(x, 0)), maxd = 1;
    if (dists[h - 1][x] < 0)
      dists[h - 1][x] = 1, q.push(pii(x, h - 1)), maxd = 1;
  }

  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int &ux = u.first, &uy = u.second;
    int vd = dists[uy][ux] + 1;
    
    for (int di = 0; di < 8; di++) {
      int vx = ux + dxs[di], vy = uy + dys[di];
      if (vx >= 0 && vx < w && vy >= 0 && vy < h && dists[vy][vx] < 0) {
	dists[vy][vx] = vd;
	q.push(pii(vx, vy));
	maxd = vd;
      }
    }
  }

  printf("%d\n", maxd);
  return 0;
}
0