Red and Black

mike090529

Red and Black
探索するのは面倒だったので,総当り的に適当に済ましちゃいました.

#include <stdio.h>

#define DOT 0
#define NOT -1
#define CAN 1

int main(void)
{
	//	ファイル出力準備
	FILE	*pfI;	//	入力ファイルポインタ
	FILE	*pfO;	//	出力ファイルポインタ
	pfI = fopen("b.in", "r");
	if(pfI == NULL)
	{
		printf("ERR: input file open\n");
		return -1;
	}
	pfO = fopen("b.out","w");
	if(pfO == NULL)
	{
		printf("ERR: output file open\n");
		fclose(pfI);
		return -2;
	}

	while(1)
	{
		//
		//	入力
		//
		int w, h;	//	横と縦の長さ
		int r[23][23];	//	部屋
		//	幅と高さ
		fscanf(pfI, "%d %d ", &w, &h);
		printf("%d %d\n", w, h);
		if(w == 0 && h == 0)	//	終了処理
			break;

		//	部屋
		for(int y = 0; y < 23; y++)
		{
			for(int x = 0; x < 23; x++)
			{
				r[y][x] = NOT;
			}
		}
		for(int y = 1; y < h+1; y++)	
		{
			for(int x = 1; x < w+1; x++)
			{
				char c;	//	入力
				fscanf(pfI, "%c ", &c);
				printf("%c", c);
				switch(c)
				{
				case '.':
					r[y][x] = DOT;
					break;
				case '#':
					r[y][x] = NOT;
					break;
				case '@':
					r[y][x] = CAN;
					break;
				default:
					printf("*");
					break;
				}
			}
			printf("\n");
		}

		//
		//	処理
		//
		int t = 1;	//	勧めるタイルの数
		bool f;	//	もう更新できなかったら抜ける
		for(int y = 1; y < 23; y++)
		{
			f = false;
			for(int x = 1; x < 23; x++)
			{
				if(r[y][x] == CAN)
				{
					//	上
					if(r[y-1][x] == DOT)
					{
						r[y-1][x] = CAN;
						t++;
						f = true;
					}
					//	下
					if(r[y+1][x] == DOT)
					{
						r[y+1][x] = CAN;
						t++;
						f = true;
					}
					//	右
					if(r[y][x+1] == DOT)
					{
						r[y][x+1] = CAN;
						t++;
						f = true;
					}
					//	左
					if(r[y][x-1] == DOT)
					{
						r[y][x-1] = CAN;
						t++;
						f = true;
					}
					if(f)	//	タイルを埋めてたら最初からやり直し
						y = 0;
				}
			}
		}

		//
		//	出力
		//
		fprintf(pfO, "%d\n", t);

		// デバッグ
		for(int y = 0; y < h+2; y++)
		{
			for(int x = 0; x < w+2; x++)
			{
				switch(r[y][x])
				{
				case DOT:
					printf("%c", '.');
					break;
				case NOT:
					printf("%c", '#');
					break;
				case CAN:
					printf("%c", '@');
					break;
				}
			}
			printf("\n");
		}
		printf("%d\n\n", t);
	}

	//ファイルポインタを閉じる
	fclose(pfI);
	fclose(pfO);

	return 0;
}