結果

問題 No.34 砂漠の行商人
ユーザー imulanimulan
提出日時 2016-07-19 17:29:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,538 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 171,796 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 19:24:11
合計ジャッジ時間 2,836 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<int,int> pi;
typedef pair<pi,int> P;

const int INF=12345678;

int cost[100][100];
int L[100][100];

int dx[4]={1,-1,0,0}, dy[4]={0,0,1,-1};

int main()
{
    int n,v,sx,sy,gx,gy;
    cin >>n >>v >>sx >>sy >>gx >>gy;
    --sx;
    --sy;
    --gx;
    --gy;
    rep(i,n)rep(j,n) scanf(" %d", &L[i][j]);

    fill(cost[0],cost[100],INF);
    cost[sy][sx]=0;

    int ans=-1;

    queue<P> que;
    que.push(P(pi(sx,sy),0));
    while(!que.empty())
    {
        P p=que.front();
        que.pop();

        pi now=p.fi;
        //printf("%d,%d : %d\n", now.fi,now.se,p.se);

        //コストv以下でゴールに辿り着いたのでおわり
        if(now==pi(gx,gy))
        {
            ans=p.se;
            break;
        }

        //4近傍
        rep(i,4)
        {
            int nx=now.fi+dx[i], ny=now.se+dy[i];
            if(0<=nx&&nx<n && 0<=ny&&ny<n)
            {
                if(cost[now.se][now.fi]+L[ny][nx]<v && cost[ny][nx]>cost[now.se][now.fi]+L[ny][nx])
                {
                    cost[ny][nx]=cost[now.se][now.fi]+L[ny][nx];
                    que.push(P(pi(nx,ny),p.se+1));
                }
            }
        }
    }

    cout << ans << endl;
    return 0;
}
0