-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (83 loc) · 1.99 KB
/
Copy pathmain.cpp
File metadata and controls
86 lines (83 loc) · 1.99 KB
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
#include <iostream>
#include <string.h>
#include <queue>
#define WIDTH 6
#define HEIGHT 12
using namespace std;
int ans;
char map[HEIGHT+1][WIDTH+1];
bool check[HEIGHT][WIDTH], flag;
int dr[] = {0, 0, -1, 1};
int dc[] = {1, -1, 0, 0};
queue<pair<int, int>> q;
void display() {
for(int i=0; i<HEIGHT; i++) {
cout << map[i] << '\n';
}
}
void dfs(int r, int c, char colour) {
for(int d=0; d<4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if(nr < 0 || nr >= HEIGHT || nc < 0 || nc >= WIDTH) continue;
if(check[nr][nc]) continue;
if(map[nr][nc] == colour) {
check[nr][nc] = true;
q.push(make_pair(nr,nc));
dfs(nr,nc,colour);
}
}
}
void make_empty() {
bool satisfied = false;
if(q.size() >= 4) {
satisfied = true;
flag = true;
}
while(!q.empty()) {
if(satisfied) {
int r = q.front().first;
int c = q.front().second;
map[r][c] = '.';
}
q.pop();
}
}
void gravity() {
for(int i=0; i<6; i++) {
for(int j=HEIGHT-1; j>=0; j--) {
if(map[j][i] == '.') continue;
for(int k=HEIGHT-1; k>j; k--) {
if(map[k][i] != '.') continue;
map[k][i] = map[j][i];
map[j][i] = '.';
break;
}
}
}
}
int main() {
for(int i=0; i<HEIGHT; i++) {
cin >> map[i];
}
while(1) {
flag = false;
memset(check, false, sizeof(check));
for(int i=0; i<HEIGHT; i++) {
for(int j=0; j<WIDTH; j++) {
if(map[i][j] != '.' && !check[i][j]) {
check[i][j] = true;
q.push(make_pair(i,j));
dfs(i, j, map[i][j]);
make_empty();
}
}
}
if(flag) {
ans++;
} else break;
gravity();
}
cout << ans << '\n';
return 0;
}