結果
問題 | No.20 砂漠のオアシス |
ユーザー |
|
提出日時 | 2015-01-26 18:15:17 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 24 ms / 5,000 ms |
コード長 | 1,662 bytes |
コンパイル時間 | 836 ms |
コンパイル使用メモリ | 73,768 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-13 05:00:20 |
合計ジャッジ時間 | 1,412 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <utility> #include <queue> using namespace std; const int INF = 1e9; vector< vector<int> > dessert; vector< vector<int> > hp; int n; void show(vector< vector<int> > &vec) { int n = vec.size(); for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { printf("%3d ", vec[i][j]); } printf("\n"); } } bool walk(int sx, int sy) { queue< pair<int, int> > que; que.push(make_pair(sx, sy)); int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int x, y, nx, ny; while (!que.empty()) { auto now = que.front(); x = now.first; y = now.second; que.pop(); for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (hp[y][x] > dessert[ny][nx] && hp[y][x] - dessert[ny][nx] > hp[ny][nx]) { hp[ny][nx] = hp[y][x] - dessert[ny][nx]; if (nx == n && ny == n) { return true; } que.push(make_pair(nx, ny)); } } } return false; } int main() { int v, ox, oy; cin >> n >> v >> ox >> oy; dessert.assign(n + 2, vector<int>(n + 2, INF)); hp.assign(n + 2, vector<int>(n + 2, -1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> dessert[i][j]; } } hp[1][1] = v; bool isGoal = walk(1, 1); // printf("%s\n", isGoal ? "OK" : "NG"); // show(hp); // cout << endl; if (!isGoal) { int hp_oasys = hp[oy][ox]; // printf("%d\n", hp_oasys); if (hp_oasys <= 0) { isGoal = false; } else { hp.assign(n + 2, vector<int>(n + 2, -1)); hp[oy][ox] = 2 * hp_oasys; isGoal = walk(ox, oy); } // show(hp); } cout << (isGoal ? "YES" : "NO") << endl; return 0; }