結果

問題 No.34 砂漠の行商人
ユーザー tktk_snsn
提出日時 2021-05-17 20:17:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,075 ms / 5,000 ms
コード長 1,706 bytes
コンパイル時間 959 ms
コンパイル使用メモリ 96,800 KB
実行使用メモリ 395,136 KB
最終ジャッジ日時 2024-10-06 23:45:12
合計ジャッジ時間 7,352 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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