結果

問題 No.8063 幅優先探索
ユーザー penta
提出日時 2020-06-08 22:02:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,422 bytes
コンパイル時間 2,453 ms
コンパイル使用メモリ 176,596 KB
実行使用メモリ 12,088 KB
最終ジャッジ日時 2024-12-26 13:17:17
合計ジャッジ時間 2,816 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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 start, goal;
cin >> start.first >> start.second >> goal.first >> goal.second;
start.first--; start.second--; goal.first--; goal.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 delta = make_grid_delta(h,w,caller);
auto res = bfs(h*w, start, delta, index);
cout << res[index(goal)] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0