結果
問題 | No.424 立体迷路 |
ユーザー |
|
提出日時 | 2020-06-08 22:35:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,958 bytes |
コンパイル時間 | 1,872 ms |
コンパイル使用メモリ | 178,036 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-26 15:20:49 |
合計ジャッジ時間 | 2,944 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for (int i = 0; i < (n); ++i)#define all(x) (x).begin(),(x).end()using namespace std;using ll = long long;using P = pair<int,int>;template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b; }template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b; }void debug_out() { cout << "\n"; }template <class T, class... Args>void debug_out(const T &x, const Args &... args) { cout << x << " "; debug_out(args...);}#ifdef _DEBUG#define debug(...) debug_out(__VA_ARGS__)#else#define debug(...)#endif/*delta(V v, update(V t))index(V v) -> int*/template<class V, class Delta, class Index>vector<ll> bfs(int n, V s, Delta delta, Index index) {vector<ll> dist(n,-1);queue<V> que;dist[index(s)] = 0;que.push(s);while(!que.empty()) {V v = que.front(); que.pop();auto update = [&](V t) {if (dist[index(t)] == -1) {dist[index(t)] = dist[index(v)]+1;que.push(t);}};delta(v,update);}return dist;}template<class F>struct grid_delta {int h,w;F caller;const int dx[8] = {-1,0,1,0,-1,1,-1,1};const int dy[8] = {0,-1,0,1,-1,-1,1,1};grid_delta(int h, int w, F f):h(h),w(w),caller(f){}template<class G>void operator()(P v, G update) {int y = v.first, x = v.second;for (int i=0; i<4; ++i) {int ny = y+dy[i], nx = x+dx[i];if (nx<0 || ny<0 || nx>=w || ny>=h) continue;caller(v, P(ny,nx), update);}}};template<class F>grid_delta<F> make_grid_delta(int h, int w, F f) { return grid_delta<F>(h, w, f);}struct grid_index {int h,w;grid_index(int h, int w):h(h),w(w){}int operator()(P v){ return v.first*w+v.second;}P inv(int ind){ return P((ind-(ind%w))/w ,ind%w);}};int main() {std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);std::cout << std::fixed << std::setprecision(15);int h,w;cin >> h >> w;P st, go;cin >> st.first >> st.second >> go.first >> go.second;st.first--; st.second--; go.first--; go.second--;vector<string> s(h);rep(i,h) cin >> s[i];auto index = grid_index(h,w);// auto caller = [&](P v, P t, auto update) {// if (s[t.first][t.second] == '.') update(t);// };auto caller = [&](P v, P t, auto update) {int now = s[v.first][v.second] - '0';if (s[t.first][t.second]-'0' == now) update(t);else if (s[t.first][t.second]-'0' == now+1) update(t);else if (s[t.first][t.second]-'0' == now-1) update(t);else if (s[t.first][t.second]-'0' < now) {int ny = v.first + 2*(t.first-v.first);int nx = v.second + 2*(t.second-v.second);if (nx<0 || ny<0 || nx>=w || ny>=h) return;if (s[ny][nx]-'0' == now) update(P(ny,nx));}};auto delta = make_grid_delta(h, w, caller);auto res = bfs(h*w, st, delta, index);if (res[index(go)] == -1) cout << "NO" <<endl;else cout << "YES" << endl;return 0;}