회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
뭐가 틀린건지 도저히 모르겠어요...
3085번 - 사탕 게임
nrcrzl93
3년 전
0
디버깅을 해보면 max값이 이상할때가 있는데 어디서 잘못된 것인지 도저히 모르겠어요.. 도와주세요ㅠㅠ
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //문제 //상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다. // //가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다. 이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다. // //사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오. // //입력 //첫째 줄에 보드의 크기 N이 주어진다. (3 ≤ N ≤ 50) // //다음 N개 줄에는 보드에 채워져 있는 사탕의 색상이 주어진다. 빨간색은 C, 파란색은 P, 초록색은 Z, 노란색은 Y로 주어진다. // //사탕의 색이 다른 인접한 두 칸이 존재하는 입력만 주어진다. // //출력 //첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다. public class Main { static char[][] bomCopy; static char[][] bom; static int N; static boolean debug = true; public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); bom = new char[N][N]; bomCopy = new char[N][N]; String a; for(int i=0; i<N; i++) { a = br.readLine(); for(int j=0; j<N; j++) { bom[i][j] = a.charAt(j); bomCopy[i][j] = a.charAt(j); // System.out.println("bom[" + i+"]["+j+"] :" + bom[i][j]); // System.out.println("bomCopy[" + i+ "]["+j+"] : " + bomCopy[i][j]); // } } int max = 1; int result = 1; for(int i=0; i<N; i++) { for(int j=1; j<N; j++) { if(bomCopy[i][j-1] != bomCopy[i][j]) { bomCopy[i][j-1] = bom[i][j]; bomCopy[i][j] = bom[i][j-1]; result = bruth(bomCopy); max = Math.max(max, result); recover(); } } } for(int j=0; j<N; j++) { for(int i=1; i<N; i++) { if(bomCopy[i-1][j] != bomCopy[i][j]) { bomCopy[i-1][j] = bom[i][j]; bomCopy[i][j] = bom[i-1][j]; result = bruth(bomCopy); max = Math.max(max, result); recover(); } } } System.out.println(max); } private static int bruth(char[][] bomCopy) { int max = 1; int result = 1; int result2 = 1; for(int i=0; i<N; i++) { for(int j=1; j<N; j++) { if(bomCopy[i][j-1] == bomCopy[i][j]) { result += 1; } else { if(max < result) { max = result; } result = 1; } } max = Math.max(max, result); } for(int j=0; j<N; j++) { for(int i=1; i<N; i++) { if(bomCopy[i-1][j] == bomCopy[i][j]) { result2 += 1; } else { if(max < result2) { max = result2; } result2 = 1; } } max = Math.max(max, result2); } for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { System.out.print("bomCopy[" + i+ "]["+j+"] : " + bomCopy[i][j] + " "); System.out.print(bomCopy[i][j]); } System.out.println(); } System.out.println(max); return max; } private static void recover() { for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { bomCopy[i][j] = bom[i][j]; // System.out.print("bomCopy[" + i+ "]["+j+"] : " + bomCopy[i][j] + " "); // } // System.out.println(); } } }
댓글을 작성하려면
로그인
해야 합니다.
nrcrzl93 3년 전
디버깅을 해보면 max값이 이상할때가 있는데 어디서 잘못된 것인지 도저히 모르겠어요.. 도와주세요ㅠㅠ