結果

問題 No.86 TVザッピング(2)
ユーザー koyumeishikoyumeishi
提出日時 2014-12-05 01:30:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,666 bytes
コンパイル時間 579 ms
コンパイル使用メモリ 78,616 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 08:49:00
合計ジャッジ時間 1,875 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using namespace std;

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){
	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+yy*m == start){
		return ret;
	}

	//今いる場所から左に曲がる
	int next_x = pos%m + x[(toward+3)%4];
	int next_y = pos/m + y[(toward+3)%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+3)%4);
	}else{
		//進めないならおしまい
		//次でstart地点に戻っていれば終了
		if(next == start) return ret;
		//そうでなければ不適切
		else return -1;
	}
	
}

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

	//ごめんなさい
	cout << "YES" << endl;
	return 0;
	
	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;
	for(int i=0; i<s.size() && valid == false; i++){
		int start = s[i];
			
		for(int j=0; j<4; j++){
			fill(visit.begin(), visit.end(), false);
			int cnt = dfs_(v, n, m, visit, start, start, j);
			valid |= cnt == s.size();
		}
	}
	cout << (valid?"YES":"NO") << endl;
	return 0;
}
0