[백준 11559번] Puyo Puyo

2022. 1. 13. 21:34·알고리즘 문제풀이/백준
반응형

문제 링크: https://www.acmicpc.net/problem/11559

 

11559번: Puyo Puyo

총 12개의 줄에 필드의 정보가 주어지며, 각 줄에는 6개의 문자가 있다. 이때 .은 빈공간이고 .이 아닌것은 각각의 색깔의 뿌요를 나타낸다. R은 빨강, G는 초록, B는 파랑, P는 보라, Y는 노랑이다.

www.acmicpc.net

<문제 풀이> BFS, 시뮬레이션

1. BFS로 한 뿌요와 4방향으로 연결된 동일한 뿌요의 개수를 센다 

2. 만약 동일한 뿌요의 개수가 4개 이상이면 해당 뿌요를 터트린다.

3. 모든 뿌요를 떨어뜨린다.

 

만약 터트릴 뿌요가 없으면 종료

 

 

 

<C++ 소스 코드>

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
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
 
#define X first
#define Y second
 
const int dx[4] = { 0, 0, 1, -1 };
const int dy[4] = { 1, -1, 0, 0 };
 
const int r = 12;
const int c = 6;
 
char board[r][c];
bool visited[r][c];
 
void boom(int x, int y) {
    queue<pair<int, int > > Q;
    Q.push({ x, y });
    char target = board[x][y];
    board[x][y] = '.';
    while (!Q.empty()) {
        auto cur = Q.front(); Q.pop();
        for (int dir = 0; dir < 4; dir++) {
            int nx = cur.X + dx[dir];
            int ny = cur.Y + dy[dir];
            if (nx < 0 || ny < 0 || nx >= r || ny >= c)continue;
            if (board[nx][ny] != target) continue;
            board[nx][ny] = '.';
            Q.push({ nx, ny });
        }
    }
}
 
void fall() {
    for (int y = 0; y < c; y++) {
        char moved[r];
        int idx = 0;
        for (int x = r - 1; x >= 0; x--) {
            if (board[x][y] != '.') {
                moved[idx++] = board[x][y];
            }
        }
        for (int i = 0; i < idx; i++) {
            board[r - 1 - i][y] = moved[i];
        }
        for (int i = idx; i < r; i++) {
            board[r - 1 - i][y] = '.';
        }
    }
}
 
bool bfs() {
    bool ret = false;
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (board[i][j] != '.' && !visited[i][j]) {
                queue<pair<int, int> > Q;
                Q.push({ i, j });
                visited[i][j] = true;
                int cnt = 1;
                while (!Q.empty()) {
                    auto cur = Q.front(); Q.pop();
                    for (int dir = 0; dir < 4; dir++) {
                        int nx = cur.X + dx[dir];
                        int ny = cur.Y + dy[dir];
                        if (nx < 0 || ny < 0 || nx >= r || ny >= c)continue;
                        if (visited[nx][ny] || board[nx][ny] != board[cur.X][cur.Y])continue;                    
                        visited[nx][ny] = true;
                        cnt++;
                        Q.push({ nx, ny });
                    }
                }
                if (cnt >= 4) { // 뿌요들이 4개 이상 모이면
                    ret = true;
                    boom(i, j);
                }
            }
        }
    }
    fill(&visited[0][0], &visited[r - 1][c], 0);
    return ret;
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            cin >> board[i][j];
        }
    }
    int res = 0;
    while (bfs()) {
        res++;
        fall();
    }
    cout << res;
    return 0;
}
Colored by Color Scripter
cs

 

반응형
저작자표시 (새창열림)

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 14499번] 주사위 굴리기  (0) 2022.01.15
[백준 6443번] 애너그램  (0) 2022.01.14
[백준 15686번] 치킨 배달  (0) 2022.01.09
[백준 12094번] 2048 (Hard)  (0) 2022.01.08
[백준 12100번] 2048(Easy)  (0) 2022.01.05
'알고리즘 문제풀이/백준' 카테고리의 다른 글
  • [백준 14499번] 주사위 굴리기
  • [백준 6443번] 애너그램
  • [백준 15686번] 치킨 배달
  • [백준 12094번] 2048 (Hard)
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (1)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백트랙킹
    그래프
    그리디
    BFS
    dfs
    C
    dp
    알고리즘
    Kotlin
    C++
    코틀린을 활용한 안드로이드 프로그래밍
    자료구조
    콘솔
    코틀린
    다이나믹 프로그래밍
    시뮬레이션
    콘솔 테트리스 게임
    우선순위 큐
    백준
    구현
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[백준 11559번] Puyo Puyo
상단으로

티스토리툴바