結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2015-01-12 22:08:43 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 860 ms / 5,000 ms | 
| コード長 | 1,389 bytes | 
| コンパイル時間 | 897 ms | 
| コンパイル使用メモリ | 96,744 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-28 10:05:13 | 
| 合計ジャッジ時間 | 2,999 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;
const int INF = INT_MAX / 4;
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, -1, 0, 1};
int main()
{
    int n, v, sx, sy, gx, gy;
    cin >> n >> v >> sx >> sy >> gx >> gy;
    vector<vector<int> > level(n+2, vector<int>(n+2, INF));
    for(int y=1; y<=n; ++y){
        for(int x=1; x<=n; ++x){
            cin >> level[y][x];
        }
    }
    vector<vector<int> > dp(n+2, vector<int>(n+2, 0));
    dp[sy][sx] = v;
    for(int i=0; i<n*n; ++i){
        vector<vector<int> > nextDp(n+2, vector<int>(n+2, 0));
        for(int y=1; y<=n; ++y){
            for(int x=1; x<=n; ++x){
                for(int j=0; j<4; ++j){
                    int y2 = y + dy[j];
                    int x2 = x + dx[j];
                    nextDp[y2][x2] = max(nextDp[y2][x2], dp[y][x] - level[y2][x2]);
                }
            }
        }
        dp.swap(nextDp);
        if(dp[gy][gx] > 0){
            cout << (i + 1) << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}
            
            
            
        