結果

問題 No.971 いたずらっ子
ユーザー polylogKpolylogK
提出日時 2020-01-17 21:34:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,660 bytes
コンパイル時間 1,809 ms
コンパイル使用メモリ 179,148 KB
実行使用メモリ 73,848 KB
最終ジャッジ日時 2023-09-08 01:47:56
合計ジャッジ時間 10,003 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 781 ms
73,320 KB
testcase_04 AC 736 ms
69,384 KB
testcase_05 AC 773 ms
73,384 KB
testcase_06 AC 568 ms
55,072 KB
testcase_07 WA -
testcase_08 AC 187 ms
21,644 KB
testcase_09 AC 174 ms
20,852 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int h, w; scanf("%d%d", &h, &w); std::vector<std::string> s(h);
	for(int i = 0; i < h; i++) cin >> s[i];
	
	auto dp = make_v<i64>(h, w), dp2 = make_v<i64>(h, w);
	for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) dp[i][j] = 1LL << 60;
	for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) dp2[i][j] = 1LL << 60;
	dp[0][0] = dp2[0][0] = 0;
	
	std::vector<int> dx = {0, -1, 0, 1};
	std::vector<int> dy = {1, 0, -1, 0};
	using P = std::pair<i64, std::pair<int, int>>;
	std::priority_queue<P, std::vector<P>, std::greater<P>> qu; qu.push({0, {0, 0}});
	while(!qu.empty()) {
		auto p = qu.top(); qu.pop();
		int x = p.second.first, y = p.second.second, from = p.first;
		if(dp[x][y] < from) continue;
		
		for(int k = 0; k < 4; k++) {
			int nx = x + dx[k], ny = y + dy[k];
			if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
			if(s[nx][ny] == '.') {
				if(dp[nx][ny] <= dp[x][y] + 1) continue;
				dp[nx][ny] = dp[x][y] + 1;
				dp2[nx][ny] = dp2[x][y] + 1;
				qu.push({dp[nx][ny], {nx, ny}});
			} else if(s[nx][ny] == 'k') {
				if(dp[nx][ny] <= dp[x][y] + dp2[x][y] + 2) continue;
				dp[nx][ny] = dp[x][y] + dp2[x][y] + 2;
				dp2[nx][ny] = dp2[x][y] + 1;
				qu.push({dp[nx][ny], {nx, ny}});
			}
		}
	}
	
	printf("%lld\n", dp[h - 1][w - 1]);
	return 0;
}
0