結果

問題 No.348 カゴメカゴメ
ユーザー gasin
提出日時 2016-03-05 12:59:24
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,762 bytes
コンパイル時間 1,761 ms
コンパイル使用メモリ 168,788 KB
実行使用メモリ 36,156 KB
最終ジャッジ日時 2024-09-24 14:18:23
合計ジャッジ時間 5,343 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef pair<int,int> P;

bool g[1000][1000];
int id[1000][1000];
bool did[1000000];
bool checked[1000000];
bool saw[1000000];
int num[1000000];
int n, m;
int x = 0;
vector<int> edge[1000000];

int func(int a, int type){
	saw[a] = true;
	int ret = 0;
	if(type == 0) ret += num[a];
	rep(i,edge[a].size()){
		int e = edge[a][i];
		if(checked[e]) continue;
		if(saw[e]) continue;
		ret += func(e,(type+1)%2);
	}
	return ret;
}

int main(){
	cin >> n >> m;
	rep(i,n){
		string str;
		cin >> str;
		rep(j,m){
			if(str[j] == 'x') g[i][j] = true;
			else g[i][j] = false;
		}
	}
	rep(i,n) rep(j,m){
		if(!g[i][j]) continue;
		if(id[i][j] != 0) continue;
		x++;
		int cnt = 1;
		queue<P> que;
		que.push(P(i,j));
		id[i][j] = x;
		while(true){
			if(que.size() == 0) break;
			P q = que.front();
			que.pop();
			for(int k = -1; k <= 1; k++) for(int l = -1; l <= 1; l++){
				if(0>q.first+k||q.first+k>=n) continue;
				if(0>q.second+l||q.second+l>=m) continue;
				if(id[q.first+k][q.second+l] != 0 || g[q.first+k][q.second+l] == false) continue;
				que.push(P(q.first+k,q.second+l));
				id[q.first+k][q.second+l] = x;
				cnt++;
			}
		}
		num[x] = cnt;
	}
	rep(i,n) rep(j,m){
		if(id[i][j] == 0) continue;
		if(did[id[i][j]]) continue;
		did[id[i][j]] = true;
		for(int u = i-1; u >= 0; u--){
			if(id[u][j] == true && id[u][j] != id[i][j]){
				edge[id[i][j]].push_back(id[u][j]);
				edge[id[u][j]].push_back(id[i][j]);
			}
		}
	}
	int ans = 0;
	rep(i,x){
		int t = i+1;
		if(checked[t]) continue;
		int f[2];
		rep(u,2){
			rep(j,x) saw[j+1] = checked[j+1];
			f[u] = func(t,u);
		}
		ans += max(f[0],f[1]);
		rep(j,x) checked[j+1] = saw[j+1];
	}
	cout << ans << endl;
}
0