結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
togari_takamoto
|
| 提出日時 | 2016-02-26 13:36:38 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 69 ms / 5,000 ms |
| コード長 | 1,365 bytes |
| コンパイル時間 | 1,655 ms |
| コンパイル使用メモリ | 172,784 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-28 10:27:56 |
| 合計ジャッジ時間 | 2,762 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>; using vb = vector<bool>; using vd = vector<double>; using vl = vector<ll>;
using vvi = vector<vi>; using vvb = vector<vb>; using vvd = vector<vd>; using vvl = vector<vl>;
#define REP(i,n) for(ll i=0; i<(n); ++i)
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool contain(ll n, ll x) { return 0 <= x && x < n; }
int main() {
ll n, v, sx, sy, gx, gy;
cin >> n >> v >> sx >> sy >> gx >> gy;
sx--; sy--; gx--; gy--;
vvi field(n, vi(n));
REP(y, n) REP(x, n) cin >> field[y][x];
vvl vital(n, vl(n, 0));
using P = pair<ll, pair<ll, pair<ll,ll>>>;
queue<P> que;
que.push({0, {v, {sx, sy}}});
while (!que.empty()) {
auto _ = que.front(); que.pop();
ll times = _.first;
ll cur_v = _.second.first;
auto pos = _.second.second;
if (pos.first == gx && pos.second == gy) {
cout << times << endl; return 0;
}
if (vital[pos.second][pos.first] >= cur_v) continue;
vital[pos.second][pos.first] = cur_v;
REP(i, 4) {
auto n_pos = make_pair(pos.first + dx[i], pos.second + dy[i]);
if (!contain(n, n_pos.first) || !contain(n, n_pos.second)) continue;
ll n_v = cur_v - field[n_pos.second][n_pos.first];
if(n_v > vital[n_pos.second][n_pos.first]) que.push({ times + 1, { n_v, n_pos } });
}
}
cout << -1 << endl;
return 0;
}
togari_takamoto