Minesweeper

Posted by 진영 Info.log : 2005/11/29 12:34
Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can't remember. The goal of the game is to find where all the mines are located within a M x N field.
The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*'' character. If we represent the same field by the hint numbers described above, we end up with the field on the right:


*...
....
.*..
....



*100
2210
1*10
1110




Input
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field.

Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.


Output
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.



Sample Input

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0



Sample Output

Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100




#include  < stdio.h>

#define MAX_SIZE 100

int main()
{
	int i, j;
	int m, n;
	char board[MAX_SIZE][MAX_SIZE];
	int cycle=1;

	while(scanf("%d %d", &m, &n)==2)
	{
		getchar();

		if (0==m && 0==n)
			return 0;
		else if (cycle!=1)
			printf("\n\n");

		if (m>MAX_SIZE || n>MAX_SIZE || m < 1 || n < 1)
			continue;

		// input
		for (i=0; i < m; i++)
		{
			for (j=0; j < n; j++)
			{
				scanf("%c", &board[i][j]);
				
				if (board[i][j]=='.')
					board[i][j]='0';
			}
			getchar();
		}

		// translate mine
		for (i=0; i < m; i++)
		{
			for (j=0; j < n; j++)
			{				
				if (board[i][j]=='*')
				{
					// above mine
					if (i>0)
					{
						if (j>0)
						{
							if (board[i-1][j-1]!='*')
								board[i-1][j-1]++;
						}
						if (j+1 < n)
						{
							if (board[i-1][j+1]!='*')
								board[i-1][j+1]++;
						}
						if (board[i-1][j]!='*')
							board[i-1][j]++;
					}
					// beside mine
					if (j>0)
					{
						if (board[i][j-1]!='*')
							board[i][j-1]++;
					}
					if (j+1 < n)
					{
						if (board[i][j+1]!='*')
							board[i][j+1]++;
					}
					// under mine
					if (i+1 < m)
					{
						if (j>0)
						{
							if (board[i+1][j-1]!='*')
								board[i+1][j-1]++;
						}
						if (j+1 < n)
						{
							if (board[i+1][j+1]!='*')
								board[i+1][j+1]++;
						}
						if (board[i+1][j]!='*')
							board[i+1][j]++;
					}

				}
			}
		}
		// output
		printf("Field #%d:", cycle++);
		for (i=0; i < m; i++)
		{
			printf("\n");
			for (j=0; j < n; j++)
			{
				printf("%c", board[i][j]);
			}
		}
	}
	return 0;
}
 «이전 1 ... 372 373 374 375 376 377 378 379 380  다음»