Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 정석
- 코딩
- 브루트포스
- 컴공과
- Stack
- coding
- 문제풀이
- 구현
- 자료구조
- 너비우선탐색
- DP
- Computer science
- 북리뷰
- 백준
- 컴공
- 개발
- 정석학술정보관
- OS
- 그래프
- 컴퓨터공학과
- 코테
- bfs
- vector
- Operating System
- cs
- c++
- 오퍼레이팅시스템
- 오에스
- 스택
- 알고리즘
Archives
- Today
- Total
Little Jay
[C++] 백준 11559번 - Puyo Puyo 본문
실제 뿌요뿌요 게임을 구현하는 문제였다.
아이디어는 이렇다 4개 이상의 같은 색깔의 슬라임(?)이 모이면 터지고 1연쇄가 일어난 후 뿌요들이 아래로 중력에 의해 내려온다. 이 문제를 풀때 간과했던 것은 한 번에 터뜨릴 수 있는 것들은 한번에 터지고 이는 1 연쇄이다. 그래서 한번에 2개의 그룹이 터져도 2연쇄가 아니라 1연쇄이다. 이 부분을 제외하면 재밌게 풀었던 문제이다.
#include <bits/stdc++.h>
#include <cstring>
#define endl '\n'
using namespace std;
char graph[12][6];
bool visited[12][6];
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
int ans = 0;
//일반 bfs
int bfs(int x, int y, bool flag) {
char target = graph[x][y];
queue<pair<int, int>> q;
queue<pair<int, int>> change; //만약 flag == true일때 방문한 점을 다시 '.'로 만들어 주기 위한 작업
change.push({ x, y });
q.push({ x, y });
visited[x][y] = true;
int connected = 1;
while (!q.empty()) {
auto curr = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = curr.first + dx[i];
int ny = curr.second + dy[i];
if (nx < 0 || ny < 0 || nx >= 12 || ny >= 6) continue;
if (graph[nx][ny] != target) continue;
if (visited[nx][ny]) continue;
q.push({ nx, ny });
change.push({ nx, ny });
visited[nx][ny] = true;
connected++;
}
}
if (flag) {
while (!change.empty()) {
auto cur = change.front();
graph[cur.first][cur.second] = '.';
change.pop();
}
}
return connected;
}
//연쇄가 일어난다면 칸을 .로 채우기
int blast() {
memset(visited, false, sizeof(visited));
bool flag = false;
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
if (graph[i][j] != '.' && !visited[i][j]) {
if (bfs(i, j, false) >= 4) {
flag = true;
memset(visited, false, sizeof(visited));
bfs(i, j, true);
//break;
}
}
}
//if (flag) break;
}
if (flag) return 1;
else return 0;
}
//중력에 의해 아래로 이끌려 내려오기
void pullDown() {
for (int j = 0; j < 6; j++) {
for (int i = 11; i >= 0; i--) {
if (graph[i][j] != '.') {
for (int o = i; i + 1 < 12 && graph[o + 1][j] == '.'; o++) {
char temp = graph[o][j];
graph[o][j] = graph[o + 1][j];
graph[o + 1][j] = temp;
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
cin >> graph[i][j];
}
}
while (blast()) {
pullDown();
ans += 1;
}
//for (int i = 0; i < 12; i++) {
// for (int j = 0; j < 6; j++) {
// cout << graph[i][j];
// }
// cout << endl;
//}
cout << ans << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 9009번 - 피보나치 (0) | 2022.07.25 |
---|---|
[C++] 백준 5972번 - 택배 배송 (0) | 2022.07.22 |
[C++] 백준 2174 - 로봇 시뮬레이션 (0) | 2022.07.19 |
[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇 (0) | 2022.07.18 |
[C++] 백준 1504 - 특정한 최단 경로 (0) | 2022.07.16 |
Comments