結果

問題 No.13 囲みたい!
ユーザー myantamyanta
提出日時 2017-04-30 23:26:36
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,536 bytes
コンパイル時間 1,461 ms
コンパイル使用メモリ 46,652 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-12 00:04:42
合計ジャッジ時間 4,468 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 3 ms
4,348 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int remove_connection(int, int)’:
main.cpp:68:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

ソースコード

diff #

#include<cstdio>
#include<vector>


using namespace std;


int w, h;
vector<vector<int> > f, c;


int calc_connection(void)
{
	int vx[]={ 1, 0,-1, 0};
	int vy[]={ 0,-1, 0, 1};
	int x, y, n, i, ret=0;

	for(y=1;y<=h;y++)
	{
		for(x=1;x<=w;x++)
		{
			if(f[y][x]==0) continue;

			c[y][x]=0;
			n=f[y][x];
			for(i=0;i<4;i++)
			{
				if(n==f[y+vy[i]][x+vx[i]])
				{
					c[y][x]++;
					ret=1;
				}
			}
			if(c[y][x]==0) f[y][x]=0;
		}
	}
	return ret;
}


int remove_connection(int x, int y)
{
	int vx[]={ 1, 0,-1, 0};
	int vy[]={ 0,-1, 0, 1};
	int i, n, nx, ny;

	n=f[y][x];
	for(;;)
	{
		c[y][x]--;
		if(c[y][x]<=0)
		{
			f[y][x]=0;
		}
		for(i=0;i<4;i++)
		{
			nx=x+vx[i];
			ny=y+vy[i];
			if(f[ny][nx]>0 && c[ny][nx]<2)
			{
				break;
			}
		}
		if(i>=4) break;
		x=nx;
		y=ny;
	}
}


int solve(void)
{
	int x, y, is_removed=1, ret=0;

	for(;is_removed;)
	{
		ret=calc_connection();
		is_removed=0;
		for(y=1;y<=h;y++)
		{
			for(x=1;x<=w;x++)
			{
				if(c[y][x]==1 && f[y][x]>0)
				{
					remove_connection(x, y);
					is_removed=1;
				}
			}
		}
	}
	return ret;
}

int main(void)
{
	int i, x, y;

	while(scanf("%d%d", &w, &h)==2)
	{
		f.clear();
		f.resize(h+2);
		for(i=0;i<f.size();i++)
		{
			f[i].resize(w+2);
		}
		c=f;

		for(y=1;y<=h;y++)
		{
			for(x=1;x<=w;x++)
			{
				scanf("%d", &f[y][x]);
			}
		}
		printf("%s\n", solve()?"possible":"impossible");
/*
		for(y=1;y<=h;y++)
		{
			for(x=1;x<=w;x++)
			{
				printf("%2d[%2d] ", f[y][x], c[y][x]);
			}
			printf("\n");
		}
		printf("\n");
*/
	}

	return 0;
}
0