結果

問題 No.2412 YOU Grow Bigger!
ユーザー square1001square1001
提出日時 2023-08-11 22:00:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 6,000 ms
コード長 1,431 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 85,528 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-13 13:21:00
合計ジャッジ時間 5,057 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 331 ms
4,380 KB
testcase_04 AC 339 ms
4,380 KB
testcase_05 AC 234 ms
4,380 KB
testcase_06 AC 291 ms
4,380 KB
testcase_07 AC 336 ms
4,380 KB
testcase_08 AC 261 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 90 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 160 ms
4,376 KB
testcase_14 AC 97 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 98 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 40 ms
4,380 KB
testcase_22 AC 132 ms
4,376 KB
testcase_23 AC 76 ms
4,380 KB
testcase_24 AC 164 ms
4,380 KB
testcase_25 AC 64 ms
4,376 KB
testcase_26 AC 77 ms
4,380 KB
testcase_27 AC 49 ms
4,380 KB
testcase_28 AC 105 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const vector<int> dx = { -1, -1, -1, 0, 0, 1, 1, 1 };
const vector<int> dy = { -1, 0, 1, -1, 1, -1, 0, 1 };

bool reachable(int H, int W, const vector<string>& S) {
	vector<vector<bool> > flag(H - 2, vector<bool>(W - 2, true));
	for (int i = 0; i < H - 2; i++) {
		for (int j = 0; j < W - 2; j++) {
			for (int k = 0; k < 3; k++) {
				for (int l = 0; l < 3; l++) {
					if (S[i + k][j + l] == '#') {
						flag[i][j] = false;
					}
				}
			}
		}
	}
	vector<vector<bool> > vis(H - 2, vector<bool>(W - 2, false));
	auto dfs = [&](auto& dfs, int x, int y) -> void {
		vis[x][y] = true;
		for (int i = 0; i < 8; i++) {
			int tx = x + dx[i];
			int ty = y + dy[i];
			if (0 <= tx && tx < H - 2 && 0 <= ty && ty < W - 2 && flag[tx][ty] && !vis[tx][ty]) {
				dfs(dfs, tx, ty);
			}
		}
	};
	dfs(dfs, 0, 0);
	return vis[H - 3][W - 3];
}

int main() {
	int H, W;
	cin >> H >> W;
	vector<string> S(H);
	for (int i = 0; i < H; i++) {
		cin >> S[i];
	}
	if (!reachable(H, W, S)) {
		cout << 0 << endl;
	}
	else {
		bool flag = false;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (S[i][j] == '.' && !(i < 3 && j < 3) && !(i >= H - 3 && j >= W - 3)) {
					S[i][j] = '#';
					if (!reachable(H, W, S)) {
						flag = true;
					}
					S[i][j] = '.';
				}
			}
		}
		cout << (flag ? 1 : 2) << endl;
	}
	return 0;
}
0