結果

問題 No.402 最も海から遠い場所
ユーザー nebukuro09nebukuro09
提出日時 2016-09-23 13:20:25
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,766 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 76,556 KB
実行使用メモリ 179,112 KB
最終ジャッジ日時 2024-04-28 21:27:46
合計ジャッジ時間 6,877 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 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 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 44 ms
7,040 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 283 ms
20,988 KB
testcase_16 AC 634 ms
25,520 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <unordered_set>
#include <string>
#include <functional>
using namespace std;

template <class T>
inline void hash_combine(std::size_t& seed, const T& v)
{
  hash<T> hasher;
  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}

struct SimpleHash {
  size_t operator()(const std::pair<int, int>& p) const {
    size_t seed = 0;
    hash_combine(seed, p.first);
    hash_combine(seed, p.second);
    return seed;
  }
};

int main() {
  int H, W;
  cin >> H >> W;
  string s;
  std::unordered_set<std::pair<int, int>, SimpleHash> frontier;
  std::unordered_set<std::pair<int, int>, SimpleHash> new_frontier;
  std::unordered_set<std::pair<int, int>, SimpleHash> visited;
  for (int i = 0; i < H; i++) {
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '.')
	frontier.emplace(i, j);
    }
  }
  for (int i = -1; i < H+1; i++) {
    frontier.emplace(i, -1);
    frontier.emplace(i, W);
  }
  for (int j = -1; j < W+1; j++) {
    frontier.emplace(-1, j);
    frontier.emplace(H, j);
  }

  int dr[8] = {-1, -1 , -1, 0, 0, 1, 1, 1};
  int dc[8] = {-1, 0, 1, -1, 1,  -1, 0, 1};
  int nr, nc;
  pair<int, int> nrnc;
  int ans = -1;
  while ((int)frontier.size() > 0) {
    ans += 1;
    new_frontier.clear();
    for (const auto& elem: frontier) {
      for (int i = 0; i < 8; i++) {
	nr = elem.first + dr[i];
	nc = elem.second+ dc[i];
	if (nr < 0 || nr >= H || nc < 0 || nc >= W)
	  continue;
	nrnc = pair<int, int>(nr, nc);
	if (visited.find(nrnc) != visited.end() || frontier.find(nrnc) != frontier.end())
	  continue;
	new_frontier.insert(nrnc);
      }
    }
    for (const auto& elem: frontier)
      visited.insert(elem);
    frontier.swap(new_frontier);
  }

  cout << ans << endl;
  return 0;
}
0