結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-06-21 23:49:35 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,167 bytes | 
| コンパイル時間 | 1,227 ms | 
| コンパイル使用メモリ | 159,912 KB | 
| 実行使用メモリ | 380,004 KB | 
| 最終ジャッジ日時 | 2024-06-30 17:46:59 | 
| 合計ジャッジ時間 | 13,356 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 9 TLE * 1 -- * 16 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
int N, V, sx, sy, gx, gy;
int L[100][100];
// (vital, x, y) => length
int dp[10000][100][100];
const int dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
const int INF = 100000000;
// v: vital, x:x coord, y:y coord, l:length, od: old direction
void recursive(int v, int x, int y, int l, int od) {
	if (v <= 0 || dp[v][y][x] <= l) return;
	dp[v][y][x] = l;
	if(x == gx && y == gy) return;
	for(int d = 0; d < 4; d++) {
		int nx = x + dir[d][1], ny = y + dir[d][0];
		if(nx < 0 || N <= nx || ny < 0 || N <= ny || d == od) continue;
		recursive(v - L[ny][nx], nx, ny, l + 1, d ^ 2);
	}
}
int main() {
	#ifdef DEBUG
	std::ifstream in("/home/share/inputf.in");
	std::cin.rdbuf(in.rdbuf());
	#endif
	cin >> N >> V >> sx >> sy >> gx >> gy;
	sx--, sy--, gx--, gy--;
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < N; j++) {
			cin >> L[i][j];
			for(int k = 0; k < 10000; k++) {
				dp[k][i][j] = INF;
			}
		}
	}
	recursive(V, sx, sy, 0, -1);
	int ans = INF;
	for(int i = 0; i < 10000; i++) {
		if(dp[i][gy][gx] == 0) continue;
		ans = min(ans, dp[i][gy][gx]);
	}
	if(ans == INF) ans = -1;
	cout << ans << endl;
	return 0;
}
            
            
            
        