結果
| 問題 |
No.8063 幅優先探索
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-01 22:02:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 97 ms / 2,000 ms |
| コード長 | 1,520 bytes |
| コンパイル時間 | 1,306 ms |
| コンパイル使用メモリ | 99,508 KB |
| 実行使用メモリ | 14,848 KB |
| 最終ジャッジ日時 | 2024-06-27 10:53:14 |
| 合計ジャッジ時間 | 1,916 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 |
ソースコード
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007
#define P pair<ll, ll>
vector<vector<ll>> d(1100, vector<ll>(1100, INF));
ll v1[4] = { 0,0,1,-1 }, v2[4] = { 1,-1,0,0 };
int main() {
ll h, w;
cin >> h >> w;
ll h1, w1, h2, w2;
cin >> h1 >> w1 >> h2 >> w2;
string s[1100];
REP(i, w + 2) s[0].push_back('.');
REP(i, h) {
s[i + 1].push_back('.');
REP(j, w) {
char a;
cin >> a;
s[i + 1].push_back(a);
}
s[i + 1].push_back('.');
}
REP(i, w + 2) s[h + 1].push_back('.');
h += 2;
w += 2;
queue<P> q;
q.push(P(h1, w1));
d[h1][w1] = 0;
while (!q.empty()) {
P v = q.front();
q.pop();
if (v == P(h2, w2)) {
cout << d[h2][w2] << endl;
return 0;
}
REP(l, 4) {
ll x = v.first + v1[l], y = v.second + v2[l];
if (0 > x or x >= h or 0 > y or y >= w)continue;
if (d[v.first][v.second] + 1 < d[x][y]) {
d[x][y] = d[v.first][v.second] + 1;
q.push(P(x, y));
}
}
}
}