結果

問題 No.34 砂漠の行商人
ユーザー tktk_snsntktk_snsn
提出日時 2021-05-17 20:17:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,203 ms / 5,000 ms
コード長 1,706 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 97,132 KB
実行使用メモリ 395,572 KB
最終ジャッジ日時 2024-04-16 08:27:51
合計ジャッジ時間 7,910 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 21 ms
14,848 KB
testcase_05 AC 24 ms
18,816 KB
testcase_06 AC 8 ms
11,392 KB
testcase_07 AC 37 ms
26,368 KB
testcase_08 AC 50 ms
35,584 KB
testcase_09 AC 440 ms
231,908 KB
testcase_10 AC 339 ms
395,012 KB
testcase_11 AC 1,183 ms
395,572 KB
testcase_12 AC 21 ms
32,784 KB
testcase_13 AC 1,203 ms
141,568 KB
testcase_14 AC 906 ms
135,936 KB
testcase_15 AC 42 ms
54,656 KB
testcase_16 AC 102 ms
92,288 KB
testcase_17 AC 39 ms
53,248 KB
testcase_18 AC 12 ms
11,648 KB
testcase_19 AC 247 ms
59,392 KB
testcase_20 AC 431 ms
76,032 KB
testcase_21 AC 32 ms
44,544 KB
testcase_22 AC 58 ms
72,320 KB
testcase_23 AC 98 ms
131,456 KB
testcase_24 AC 718 ms
241,920 KB
testcase_25 AC 49 ms
49,120 KB
権限があれば一括ダウンロードができます

ソースコード

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*n + 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