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 |
| 31 |
Tags
- 백준
- math
- 시뮬레이션
- 투포인터
- binary search
- MySQL
- SWEA
- 이분탐색
- 건강
- SQL
- 자바
- 러닝일지
- 프로그래머스
- 운동기록
- COS PRO
- 문제풀이
- db
- greedy
- BFS
- oracle
- Java
- dfs
- 알고리즘
- 문제해결
- 코테
- DP
- 코딩테스트
- 문자열
- BOJ
- priorityqueue
Archives
- Today
- Total
슈콩
[BOJ] 백준 11559 Puyo Puyo 본문
[문제]
https://www.acmicpc.net/problem/11559
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static boolean[][] visit;
static char[][] map;
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
static class Puyo{
int r,c;
Puyo(int r,int c){
this.r = r;
this.c = c;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
map = new char[12][6];
for(int i=0;i<12;i++) {
String s = br.readLine();
for(int j=0;j<6;j++) {
map[i][j] = s.charAt(j);
}
}
int result = 0;
while(true) {
boolean check = false;
visit = new boolean[12][6];
for(int i=0;i<12;i++) {
for(int j=0;j<6;j++) {
if(map[i][j]!='.' && !visit[i][j]) {
List<Puyo> list = bfs(i,j,map[i][j]);
if(list.size()>=4) {
for(Puyo p : list) {
map[p.r][p.c] = '.';
}
check = true;
}
}
}
}
if(!check) break;
result++;
gravity();
}
System.out.println(result);
}
private static List<Puyo> bfs(int i,int j,char c){
List<Puyo> list = new LinkedList<>();
Queue<int[]> q = new LinkedList<>();
q.offer(new int[] {i,j});
visit[i][j] = true;
list.add(new Puyo(i,j));
while(!q.isEmpty()) {
int[] curr = q.poll();
for(int d=0;d<4;d++) {
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr<0 || nr>=12 || nc<0 || nc>=6) continue;
if(!visit[nr][nc] && map[nr][nc]==c) {
q.offer(new int[] {nr,nc});
list.add(new Puyo(nr,nc));
visit[nr][nc] = true;
}
}
}
return list;
}
private static void gravity() {
for(int j=0;j<6;j++) {
int idx = 11;
for(int i=11;i>=0;i--) {
if(map[i][j]!='.') {
map[idx--][j] = map[i][j];
}
}
while(idx>=0) {
map[idx--][j] = '.';
}
}
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 13335 트럭 (0) | 2025.09.17 |
|---|---|
| [BOJ] 백준 12100 2024 Easy (0) | 2025.09.17 |
| [BOJ] 백준 12015 가장 긴 증가하는 부분 수열 2 (0) | 2025.09.17 |
| [BOJ] 백준 10815 숫자 카드 (0) | 2025.09.16 |
| [BOJ] 백준 1654 랜선 자르기 (0) | 2025.09.16 |