結果

問題 No.34 砂漠の行商人
ユーザー tnakao0123tnakao0123
提出日時 2016-03-03 18:05:15
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,808 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 96,880 KB
実行使用メモリ 351,496 KB
最終ジャッジ日時 2023-10-24 19:41:12
合計ジャッジ時間 11,945 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
351,496 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 5 ms
4,668 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 66 ms
12,172 KB
testcase_05 AC 63 ms
12,296 KB
testcase_06 AC 4 ms
4,712 KB
testcase_07 AC 107 ms
17,556 KB
testcase_08 AC 151 ms
22,304 KB
testcase_09 AC 3,414 ms
204,780 KB
testcase_10 AC 144 ms
21,556 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 34.cc: No.34 砂漠の行商人 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_V = 10000;

const int INF = 1 << 30;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

typedef map<int,int> mii;

struct Stat {
  int d, x, y, v;
  Stat() {}
  Stat(int _d, int _x, int _y, int _v): d(_d), x(_x), y(_y), v(_v) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

int flds[MAX_N][MAX_N];
mii dists[MAX_N][MAX_N];

/* subroutines */

/* main */

int main() {
  int n, v, sx, sy, gx, gy;
  cin >> n >> v >> sx >> sy >> gx >> gy;
  v--, sx--, sy--, gx--, gy--;

  for (int y = 0; y < n; y++)
    for (int x = 0; x < n; x++) cin >> flds[y][x];

  dists[sy][sx][v] = 0;
  
  queue<Stat> q;
  q.push(Stat(0, sx, sy, v));

  int mind = -1;

  while (! q.empty()) {
    Stat u = q.front(); q.pop();

    if (u.x == gx && u.y == gy) {
      mind = u.d;
      break;
    }

    int vd = u.d + 1;
    
    for (int di = 0; di < 4; di++) {
      int vx = u.x + dxs[di], vy = u.y + dys[di];
      if (vx >= 0 && vx < n && vy >= 0 && vy < n && u.v >= flds[vy][vx]) {
	int vv = u.v - flds[vy][vx];
	mii::iterator mit = dists[vy][vx].find(vv);
	if (mit == dists[vy][vx].end()) {
	  dists[vy][vx][vv] = vd;
	  q.push(Stat(vd, vx, vy, vv));
	}
	else if (mit->second > vd) {
	  mit->second = vd;
	  q.push(Stat(vd, vx, vy, vv));
	}
      }
    }
  }

  printf("%d\n", mind);
  return 0;
}
0