Programming Challenges – 지뢰찾기 (Minesweeper)

문제 <- 클릭

재미있다.! 하지만 채점 로봇이 너무… 공백 줄 하나 때문에 걸고 넘어져서 실망이다.

#include 
#define MAX 100

int main() {
	char field[MAX + 1][MAX + 1];
	long row, col;
	int fieldNumber = 1;
	while (scanf("%ld %ld", &col, &row) == 2) {
		if (col == 0 && row == 0) //0 0이면 종료
			break;
		for (int i = 0; i <= row; ++i) //마지막줄 다음줄만 초기화 한다
			field[col][i] = 0;

		for (int i = 0; i < col; ++i) {
			scanf("%s", field[i]);
			field[i][row] = 0;	//입력 받고 마지막 글자 다음 글자 초기
		}
		if (fieldNumber > 1)	//이거때매 애먹음.. 마지막에 공백이 없어야 채점이 됨
			putchar('n');

		printf("Field #%d:n", fieldNumber++);

		for (int colIndex = 0; colIndex < col; ++colIndex) {
			for (int rowIndex = 0; rowIndex < row; ++rowIndex) {
				if (field[colIndex][rowIndex] == '*') {
					putchar('*');
				} else if (field[colIndex][rowIndex] == '.') {
					int bombCounter = 0;
					for (int relativeCol = -1; relativeCol <= 1; ++relativeCol) {
						for (int relativeRow = -1; relativeRow <= 1; ++relativeRow) { //주변을 탐색
							int checkCol = colIndex + relativeCol;
							int checkRow = rowIndex + relativeRow;
							if (checkCol >= 0 && checkRow >= 0 && field[checkCol][checkRow] == '*')
								bombCounter++; //폭탄의 갯수를 카운트함
						}
					}
					printf("%d", bombCounter);
				}
			}
			putchar('n');
		}
	}
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.