結果

問題 No.86 TVザッピング(2)
ユーザー koyumeishikoyumeishi
提出日時 2014-12-05 02:12:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,472 bytes
コンパイル時間 866 ms
コンパイル使用メモリ 82,540 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 08:50:20
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge11 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 4 ms
4,384 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 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 WA -
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>

using namespace std;

//l b r u
int x[] = {-1,0,1,0};
int y[] = {0,1,0,-1};

int dfs(vector<vector<int> > &G, vector<bool> &visit, int pos){
	visit[pos] = true;
	int ret = 1;
	for(int i=0; i<G[pos].size(); i++){
		int next = G[pos][i];
		if(visit[next]) continue;
		ret += dfs(G, visit, next);
	}
	return ret;
}

int dfs_(vector<string> &G, int n, int m, vector<bool> &visit, int pos, int start, int toward, bool right){
	visit[pos] = true;
	int ret = 1;
	//進めるだけ進む
	//すでに訪れた点にぶつかるとそこで止まる。
	int xx = pos%m + x[toward];
	int yy = pos/m + y[toward];
	while(xx>=0 && xx < m && yy>=0 && yy < n){
		if(G[yy][xx]=='.' && !visit[xx+yy*m]){
			pos = xx + yy*m;
			visit[pos] = true;
			ret++;
			
			xx = pos%m + x[toward];
			yy = pos/m + y[toward];
		}else{
			break;
		}
	}

	//次がstart地点なら終了
	if(xx>=0 && xx < m && yy>=0 && yy < n && xx+yy*m == start){
		return ret;
	}

	//今いる場所から左に曲がる
	int next_x = pos%m + x[(toward+1)%4];
	int next_y = pos/m + y[(toward+1)%4];
	int next = next_x + next_y*m;

	//曲がった先の場所に進めるなら進む
	if(next_x>=0 && next_x < m && next_y>=0 && next_y < n && !visit[next] && G[next_y][next_x]=='.' ){
		return ret + dfs_(G,n,m,visit, next, start, (toward+1)%4, right);
	}else{
		//次でstart地点に戻っていれば終了
		if(next_x>=0 && next_x < m && next_y>=0 && next_y < n && next == start){
			return ret;
		}
	}

	//右に1回だけ曲がれる
	if(right==false){
		right = true;
		
		next_x = pos%m + x[(toward+3)%4];
		next_y = pos/m + y[(toward+3)%4];
		next = next_x + next_y*m;

		//曲がった先の場所に進めるなら進む
		if(next_x>=0 && next_x < m && next_y>=0 && next_y < n && !visit[next] && G[next_y][next_x]=='.' ){
			return ret + dfs_(G,n,m,visit, next, start, (toward+3)%4, right);
		}else{
			//次でstart地点に戻っていれば終了
			if(next_x>=0 && next_x < m && next_y>=0 && next_y < n && next == start){
				return ret;
			}
		}
	}
	
	//start地点に戻っていないのに行く先がなかったら不適切
	return -1;
	
}

int main(){
	int n,m;
	cin >> n >> m;
	vector<string> v(n);
	for(int i=0; i<n; i++){
		cin >> v[i];
	}

	vector<vector<int> > G(n*m);
	vector<int> s;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(v[i][j] == '.'){
				s.push_back(i*m + j);
				for(int k=0; k<4; k++){
					int xx = j + x[k];
					int yy = i + y[k];
 					if(xx<0 || xx >= m || yy<0 || yy >= n) continue;
					if(v[yy][xx] == '.'){
						G[m*i + j].push_back(yy*m + xx);
					}
				}
			}
		}
	}

	vector<bool> visit(n*m, false);
	bool valid = dfs(G, visit, s[0]) == s.size();
	if(valid == false){
		cout << "NO" << endl;
		return 0;
	}

	valid = false;
	//最初に追加された点は必ず角になるので探索は1回で十分
	for(int i=0; i<1 && valid == false; i++){
		int start = s[i];
		int start_x = s[i]%m;
		int start_y = s[i]/m;
		for(int j=0; j<4 && valid == false; j++){
			fill(visit.begin(), visit.end(), false);
			int cnt = dfs_(v, n, m, visit, start, start, j, false);
			valid |= cnt == s.size();
		}
	}
	cout << (valid?"YES":"NO") << endl;
	return 0;
}
0