結果

問題 No.34 砂漠の行商人
ユーザー たこしたこし
提出日時 2015-06-12 16:20:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 902 ms / 5,000 ms
コード長 1,876 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 96,108 KB
実行使用メモリ 397,508 KB
最終ジャッジ日時 2023-09-10 19:09:06
合計ジャッジ時間 6,675 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,468 KB
testcase_01 AC 3 ms
9,764 KB
testcase_02 AC 15 ms
64,928 KB
testcase_03 AC 13 ms
58,776 KB
testcase_04 AC 44 ms
148,140 KB
testcase_05 AC 56 ms
150,760 KB
testcase_06 AC 34 ms
165,364 KB
testcase_07 AC 60 ms
201,556 KB
testcase_08 AC 71 ms
207,696 KB
testcase_09 AC 311 ms
277,396 KB
testcase_10 AC 167 ms
396,684 KB
testcase_11 AC 763 ms
397,508 KB
testcase_12 AC 47 ms
196,880 KB
testcase_13 AC 902 ms
268,344 KB
testcase_14 AC 721 ms
265,200 KB
testcase_15 AC 51 ms
201,156 KB
testcase_16 AC 86 ms
203,836 KB
testcase_17 AC 57 ms
213,464 KB
testcase_18 AC 20 ms
75,496 KB
testcase_19 AC 231 ms
224,584 KB
testcase_20 AC 392 ms
235,020 KB
testcase_21 AC 58 ms
218,488 KB
testcase_22 AC 73 ms
229,984 KB
testcase_23 AC 67 ms
226,200 KB
testcase_24 AC 519 ms
314,116 KB
testcase_25 AC 69 ms
207,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define INF_MIN 100000000
#define INF 1145141919
#define INF_MAX 2147483647
#define LL_MAX 9223372036854775807
#define EPS 1e-10
#define PI acos(-1)
#define LL long long

using namespace std;

#define MAX_N 101
#define MAX_V 10001

int N, V, Sx, Sy, Gx, Gy;

int maze[MAX_N][MAX_N];
int dp[MAX_N][MAX_N][MAX_V];

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

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

int solve(){

  dp[Sy][Sx][V] = 0;
  
  queue<PP> que;
  que.push(PP(P(Sx, Sy), P(V, -1)));

  while(que.size()){

    PP p = que.front();
    que.pop();
    int x = p.first.first, y = p.first.second;
    int v = p.second.first;
    int preDir = p.second.second;

    if(Gx == x && Gy == y)
      return dp[y][x][v];

    for(int d = 0; d < 4; d++){
      if(d == preDir)
	continue;
      int nx = x + dx[d];
      int ny = y + dy[d];
      int nextDir = (d + 2) % 4;
      if(0 <= nx && nx < N && 0 <= ny && ny < N){
	int nextV = v - maze[ny][nx];
	if(nextV > 0 && dp[ny][nx][nextV] > dp[y][x][v] + 1){
	  dp[ny][nx][nextV] = dp[y][x][v] + 1;
	  que.push(PP(P(nx, ny), P(nextV, nextDir)));
	}
      }
    }
    
  }
  
  return -1;

}

int main(){

  cin >> N >> V >> Sx >> Sy >> Gx >> Gy;
  
  Sx--; Sy--;
  Gx--; Gy--;
  
  for(int i = 0; i < N; i++){
    for(int j = 0; j < N; j++){
      cin >> maze[i][j];
      for(int k = 0; k <= V; k++){
	dp[i][j][k] = INF;
      }
    }
  }

  cout << solve() << endl;

  return 0;

}
0