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
- dfs
- 동적계획법
- Android
- 분할정복
- 이분탐색
- 코딩테스트
- 프로그래머스
- activity
- 운영체제
- 자바
- 문자열
- 다이나믹프로그래밍
- 스택
- BOJ
- 문자열다루기
- 완전탐색
- 코딩
- 코틀린
- 그래프
- 배열
- 안드로이드
- component
- 백준
- BFS
- 카카오블라인드
- 세그먼트트리
- GIT
- 알고리즘
- 트리
- 생명주기
Archives
- Today
- Total
HS_development_log
백준 3085번 - 사탕게임 / Java 본문
반응형
문제
https://www.acmicpc.net/problem/3085
모든 경우의 수를 탐색해서 답을 구하는 문제. 브루트포스이다
접하는 두칸을 변경하는데 걸리는 시간N^2 배열을 검사하는데 걸리는시간 N^2
따라서 모든경우의 수를 탐색할 경우 걸리는 시간은 O(N^4)이다
N의 최댓값은 50이므로 약 6,250,000의 경우의수가 나온다.
많지 않은 경우이므로 브루트포스로 풀 수 있다.
알고리즘
-
인접한 칸의 사탕이 다를경우 서로 바꿔준다
-
먹을 수 있는 사탕의 개수를 계산한다
-
최댓값을 출력한다
소스코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/*
* BOJ3085
* 2020.01.20
* Hyeonseong
*/
import java.util.*;
public class BOJ3085
{
public static void main(String[] args)
{
Solution s = new Solution();
}
}
class Solution{
char[][] candy;
int max;
public Solution() {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
max = 0;
candy = new char[n][n];
for(int i=0;i<n;i++) {
candy[i] = scan.next().toCharArray();
}
char temp=' ';
for(int i=0;i<n;i++) { // 가로 확인
for(int j=0;j<n-1;j++) {
if(candy[i][j] != candy[i][j+1]) {
temp= candy[i][j];
candy[i][j] = candy[i][j+1];
candy[i][j+1] = temp;
check();
temp = candy[i][j];
candy[i][j] = candy[i][j+1];
candy[i][j+1] = temp;
}
}
}
for(int i=0;i<n;i++) { // 세로확인
for(int j=0;j<n-1;j++) {
if(candy[j][i]!=candy[j+1][i]) {
temp = candy[j][i];
candy[j][i] = candy[j+1][i];
candy[j+1][i] = temp;
check();
temp = candy[j][i];
candy[j][i] = candy[j+1][i];
candy[j+1][i] = temp;
}
}
}
System.out.println(max);
}
public void check(){
int num = 1;
for(int i=0;i<candy.length;i++) { // 가로체크
num = 1;
for(int j=0;j<candy[i].length-1;j++) {
if(candy[i][j]==candy[i][j+1]) {
num++;
if(max < num) {
max = num;
}
}
else {
num = 1;
}
}
}
for(int i=0;i<candy.length;i++) { // 세로체크
num = 1;
for(int j=0;j<candy[i].length-1;j++) {
if(candy[j][i]==candy[j+1][i]) {
num++;
if(max < num) {
max = num;
}
}
else {
num = 1;
}
}
}
}
}
|
cs |
반응형
'Algorithm-백준 > 브루트포스' 카테고리의 다른 글
백준 1748번 - 수 이어 쓰기1 / Kotlin (0) | 2020.08.04 |
---|