結果

問題 No.34 砂漠の行商人
ユーザー codershifthcodershifth
提出日時 2015-07-23 23:25:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 2,860 bytes
コンパイル時間 1,539 ms
コンパイル使用メモリ 159,432 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 19:09:41
合計ジャッジ時間 2,829 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 9 ms
4,380 KB
testcase_09 AC 19 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 8 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 53 ms
4,376 KB
testcase_14 AC 40 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 14 ms
4,380 KB
testcase_20 AC 21 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 25 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class DesertChapman {
public:
    void solve(void) {
            int N,V,sx,sy,gx,gy;
            cin>>N>>V>>sx>>sy>>gx>>gy;
            --sx,--sy,--gx,--gy;
            vector<vector<int>> fld(N,vector<int>(N,0));
            REP(i,N)
            REP(j,N)
                cin>>fld[i][j];

            const int inf = (1<<30);

            // cost[y][x] := (x,y) までの到達コストの最小値
            vector<vector<int>> cost(N,vector<int>(N,inf));
            queue<tuple<int,int,int,int>> que;
            // 幅優先探索でやる
            que.emplace(sx,sy,0,0);
            // 最悪ゴールまで辿りつけないケースは全探索になるので
            // O(N^2*V) <= 10^10 となる。
            //
            // ただし障害物がないので
            // ((横の移動最大回数)+(縦の移動最大回数))*max(fld[y][x]) < V なら到達可能なので
            // O(N^2*V) <= 1.8*10^7 程度の計算量になる。
            //
            while (!que.empty())
            {
                int x,y,c,t;
                tie(x,y,c,t) = que.front();
                que.pop();

                const int dx[] = {1,0,-1,0};
                const int dy[] = {0,-1,0,1};
                REP(d,4)
                {
                    int nx = x+dx[d];
                    int ny = y+dy[d];

                    if (nx < 0 || ny < 0 || N <= nx || N <= ny)
                        continue;

                    int nc = c+fld[ny][nx];
                    // 体力が付きてしまった
                    if (nc >= V)
                        continue;

                    // 到達できたら終了。幅優先なのでこれが最短時間
                    if (nx==gx && ny==gy)
                    {
                        cout<<t+1<<endl;
                        return;
                    }
                    // 到達コストを更新できるなら更新して enqueue
                    // 更新前の到達コストの方が到達時間が短い可能性があるが、
                    // その場合はとなりのマスへの移動がすでに enqueu されているはず。
                    if (cost[ny][nx] > nc)
                    {
                        cost[ny][nx] = c+fld[ny][nx];
                        que.emplace(nx,ny,cost[ny][nx],t+1);
                    }
                }
            }
            cout<<-1<<endl;
            return;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new DesertChapman();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0