結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  tnakao0123 | 
| 提出日時 | 2016-03-03 18:27:53 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 950 ms / 5,000 ms | 
| コード長 | 1,748 bytes | 
| コンパイル時間 | 784 ms | 
| コンパイル使用メモリ | 95,196 KB | 
| 実行使用メモリ | 93,312 KB | 
| 最終ジャッジ日時 | 2024-06-28 10:31:28 | 
| 合計ジャッジ時間 | 4,574 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
/* -*- 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].lower_bound(vv);
	if (mit == dists[vy][vx].end() || mit->second > vd) {
	  dists[vy][vx][vv] = vd;
	  q.push(Stat(vd, vx, vy, vv));
	}
      }
    }
  }
  printf("%d\n", mind);
  return 0;
}
            
            
            
        