본문 바로가기
#DevStudy/문제풀이

[P.C] 2. 지뢰찾기 (Minesweeper)

by 검은_백조 2016. 9. 23.

2. Minesweeper

지뢰찾기

(* = 지뢰  . = 빈공간)

지뢰 주변 8방향에 지뢰의 개수가 표시되기.



입력

4 4

*...

....

.*..

....


출력

Field #1:

*100

2210

1*10

1110





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
 
void SearchMine(char* field, int x, int y) {
    for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++) {
            int pos = i * x + j;
            if (field[pos] == '*'continue;
 
            int count = 0;
 
            if (j > 0) {
                if (field[pos - 1== '*') count++;
                if (i > 0) {
                    if (field[pos - x - 1== '*') count++;
                }
                if (i < y - 1) {
                    if (field[pos + x - 1== '*') count++;
                }
            }
            if (j < x - 1) {
                if (field[pos + 1== '*') count++;
                if (i > 0) {
                    if (field[pos - x + 1== '*') count++;
                }
                if (i < y - 1) {
                    if (field[pos + x + 1== '*') count++;
                }
            }
            if (i > 0) {
                if (field[pos - x] == '*') count++;
            }
            if (i < y - 1) {
                if (field[pos + x] == '*') count++;
            }
 
            field[pos] = count + '0';
        }
    }
}
 
int main() {
    char* field;
    int x, y;
    int n = 1;
 
    while (true) {
        scanf("%d %d\n"&x, &y);
        if (x == 0 || y == 0break;
        field = (char*)malloc(sizeof(char* x * y);
 
        for (int i = 0; i < y; i++) {
            for (int j = 0; j <= x; j++) {
                scanf("%c"&field[i*+ j]);
            }
        }
 
        SearchMine(field, x, y);
 
        printf("Field #%d\n", n++);
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                printf("%c", field[i*+ j]);
            }
            printf("\n");
        }
    }
 
 
    return 1;
}
cs


'#DevStudy > 문제풀이' 카테고리의 다른 글

[Baekjoon] 2579 - 계단오르기  (0) 2016.10.11
[Baekjoon] 1149 - RGB  (0) 2016.10.11
[P.C] 3. 여행 (The trip)  (0) 2016.09.23
[Baekjoon] 5393 콜라츠  (0) 2016.09.22
[P.C] 1. 3n+1 문제  (0) 2016.09.22

댓글