結果
| 問題 | No.20 砂漠のオアシス |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-09-18 14:04:31 |
| 言語 | C++11(廃止可能性あり) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 33 ms / 5,000 ms |
| コード長 | 1,881 bytes |
| 記録 | |
| コンパイル時間 | 1,254 ms |
| コンパイル使用メモリ | 81,824 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-08 02:31:41 |
| 合計ジャッジ時間 | 2,026 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
コンパイルメッセージ
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]);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#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;
}