본문 바로가기

알고리즘/BOJ

11559

11559번 - Puyo Puyo


시뮬레이션 문제.


dfs를 돌면서, 길이가 4이상인 값들은 맵에서 전부 '.' 로 바꿔준다.


era 라는 함수를 써서 dfs와 똑같이 돌면서 값을 . 으로 바꿔주는 함수를 이용했다.


dfs를 이용해서는 return 을 이용해서 길이 체크하는 코딩이 생각이 나지 않아서, 값이 작아서 두번 이용했다.


그리고 다시 전체를 돌면서 이동시켜준다.


이때는 맨 밑 바닥부터 시작해서 위에 값들과 swap 시켜주는 방식으로 문제를 풀었다.


<정답 코드>


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
 
using namespace std;
 
int cnt, len;
char map[12][6];
bool chk[12][6];
int dx[4= { 0,0,1,-1 };
int dy[4= { 1,-1,0,0 };
 
void init()
{
    cnt = 0;
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            chk[i][j] = false;
        }
    }
}
void dfs(int x, int y,char c)
{
    len++;
    chk[x][y] = true;
 
    for (int d = 0; d < 4; d++)
    {
        int nx = x + dx[d];
        int ny = y + dy[d];
        if (nx >= 0 && ny >= 0 && nx < 12 && ny < 6 && !chk[nx][ny] && map[nx][ny]==c)
        {
            dfs(nx, ny,c);
        }
    }
}
 
void era(int x, int y, char c)
{
    map[x][y] = '.';
 
    for (int d = 0; d < 4; d++)
    {
        int nx = x + dx[d];
        int ny = y + dy[d];
        if (nx >= 0 && ny >= 0 && nx < 12 && ny < 6 && map[nx][ny] == c)
        {
            era(nx, ny, c);
        }
    }
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 6; j++) {
            cin >> map[i][j];
        }
    }
 
    int combo = 0;
 
    while (true)
    {
        init();
        // 지우기
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 6; j++) {
                if (map[i][j] != '.' && !chk[i][j])
                {
                    len = 0;
                    dfs(i, j, map[i][j]);
                    if (len>=4)
                    {
                        cnt++;
                        era(i, j, map[i][j]);
                    }
                }
            }
        }
        
        //아래로 이동 시키기
        for (int j = 0; j < 6; j++)
        {
            for (int i = 11; i >= 0; i--)
            {
                if (map[i][j] == '.')
                {
                    for (int u = i - 1; u >= 0; u--)
                    {
                        if (map[u][j] != '.')
                        {
                            char tmp = map[i][j];
                            map[i][j] = map[u][j];
                            map[u][j] = tmp;
                            break;
                        }
                    }
                }
            }
        }
 
        if (cnt == 0)
        {
            break;
        }
        else
        {
            combo++;
        }
    }
 
    cout << combo << "\n";
 
 
    return 0;
}
cs


반응형

'알고리즘 > BOJ' 카테고리의 다른 글

1799번  (4) 2018.04.13
1063번  (0) 2018.04.11
1793번  (0) 2018.03.25
15559번  (0) 2018.03.25
2484번  (0) 2018.03.25