結果

問題 No.2913 二次元距離空間
ユーザー eve__fuyuki
提出日時 2024-10-07 02:03:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,265 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 209,460 KB
最終ジャッジ日時 2025-02-24 16:15:18
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

void fast_io() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
}

int main() {
	fast_io();
	int h, w;
	cin >> h >> w;
	vector<string> s(h);
	for (int i = 0; i < h; i++) {
		cin >> s[i];
	}
	const int INF = 1e9;
	int dx[] = {1, -1, 0, 0};
	int dy[] = {0, 0, 1, -1};
	auto is_in = [&](int x, int y) {
		return 0 <= x && x < h && 0 <= y && y < w;
	};
	vector<vector<pair<int, int>>> dist(h,
										vector<pair<int, int>>(w, {INF, INF}));
	dist[0][0] = {0, 0};
	using P = tuple<int, int, int, int>;
	priority_queue<P, vector<P>, greater<P>> pq;
	pq.push({0, 0, 0, 0});
	while (!pq.empty()) {
		auto [dh, dv, x, y] = pq.top();
		pq.pop();
		if (dist[x][y] < make_pair(dh, dv)) {
			continue;
		}
		for (int i = 0; i < 4; i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (!is_in(nx, ny) || s[nx][ny] == '#') {
				continue;
			}
			int ndh = dh + (i >= 2);
			int ndv = dv + (i < 2);
			if (dist[nx][ny] > make_pair(ndh, ndv)) {
				dist[nx][ny] = {ndh, ndv};
				pq.push({ndh, ndv, nx, ny});
			}
		}
	}
	if (dist[h - 1][w - 1] == make_pair(INF, INF)) {
		cout << "No" << endl;
	} else {
		cout << "Yes" << endl;
		cout << dist[h - 1][w - 1].first << " " << dist[h - 1][w - 1].second
			 << endl;
	}
}
0