-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path723. Candy Crush.cpp
65 lines (63 loc) · 1.91 KB
/
723. Candy Crush.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
class Solution {
public:
vector<vector<int>> candyCrush(vector<vector<int>>& board) {
int m = board.size(), n = board[0].size();
while (crush(board, m, n)) {
drop(board, m, n);
}
return board;
}
bool crush(vector<vector<int>>& board, int m, int n) {
vector<vector<int>>copy(board);
bool canBeCrushed = false;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 0) {
continue;
}
int r = i, c = j, count = 0;
// check horizontal
while (c < n && board[i][c] == board[i][j]) {
++count;
++c;
}
if (count >= 3) {
canBeCrushed = true;
while (c > j) {
copy[i][--c] = 0;
}
}
count = 0;
// check vertical
while (r < m && board[r][j] == board[i][j]) {
++count;
++r;
}
if (count >= 3) {
canBeCrushed = true;
while (r > i) {
copy[--r][j] = 0;
}
}
}
}
board = copy;
return canBeCrushed;
}
void drop(vector<vector<int>>& board, int m, int n) {
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 0) {
// drop
int r = i, c = j;
while (r >= 0 && board[r][c] == 0) {
--r;
}
if (r >= 0) {
swap(board[i][j], board[r][c]);
}
}
}
}
}
};