Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- #include<memory.h>
- using namespace std;
- char map[12][6];
- int check[12][6];
- int ans = 0;
- int dy[] = { -1,1,0,0 };
- int dx[] = { 0,0,-1,1 };
- bool bfs(int y, int x) {
- queue<pair<int, int> > q;
- int cnt = 0;
- check[y][x] = 1;
- q.push({ y,x });
- //q가 빌때까지
- while (!q.empty()) {
- pair<int, int> now = q.front();
- q.pop();
- //뿌요의 갯수 세기
- cnt++;
- //인접해있는 같은 색의 뿌요가 있으면 큐에 넣어주자
- for (int i = 0; i < 4; i++) {
- int nY = now.first + dy[i];
- int nX = now.second + dx[i];
- if (nY >= 0 && nY < 12 && nX >= 0 && nX < 6 && map[nY][nX] == map[now.first][now.second] && check[nY][nX] == 0) {
- check[nY][nX] = 1;
- q.push({ nY,nX });
- }
- }
- }
- //인접해 있는 뿌요가 4개 이상이면 리턴 true
- if (cnt >= 4)
- return true;
- else
- return false;
- }
- void restoreMap() {
- queue<pair<int, int> > q;
- //11행부터 위로 올라가면서 뿌요가 있으면 큐에 넣어준다.
- for (int i = 0; i < 6; i++) {
- for (int j = 11; j >= 0; j--) {
- if (map[j][i] != '.') {
- q.push({ j,i });
- }
- }
- }
- //q가 빌때까지
- while (!q.empty()) {
- pair<int, int> now = q.front();
- q.pop();
- //뿌요알파벳 저장
- char alphabet = map[now.first][now.second];
- //아래로 한칸 내려 주기 위해
- int nY = now.first + dy[1];
- //아래가 [0,12) && 빈공간이면
- if (nY >= 0 && nY < 12 && map[nY][now.second] == '.') {
- //현재 뿌요 위치는 . 으로 바꿔주고
- map[now.first][now.second] = '.';
- //한 칸 내려준다
- map[nY][now.second] = alphabet;
- //그 밑에 칸에 또 있을 수 있으니 큐에 삽입
- q.push({ nY, now.second });
- }
- }
- //뿌요를 재정립 끝
- }
- int main() {
- //input
- for (int i = 0; i < 12; i++)
- for (int j = 0; j < 6; j++)
- scanf(" %c", &map[i][j]);
- //아래부터 뿌요를 보자
- for (int i = 11; i >= 0; i--) {
- bool flag = false;
- for (int j = 0; j < 6; j++) {
- //check배열 초기화
- memset(check, 0, sizeof(check));
- flag = false;
- //뿌요가 있으면
- if (map[i][j] != '.') {
- //bfs(인접한 뿌요가 4개이상이면 flag= true, 4개 미만이면 flag=false;
- flag = bfs(i, j);
- //인접해 있는게 4개 이상이면
- if (flag) {
- //연쇄 하나 추가
- ans++;
- //check된 곳이 뿌요가 있던 곳 이기 때문에 다 터트려 준다(.으로바꿔준다)
- for (int k = 11; k >= 0; k--) {
- for (int l = 0; l < 6; l++) {
- if (check[k][l] == 1) {
- map[k][l] = '.';
- }
- }
- }
- //터진 뿌요 위에 다른 색상의 뿌요가 있을 수 있으므로 아래로 내려준다.
- restoreMap();
- }
- }
- }
- }
- printf("%d\n", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement