#include <stdio.h>
int checkMine ( char [100][100], int, int, int, int );
int main ( void )
{
char input[100][100];
int fieldsize_x, fieldsize_y, output[100][100];
int count_x, count_y, fieldcount = 0;
while ( 1 ) {
scanf ( “%d %d“, &fieldsize_x, &fieldsize_y );
getchar ();
if ( fieldsize_x == 0 && fieldsize_y == 0 ) break;
for ( count_x = 0 ; count_x < fieldsize_x ; count_x++ ) {
for ( count_y = 0 ; count_y < fieldsize_y ; count_y++ ) {
scanf ( “%c“, &input[count_x][count_y] );
}
getchar ();
}
for ( count_x = 0 ; count_x < fieldsize_x ; count_x++ ) {
for ( count_y = 0 ; count_y < fieldsize_y ; count_y++ ) {
if ( input[count_x][count_y] == ‘*’ ) {
output[count_x][count_y] = –1;
continue;
}
else {
output[count_x][count_y] = checkMine ( input, count_x, count_y,
fieldsize_x, fieldsize_y );
}
}
}
if ( fieldcount != 0 ) printf ( “\n“ );
printf ( “Field #%d:\n“, fieldcount + 1 );
for ( count_x = 0 ; count_x < fieldsize_x ; count_x++ ) {
for ( count_y = 0; count_y < fieldsize_y ; count_y++ ) {
if ( output[count_x][count_y] == –1 ) {
putchar ( ‘*’ );
}
else {
printf ( “%d“, output[count_x][count_y] );
}
}
printf ( “\n“ );
}
fieldcount++;
}
}
// Check around Mine’s Number
int checkMine ( char data[100][100], int x, int y, int max_x, int max_y )
{
int numOfMine = 0;
if ( x-1 >= 0 && y-1 >= 0 && data[x-1][y-1] == ‘*’) numOfMine++;
if ( y-1 >= 0 && data[x][y-1] == ‘*’ ) numOfMine++;
if ( x+1 < max_x && y-1 >= 0 && data[x+1][y-1] == ‘*’ ) numOfMine++;
if ( x-1 >= 0 && data[x-1][y] == ‘*’ ) numOfMine++;
if ( x+1 < max_x && data[x+1][y] == ‘*’ ) numOfMine++;
if ( x-1 >= 0 && y+1 < max_y && data[x-1][y+1] == ‘*’ ) numOfMine++;
if ( y+1 < max_y && data[x][y+1] == ‘*’ ) numOfMine++;
if ( x+1 < max_x && y+1 < max_y && data[x+1][y+1] == ‘*’ ) numOfMine++;
return numOfMine;
}
댓글 남기기