結果

問題 No.424 立体迷路
ユーザー misora192misora192
提出日時 2020-05-21 08:38:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,458 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 172,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 23:15:19
合計ジャッジ時間 2,814 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

vector<int> dr = {1, 0, -1, 0};
vector<int> dc = {0, 1, 0, -1};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int h, w;
	cin >> h >> w;

	int sx, sy, gx, gy;
	cin >> sx >> sy >> gx >> gy;
	sx--; sy--; gx--; gy--;

	vector<vector<int>> g(h, vector<int>(w));
	rep(i, h) {
		string s;
		cin >> s;
		rep(j, w) g[i][j] = s[j] - '0';
	}

	int INF = INT_MAX / 10;
	vector<vector<int>> d(h, vector<int>(w, INF));
	d[sx][sy] = 0;

	queue<int> qr, qc;
	qr.push(sx); qc.push(sy);

	while(!qr.empty()){
		int r = qr.front(), c = qc.front();
		qr.pop(); qc.pop();

		if(r == gx && c == gy){
			cout << "YES" << endl;
			exit(0);
 		}

		rep(i, 4){
			int nr = r + dr[i];
			int nc = c + dc[i];

			if(0 <= nr && nr < h && 0 <= nc && nc < w){
				if(d[nr][nc] > d[r][c] + 1 && abs(g[nr][nc] - g[r][c]) <= 1){
					d[nr][nc] = d[r][c] + 1;
					qr.push(nr); qc.push(nc);
				}
			}

			nr += dr[i];
			nc += dc[i];
			if(0 <= nr && nr < h && 0 <= nc && nc < w){
				if(d[nr][nc] > d[r][c] + 1 && abs(g[nr][nc] - g[r][c]) == 0 && g[nr-dr[i]][nc-dc[i]] < g[r][c]){
					d[nr][nc] = d[r][c] + 1;
					qr.push(nr); qc.push(nc);
				}
			}
		}
	}

	cout << "NO" << endl;

}
0