結果

問題 No.697 池の数はいくつか
ユーザー ohreitetsuohreitetsu
提出日時 2018-06-10 14:56:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,050 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 72,848 KB
実行使用メモリ 11,324 KB
最終ジャッジ日時 2023-08-16 22:18:16
合計ジャッジ時間 9,801 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
int H,W;
int ans=0;
bool not1(vector<vector<int>> A)
{
	int i,j;
	for (i=1;i<=H;i++){
		for (j=1;j<=W;j++){
			if (A[i][j]==1){
				return false;
			}
		}
	}
	return true;
}
bool check(vector<vector<int>> &A,int I,int J,int &index)
{
	if (A[I][J]==1){
		A[I][J]=index;
		if (A[I-1][J]==1){
			return check(A,I-1,J,index);
		}
		if (A[I][J-1]==1){
			return check(A,I,J-1,index);
		}
		if (A[I][J+1]==1){
			return check(A,I,J+1,index);
		}
		if (A[I+1][J]==1){
			return check(A,I+1,J,index);
		}
	}
	return false;
}
void setIndex(vector<vector<int>> &A,int index)
{
	bool loop=true;
	while (loop){
		loop=false;
		for (int i=1;i<=H;i++){
			for (int j=1;j<=W;j++){
				if (A[i][j]>0){
					if (A[i][j]==index || A[i-1][j]==index ||  A[i][j-1]==index || A[i][j+1]==index || A[i+1][j]==index){
						if (A[i][j]>0 && A[i][j]!=index ){
							A[i][j]=index;
							loop=true;
						}
						if (A[i-1][j]>0 && A[i-1][j]!=index ){
							A[i-1][j]=index;
							loop=true;
						}
						if (A[i][j-1]>0 && A[i][j-1]!=index ){
							A[i][j-1]=index;
							loop=true;
						}
						if (A[i][j+1]>0 && A[i][j+1]!=index ){
							A[i][j+1]=index;
							loop=true;
						}
						if (A[i+1][j]>0 && A[i+1][j]!=index ){
							A[i+1][j]=index;
							loop=true;
						}
					}
				}
			}
		}
	}
}
int main(int argc, char* argv[])
{
	cin>>H>>W;
	int n0=0,n1=0;
	vector<vector<int>> A(H+2,vector<int>(W+2));
	int i,j;
	for (i=1;i<=H;i++){
		for (j=1;j<=W;j++){
			cin>>A[i][j];
			if (A[i][j]==0){
				n0++;
			}else{
				n1++;
			}
		}
	}
	if (n0==H*W){
		cout<<0<<endl;
		return 0;
	}
	if (n1==H*W){
		cout<<1<<endl;
		return 0;
	}
	int index=2;
	int I=0,J=0;
	while(true){
		I=0;
		J=0;
		bool loop=true;
		for (i=1;i<=H;i++){
			for (j=1;j<=W;j++){
				if (A[i][j]==1){
					I=i;
					J=j;
					loop=false;
					break;
				}
			}
			if (!loop){
				break;
			}
		}
		if (I==0 && J==0){
			break;
		}
		check(A,I,J,index);
		setIndex(A,index);
		index++;
		ans++;

	}
	cout<<ans<<endl;
	return 0;
}
0