結果

問題 No.157 2つの空洞
ユーザー motimoti
提出日時 2015-04-15 02:23:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,126 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 70,112 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 20:22:29
合計ジャッジ時間 1,586 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <tuple>
using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int W, H;
char G[22][22];
int dist[22][22];

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

inline bool valid(int x, int y) {
  return 0<=x&&x<W && 0<=y&&y<H;
}

int main() {

  int sx, sy;
  cin >> W >> H;
  rep(i, H) rep(j, W) {
    cin >> G[i][j];
    if(G[i][j] == '.') { sx = j, sy = i; }
  }

  std::priority_queue<std::tuple<int, int, int>> q;
  q.emplace(0, sx, sy);
  rep(i, 22) rep(j, 22) dist[i][j] = 1<<29;
  dist[sy][sx] = 0;
  while(!q.empty()) {
    int c = -std::get<0>(q.top());
    int x = std::get<1>(q.top());
    int y = std::get<2>(q.top());
    q.pop();
    rep(k, 4) {
      int nx = x+dx[k], ny = y+dy[k];
      if(!valid(nx, ny)) { continue; }
      int nc = c+(G[ny][nx]=='#');
      if(dist[ny][nx] <= nc) { continue; }
      dist[ny][nx] = nc;
      q.emplace(-nc, nx, ny);
    }
  }

  rep(i, H) rep(j, W) {
    if(G[i][j] == '.' && dist[i][j]) {
      cout << dist[i][j] << endl;
      return 0;
    }
  }

  return 0;
}
0