結果

問題 No.86 TVザッピング(2)
ユーザー pekempeypekempey
提出日時 2016-02-18 06:57:29
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,379 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 160,624 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-25 12:51:02
合計ジャッジ時間 2,800 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
typedef long long ll;

int h, w;
string g[111];

char isWall(int y, int x) {
	if (y < 0 || y >= h || x < 0 || x >= w) return true;
	return g[y][x] == '#';
}

int main() {
	cin >> h >> w;
	rep(i, h) cin >> g[i];

	pair<int, int> start(1e9, 1e9);
	rep(i, h) rep(j, w) if (g[i][j] == '.') chmin(start, make_pair(i, j));

	int sy = start.first;
	int sx = start.second;

	const int dy[] = { 0, 1, 0, -1 };
	const int dx[] = { 1, 0, -1, 0 };

	int y = sy;
	int x = sx;
	int dir = 1;

	int R = 0;
	int L = 0;

	while (true) {
		while (!isWall(y + dy[dir], x + dx[dir])) {
			y += dy[dir];
			x += dx[dir];
			g[y][x] = '#';
		}
		int ld = (dir + 3) % 4;
		int rd = (dir + 1) % 4;
		if (!isWall(y + dy[rd], x + dx[rd])) {
			dir = rd;
			R++;
		} else if (!isWall(y + dy[ld], x + dx[ld])) {
			dir = ld;
			L++;
		} else break;
	}

	bool all = R <= 1;
	rep(i, h) rep(j, w) all &= g[i][j] == '#';

	cout << (all ? "YES" : "NO") << endl;

	return 0;
}
0