/* 다시 정돈해서 풀어보도록 하겠습니다. 와일(1){ 1. 뿌요찾아서 터트리기 cnt++; (터뜨릴 뿌요없으면 break) 2. 중력을 이용해서 아래 0 없애기 } */ #include <iostream> #include <queue> #include <algorithm> #include <string> #include <vector> using namespace std; int map[13][7]; int visit[13][7]; int x, y, nx, ny; int dx[] = { 0,0,1,-1 }; int dy[] = { 1,-1,0,0 }; int chain_cnt, zero_cnt; string a; int curNum; int same_color_cnt, flag; struct points { int x, y; }; queue<points> que; vector<points> vec; void problemIn() { for (int i = 0; i < 12; i++) { cin >> a; for (int j = 0; j < 6; j++) { if (a[j] == '.') map[i][j] = 0; else if (a[j] == 'R') map[i][j] = 1; else if (a[j] == 'G') map[i][j] = 2; else if (a[j] == 'B') map[i][j] = 3; else if (a[j] == 'P') map[i][j] = 4; else if (a[j] == 'Y') map[i][j] = 5; } } } void visit_init() { for (int i = 0; i < 12; i++) { for (int j = 0; j < 6; j++) { visit[i][j] = 0; } } } void bfs() { while (!que.empty()) { x = que.front().x; y = que.front().y; que.pop(); for (int i = 0; i < 4; i++) { nx = x + dx[i]; ny = y + dy[i]; if (nx >= 0 && ny >= 0 && nx < 12 && ny < 6) { if (map[nx][ny] == curNum && visit[nx][ny] == 0) { visit[nx][ny] = 1; same_color_cnt++; que.push({ nx,ny }); vec.push_back({ nx,ny }); } } } } if (same_color_cnt >= 4) { flag = 1; for (int i = 0; i < vec.size(); i++) { map[vec[i].x][vec[i].y] = 0; } } same_color_cnt = 0; vec.clear(); visit_init(); } void gravity() { for (int j = 0; j < 6; j++) { for (int i = 11; i >= 0; i--) { if (map[i][j] != 0) { // i = 9; for (int k = 11; k > i; k--) { // k = 11; if (map[k][j] == 0) { swap(map[i][j], map[k][j]); // break; // k for문을 벗어나도록 합니다. 새로운 아이를 찾도록 합니다. 2 그럼 다시 포문에 들어옵니다. } } } } } } void solve() { while (1) { flag = 0; for (int i = 0; i < 12; i++) { for (int j = 0; j < 6; j++) { if (map[i][j] != 0) { curNum = map[i][j]; // 처음 예제에서 10, 0 에서 1이 들어갑니다. que.push({ i,j }); vec.push_back({ i,j }); visit[i][j] = 1; same_color_cnt++; bfs(); } } } if (flag == 1) chain_cnt++; else { break; } // 연쇄할 것이 없다면, 와일문 끝. gravity(); } } int main(void) { problemIn(); solve(); cout << chain_cnt << endl; return 0; } |
뿌요뿌요, 중력부분에서 예제 몇개가 틀리기에 디버깅 후 완료하였습니다. 코드를 짤 때, 테스트 케이스를 넣기 전에도 알수 있도록. 애매한 부분이 어디인지 확실히 알아 둘 것! 아이디어를 애매하게 하지 말고, 실제로 숫자를 넣어서 어떻게 변경되는지 명확히 하면 좋습니다. 그리고 그것을 토대로 코딩을 하면 되니까요. 변수가 3개 이상 늘어나면 인덱스 처리하기가 까다롭습니다. |