結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  btk | 
| 提出日時 | 2015-05-13 23:53:47 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 825 ms / 5,000 ms | 
| コード長 | 982 bytes | 
| コンパイル時間 | 615 ms | 
| コンパイル使用メモリ | 57,852 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-28 10:16:55 | 
| 合計ジャッジ時間 | 2,545 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#include<iostream>
#include<algorithm>
using namespace std;
int cost[110][110];
int dp[2][110][110];
int INF = 1000000;
int dir[] = { 0, 1, 0, -1, 0 };
int main(void){
	int N, V, NN, SX, SY, GX, GY;
	cin >> N >> V >> SY >> SX >> GY >> GX;
	NN = N*N;
	for (int i = 0; i < N; i++){
		cost[i][0] = cost[0][i] = cost[i][N + 1] = cost[N + 1][i] = INF;
	}
	for (int i = 0; i < N; i++){
		for (int j = 0; j < N; j++){
			cin >> cost[i + 1][j + 1];
			dp[0][i + 1][j + 1]=dp[1][i + 1][j + 1] = INF;
		}
	}
	dp[0][SX][SY] = 0;
	for (int i = 1; i <= NN; i++){
		for (int x = 1; x <= N; x++){
			for (int y = 1; y <= N; y++){
				for (int j = 0; j < 4; j++){
					dp[i & 1][x + dir[j]][y + dir[j + 1]] = min(dp[i & 1][x + dir[j]][y + dir[j + 1]], dp[(i & 1) ^ 1][x][y] + cost[x + dir[j]][y + dir[j + 1]]);
				}
				dp[i&1][x][y] = min(dp[i&1][x][y], dp[(i & 1) ^ 1][x][y]);
			}
		}
		if (dp[i&1][GX][GY] < V){
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}
            
            
            
        