結果

問題 No.34 砂漠の行商人
ユーザー tktk_snsntktk_snsn
提出日時 2021-05-17 20:13:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,706 bytes
コンパイル時間 899 ms
コンパイル使用メモリ 97,836 KB
実行使用メモリ 396,372 KB
最終ジャッジ日時 2024-04-16 08:24:09
合計ジャッジ時間 4,889 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,636 KB
testcase_01 AC 3 ms
9,816 KB
testcase_02 AC 9 ms
35,100 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 299 ms
395,432 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 32 ms
44,544 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <deque>
#include <unordered_map>
#include <string>
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }

int dist[102][102][10004];
int A[102][102];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
    int n, v, sx, sy, gx, gy;
    cin >> n >> v >> sy >> sx >> gy >> gx;
    sx--; sy--; gx--; gy--;
    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            cin >> A[i][j];

    for(int i=0; i<n; ++i)
        for(int j=0; j<n; ++j)
            for(int k=0; k<=v; ++k)
                dist[i][j][k] = -1;

    dist[sx][sy][v] = 0;
    int nsq = n*n;
    deque<int> que;
    que.push_back(nsq*v + sx*v + sy);
    while (!que.empty()) {
        int vxy, tx, ty, tv;
        vxy = que.front(); que.pop_front();
        tv = vxy/nsq;
        tx = (vxy%nsq)/n;
        ty = (vxy%nsq)%n;
        if (tx==gx && ty==gy) {
            cout << dist[tx][ty][tv] << endl;
            return 0;
        }
        for(int i=0; i<4; ++i) {
            int nx, ny, nv;
            nx = tx + dx[i];
            ny = ty + dy[i];
            if (0 <= nx && nx < n && 0 <= ny && ny < n && tv - A[nx][ny] > 0) {
                nv = tv - A[nx][ny];
                if (dist[nx][ny][nv] == -1) {
                    dist[nx][ny][nv] = dist[tx][ty][tv] + 1;
                    que.push_back(nsq*nv + nx*n + ny);
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
0