結果

問題 No.3063 幅優先探索
ユーザー kenken714kenken714
提出日時 2020-04-01 22:02:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 99,148 KB
実行使用メモリ 14,920 KB
最終ジャッジ日時 2023-09-09 18:16:59
合計ジャッジ時間 2,311 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
12,892 KB
testcase_01 AC 8 ms
13,072 KB
testcase_02 AC 8 ms
13,072 KB
testcase_03 AC 7 ms
12,944 KB
testcase_04 AC 7 ms
12,956 KB
testcase_05 AC 8 ms
12,880 KB
testcase_06 AC 67 ms
14,920 KB
testcase_07 AC 79 ms
14,920 KB
testcase_08 AC 8 ms
12,880 KB
testcase_09 AC 8 ms
12,888 KB
testcase_10 AC 8 ms
12,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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));
			}
		}
	}
}
0