Skip to content

Commit fb4cc44

Browse files
committed
feat: boj_3085
1 parent 7bd98f4 commit fb4cc44

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

3085번: 사탕 게임/Main.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: ::: ::: */
4+
/* Problem Number: 3085 :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: dmb07301 <boj.kr/u/dmb07301> +#+ +#+ +#+ */
7+
/* +#+ +#+ +#+ */
8+
/* https://boj.kr/3085 #+# #+# #+# */
9+
/* Solved: 2026/01/05 14:36:49 by dmb07301 ### ### ##.kr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
/*
14+
사탕게임
15+
16+
N x N 크기
17+
사탕의 색이 다른 인접한 두 칸을 고름
18+
사탕 교화 -> 모두 같은 색으로 이루어진 가장 긴 연속 부분을 고르고 행이나 열을 먹음
19+
먹을 수 있는 사탕의 최대 개수
20+
21+
설마 다 바꿔보면서 개수 체크 해야되나?????
22+
개귀찮네
23+
24+
4방향? -> 그냥 우,하 만 검사하면 다 될듯?
25+
*/
26+
27+
import java.io.*;
28+
import java.util.*;
29+
30+
public class Main {
31+
32+
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
33+
private static String[][] arr;
34+
public static void main(String[] args) throws IOException {
35+
int n = Integer.parseInt(br.readLine());
36+
arr = new String[n][n];
37+
38+
for(int i = 0; i < n; i++) {
39+
arr[i] = br.readLine().split("");
40+
}
41+
int rMax = 0;
42+
for(int i = 0; i < n; i++) {
43+
for(int j = 0; j < n; j++){
44+
if(i + 1 < n) {
45+
swap(i, j, i + 1, j);
46+
rMax = Math.max(rMax, checkLines(i, j, i + 1, j));
47+
swap(i, j, i + 1, j);
48+
}
49+
50+
if(j + 1 < n) {
51+
swap(i, j, i, j + 1);
52+
rMax = Math.max(rMax, checkLines(i, j, i, j + 1));
53+
swap(i, j, i, j + 1);
54+
}
55+
}
56+
}
57+
58+
System.out.println(rMax);
59+
}
60+
61+
private static int checkLines(int x1, int y1, int x2, int y2) {
62+
int max = 1;
63+
64+
max = Math.max(max,maxInRow(x1));
65+
max = Math.max(max,maxInRow(x2));
66+
max = Math.max(max,maxInCol(y1));
67+
max = Math.max(max,maxInCol(y2));
68+
69+
return max;
70+
}
71+
72+
private static int maxInRow(int r) {
73+
int n = arr.length;
74+
int best = 1, cnt = 1;
75+
for (int c = 1; c < n; c++) {
76+
if (arr[r][c].equals(arr[r][c - 1])) cnt++;
77+
else cnt = 1;
78+
if (cnt > best) best = cnt;
79+
}
80+
return best;
81+
}
82+
83+
private static int maxInCol(int c) {
84+
int n = arr.length;
85+
int best = 1, cnt = 1;
86+
for (int r = 1; r < n; r++) {
87+
if (arr[r][c].equals(arr[r - 1][c])) cnt++;
88+
else cnt = 1;
89+
if (cnt > best) best = cnt;
90+
}
91+
return best;
92+
}
93+
94+
private static void swap(int x1, int y1, int x2, int y2) {
95+
String temp = arr[x1][y1];
96+
arr[x1][y1] = arr[x2][y2];
97+
arr[x2][y2] = temp;
98+
}
99+
}

3085번: 사탕 게임/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 3085번: 사탕 게임 - <img src="https://static.solved.ac/tier_small/9.svg" style="height:20px" /> Silver II
2+
3+
<!-- performance -->
4+
5+
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.-->
6+
7+
<!-- end -->
8+
9+
## 문제
10+
11+
[문제 링크](https://boj.kr/3085)
12+
13+
<p>상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다.</p>
14+
15+
<p>가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다.&nbsp;이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다.</p>
16+
17+
<p>사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오.</p>
18+
19+
## 입력
20+
21+
<p>첫째 줄에 보드의 크기 N이 주어진다. (3 ≤ N ≤ 50)</p>
22+
23+
<p>다음 N개 줄에는 보드에 채워져 있는 사탕의 색상이 주어진다. 빨간색은 C, 파란색은 P, 초록색은 Z, 노란색은 Y로 주어진다.</p>
24+
25+
<p>사탕의 색이 다른 인접한 두 칸이 존재하는 입력만 주어진다.</p>
26+
27+
## 출력
28+
29+
<p>첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.</p>
30+
31+
## 소스코드
32+
33+
[소스코드 보기](Main.java)

0 commit comments

Comments
 (0)