Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <cstring>
- #define rangecheck(a,b) a<0 || a>=12 || b<0 || b>=6
- using namespace std;
- struct point {
- int r, c;
- char cnt;
- };
- queue<point> q, bamm_q;
- char map[13][7];
- bool visited[13][7];
- bool flag;
- int dr[] = { -1,0,1,0 };
- int dc[] = { 0,1,0,-1 };
- int cntt = 0;
- int ans = 0;
- void bfs(int r, int c) {
- point tmp = { r,c,0 };
- q.push(tmp);
- visited[r][c] = true;
- int nr, nc;
- while (!q.empty()) {
- r = q.front().r;
- c = q.front().c;
- bamm_q.push(q.front());
- q.pop();
- cntt++;
- //printf("%d %d %d %c\n", r, c, cntt, map[r][c]);
- for (int i = 0; i < 4; i++) {
- nr = r + dr[i];
- nc = c + dc[i];
- if (rangecheck(nr, nc)) continue;
- if (map[nr][nc] == map[r][c] && !visited[nr][nc]) {
- visited[nr][nc] = true;
- q.push({ nr,nc,0 });
- }
- }
- }
- //printf("\n");
- }
- void print() {
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 6; j++) {
- printf("%c", map[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void bamm() {
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 6; j++) {
- if (map[i][j] != '.') {
- cntt = 0;
- bfs(i, j);
- if (cntt >= 4) {
- flag = true;
- while (!bamm_q.empty()) {
- map[bamm_q.front().r][bamm_q.front().c] = '.';
- bamm_q.pop();
- }
- }
- else {
- while (!bamm_q.empty()) {
- bamm_q.pop();
- }
- }
- }
- }
- }
- }
- void down() {
- for (int j = 0; j < 6; j++) {
- for (int i = 11; i >= 0; i--) {
- if (map[i][j] != '.') {
- q.push({ i,j,map[i][j] });
- map[i][j] = '.';
- }
- }
- int ii = 11;
- while (!q.empty()) {
- map[ii--][j] = q.front().cnt;
- q.pop();
- }
- }
- }
- void go() {
- flag = true;
- while (flag) {
- flag = false;
- bamm();
- down();
- memset(visited, false, sizeof(visited));
- //print();
- if (flag) ans++;
- }
- }
- int main() {
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 6; j++) {
- scanf(" %1c", &map[i][j]);
- }
- }
- go();
- printf("%d\n", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement