[BOJ] 11559 Puyo Puyo

Time Lapse :23min 19sec

11559.cpp

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
#include <iostream>
#include <queue>
using namespace std;

char MAP[12][6];
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};

void down(){
for(int j=0;j<6;j++){
for(int i=10;i>=0;i--){ //
if(MAP[i][j]!='.'){
int k = i;
bool flag = false;
while(MAP[k+1][j]=='.'){
++k;
flag = true;
}
if(flag) {
MAP[k][j] = MAP[i][j];
MAP[i][j] = '.';
}
}
}
}
}

bool play(){
bool flag = false;
bool visited[12][6] = {false, };
for(int i=0;i<12;i++){
for(int j=0;j<6;j++){
if(MAP[i][j]=='.'){
visited[i][j]=true;
}
else{
int cnt = 1;
queue<pair<int, int>> q;
queue<pair<int, int>> dq;
q.push(make_pair(i,j));
dq.push(make_pair(i,j));
visited[i][j]=true;
while(!q.empty()){
int x = q.front().second;
int y = q.front().first;
q.pop();
for(int k=0;k<4;k++){
int nx = x + dx[k];
int ny = y + dy[k];
if(0<=nx&&nx<6 && 0<=ny&&ny<12){
if(MAP[y][x]==MAP[ny][nx]&&!visited[ny][nx]){
++cnt;
q.push(make_pair(ny,nx));
dq.push(make_pair(ny,nx));
visited[ny][nx] = true;
}
}
}
}
if(cnt>=4){
flag = true;
while(!dq.empty()){
int x = dq.front().second;
int y = dq.front().first;
dq.pop();
MAP[y][x] = '.';
}
}
}
}
}
return flag;
}
int main(void){
for(int i=0;i<12;i++){
for(int j=0;j<6;j++){
cin>>MAP[i][j];
}
}
int answer = 0;
while(play()){
++answer;
down();
}
cout<<answer<<endl;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/11559/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.