結果

問題 No.20 砂漠のオアシス
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-09-18 14:04:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,881 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 15:28:58
合計ジャッジ時間 1,725 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 32 ms
6,944 KB
testcase_06 AC 28 ms
6,940 KB
testcase_07 AC 27 ms
6,944 KB
testcase_08 AC 14 ms
6,940 KB
testcase_09 AC 23 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:45:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |   scanf("%d%d%d%d", &N, &V, &Oc, &Or);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:53:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   53 |       scanf("%d", &G[r][c]);
      |       ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <array>
#include <cassert>
#include <tuple>
#include <queue>
using namespace std;

constexpr int inf = 987654321;
                            // N  E  S  W
constexpr array<int, 4> dr = {-1, 0, 1, 0},
                        dc = { 0, 1, 0,-1};
constexpr int dr_size = dr.size();

int N, V, Oc, Or;
vector<vector<int>> G, cost;

void spfa(vector<vector<int>> &cost, int sr, int sc, int V) {
  cost.assign(N, vector<int>(N, inf));
  // cost[座標] := ダメージ量
  cost[sr][sc] = 0;
  queue<pair<int, int>> que;
  que.emplace(sr, sc);
  vector<vector<bool>> inque(N, vector<bool>(N, false));
  inque[sr][sc] = true;
  while(!que.empty()) {
    int r, c; tie(r, c) = que.front(); que.pop();
    inque[r][c] = false;
    for(int i=0; i<dr_size; ++i) {
      int nr = r + dr[i],
          nc = c + dc[i];
      if(!(0 <= nr && nr < N && 0 <= nc && nc < N)) { continue; }
      if(cost[nr][nc] > cost[r][c] + G[nr][nc]) {
        cost[nr][nc] = cost[r][c] + G[nr][nc];
        if(!inque[nr][nc]) {
          inque[nr][nc] = true;
          que.emplace(nr, nc);
        }
      }
    }
  }
}

int main(void) {
  scanf("%d%d%d%d", &N, &V, &Oc, &Or);
  assert(2 <= N && N <= 200);
  assert(1 <= V && V <= 500);
  assert((1 <= Or && Or <= N && 1 <= Oc && Oc <= N) || (Or == 0 && Oc == 0));
  --Or, --Oc;
  G.assign(N, vector<int>(N, 0));
  for(int r=0; r<N; ++r) {
    for(int c=0; c<N; ++c) {
      scanf("%d", &G[r][c]);
      assert(0 <= G[r][c] && G[r][c] <= 9);
    }
  }
  spfa(cost, 0, 0, V);
  if(cost[N-1][N-1] < V) {
    puts("YES");
    return 0;
  }
  // オアシスがあってそこまで辿りつける
  if(Or != -1 && Oc != -1 && cost[Or][Oc] < V) {
    int nV = (V - cost[Or][Oc]) * 2;
    spfa(cost, Or, Oc, nV);
    if(cost[N-1][N-1] < nV) {
      puts("YES");
      return 0;
    }
  }
  puts("NO");
  return 0;
}
0