다시 풀어보자.. 10189 Minesweeper 쿠쿠






Problem Link : http://acm.uva.es/p/v101/10189.html










■ Problem
– 입력된 지뢰필드를 펼쳐보아라

■ Solution
– 한칸한칸 지나가면서 주변의 지뢰수를 세어 기록

■ Critical Input
– 간단 문제지만 P.E에러가 많이 났음…
   왜 났었지?? 어라 몇일 밖에 안지났는데… ^^;; 기억나면 수정.. 쿠쿠





[ Source Code ]

#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;

}


댓글 남기기