結果
| 問題 |
No.424 立体迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-23 23:58:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,494 bytes |
| コンパイル時間 | 808 ms |
| コンパイル使用メモリ | 84,632 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-05 07:08:40 |
| 合計ジャッジ時間 | 1,436 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 21 |
ソースコード
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1,};
int h, w;
bool inside(int x, int y) {
return (0 <= x && x < w && 0 <= y && y < h);
}
int main() {
int sx, sy, gx, gy;
cin >> h >> w >> sy >> sx >> gy >> gx;
sx--;
sy--;
gx--;
gy--;
vector<string> b(h);
for (int i = 0; i < h; i++) {
cin >> b[i];
}
vector<vector<bool>> went(h, vector<bool>(w, false));
queue<pair<int, int>> que;
bool possible = false;
if (sx == gx && sy == gy) {
possible = true;
}
que.push(make_pair(sx, sy));
while (!possible && !que.empty()) {
auto p = que.front();
que.pop();
int x = p.first;
int y = p.second;
for (int i = 0; i < 4; i++) {
int nx1 = x + dx[i];
int ny1 = y + dy[i];
int nx2 = x + dx[i] * 2;
int ny2 = y + dy[i] * 2;
if (inside(nx1, ny1) &&
!went[ny1][nx1] &&
abs(b[ny1][nx1] - b[y][x]) <= 1)
{
went[ny1][nx1] = true;
que.push(make_pair(nx1, ny1));
if (nx1 == gx && ny1 == gy) {
possible = true;
break;
}
}
if (inside(nx2, ny2) &&
!went[ny2][nx2] &&
b[ny1][nx1] < b[y][x] &&
b[ny2][nx2] == b[y][x])
{
went[ny2][nx2] = true;
que.push(make_pair(nx2, ny2));
if (nx2 == gx && ny2 == gy) {
possible = true;
break;
}
}
}
}
cout << (possible ? "YES" : "NO") << endl;
return 0;
}