結果

問題 No.20 砂漠のオアシス
ユーザー 古寺いろは
提出日時 2015-04-15 22:42:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,092 bytes
コンパイル時間 1,365 ms
コンパイル使用メモリ 168,944 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-13 05:06:23
合計ジャッジ時間 2,013 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int N;
int dp[200][200][2];
int L[200][200];

int ok(int a, int b){
	return a >= 0 && b >= 0 && a < N && b < N;
}

int main() {
	int V, Ox, Oy;
	cin >> N >> V >> Ox >> Oy;
	Ox--; Oy--;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			cin >> L[i][j];
		}
	}
	
	dp[0][0][1] = V;
	priority_queue<tuple<int, int, int, int>> pq;

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

	pq.push(make_tuple(V, 0, 0, 1));
	while (!pq.empty()){
		auto now = pq.top(); pq.pop();
		int h = get<0>(now);
		int y = get<1>(now);
		int x = get<2>(now);
		int f = get<3>(now);
		if (y == N - 1 && x == N - 1){
			cout << "YES" << endl;
			return 0;
		}

		if (dp[y][x][f] != h) continue;

		if (f == 1 && y == Oy && x == Ox){
			h *= 2;
			f = 0;
		}
		for (int k = 0; k < 4; k++)
		{
			int ny = y + dy[k];
			int nx = x + dx[k];
			if (!ok(ny, nx)) continue;
			int nh = h - L[ny][nx];
			if (nh <= 0) continue;
			if (dp[ny][nx][f] < nh){
				dp[ny][nx][f] = nh;
				pq.push(make_tuple(nh, ny, nx, f));
			}
		}

	}
	cout << "NO" << endl;
}



0