티스토리 뷰
3085번: 사탕 게임
첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.
www.acmicpc.net
풀이
1) 이차원 배열을 탐색하면서 오른쪽으로 한번, 아래쪽으로 한번 바꿈
2) 최대길이를 갱신한다.
3) 바꾼거 제자리로 원위치
주의사항
1) 배열의 오른쪽, 아래 가장자리 때문에 isIn()으로 체크해줘야함
package com.baekJoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.StringTokenizer;
public class BJ_S4_3085_사탕게임 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int N, max=1;
static char map[][];
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
N = Integer.parseInt(input.readLine());
map = new char[N][N];
for(int r=0; r<N; r++) {
String line = input.readLine();
for(int c=0; c<N; c++) {
map[r][c] = line.charAt(c);
}
}
solve();
System.out.println(max);
}
private static void solve() {
for(int r=0; r<N; r++) {
for(int c=0; c<N; c++) {
int nr = r+1;
int nc = c+1;
if(isIn(r, nc)) {
char temp = map[r][c];
map[r][c] = map[r][nc];
map[r][nc] = temp;
getMaxCandy();
temp = map[r][nc];
map[r][nc] = map[r][c];
map[r][c] = temp;
}
if(isIn(nr, c)) {
char temp = map[r][c];
map[r][c] = map[nr][c];
map[nr][c] = temp;
getMaxCandy();
temp = map[nr][c];
map[nr][c] = map[r][c];
map[r][c] = temp;
}
}
}
}
private static void getMaxCandy() {
char peek = '0';
for(int r=0; r<N; r++) {
int cnt = 0;
for(int c=0; c<N; c++) {
if(peek == '0') {
peek = map[r][c];
cnt = 1;
}else {
if(peek == map[r][c]) {
cnt++;
if(max < cnt) {
max = cnt;
}
}else {
cnt = 1;
peek = map[r][c];
}
}
}
}
peek = '0';
for(int c=0; c<N; c++) {
int cnt = 0;
for(int r=0; r<N; r++) {
if(peek == '0') {
peek = map[r][c];
cnt = 1;
}else {
if(peek == map[r][c]) {
cnt++;
if(max < cnt) {
max = cnt;
}
}else {
cnt = 1;
peek = map[r][c];
}
}
}
}
}
static boolean isIn(int r, int c) {
return (r>=0 && c>=0 && r<N && c<N);
}
static String src =
"4\r\n" +
"CCCC\r\n" +
"YDYD\r\n" +
"DYDY\r\n" +
"YDYD";
}
후기
처음엔 있어보일려고 스택을 썼는데 메모리가 213080KB, 시간이 900ms가 나와서 스택부분을 없앴더니 시간과 메모리가 단축되었다. 쉽게생각하는것이 좋다는것을 깨달았다.
'Algorithm' 카테고리의 다른 글
[백준] B2 2596 비밀 편지 (java) (0) | 2020.12.30 |
---|---|
[백준] S2 2210 숫자판 점프 (java) (0) | 2020.12.30 |
[백준] S2 2304 창고 다각형 (java) (0) | 2020.12.28 |
[백준] S2 3019 테트리스 (java) (0) | 2020.12.28 |
[백준] G5 15683 감시 (java) (0) | 2020.12.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- S2
- 백트래킹
- G5
- 문자열
- map
- react
- BFS
- Spring Boot
- SWEA
- 알고리즘
- S3
- 자바
- 백준
- 그리디
- 시뮬레이션
- laugh4mile
- PriorityQueue
- 다익스트라
- 코딩새내기
- java
- g4
- 리액트
- DFS
- 리액트 네이티브
- Spring
- react native
- 구현
- 객체지향
- 현꾸라지
- 우선순위큐
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함