Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 3085번 사탕 게임 문제! (silver 2 본문
import java.io.*;
import java.util.*;
public class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.parseInt(br.readLine());
char[][] board=makeBoard(n);
int answer=0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(j>0) {
char temp = board[i][j];
board[i][j] = board[i][j - 1];
board[i][j - 1] = temp;
int tmp = Math.max(colMany(board, n), rowMany(board, n));
answer = answer > tmp ? answer : tmp;
board[i][j - 1] = board[i][j];
board[i][j] = temp;
}
if(i<n-1) {
char temp = board[i][j];
board[i][j] = board[i + 1][j];
board[i + 1][j] = temp;
int tmp = Math.max(colMany(board, n), rowMany(board, n));
answer = answer > tmp ? answer : tmp;
board[i + 1][j] = board[i][j];
board[i][j] = temp;
}
}
}
bw.write(answer+"");
bw.flush();
}
//보드판 만들고 채워넣기
public static char[][] makeBoard(int n) throws IOException{
char[][] board=new char[n][n];
for(int i=0; i<n; i++){
String a=br.readLine();
for(int j=0; j<n; j++){
board[i][j]=a.charAt(j);
}
}
return board;
}
//가로열
public static int rowMany(char[][] board, int n){
int max=0;
for(int i=0; i<n; i++){
char tempc=board[i][0];
int temp=0;
for(int j=0; j<n; j++){
if(board[i][j]==tempc){
temp++;
}
else{
max= max > temp ? max:temp;
temp=1;
tempc=board[i][j];
}
}
max= max > temp ? max:temp;
}
return max;
}
//세로열 검사
public static int colMany(char[][] board, int n){
int max=0;
for(int j=0; j<n; j++){
char tempc=board[0][j];
int temp=0;
for(int i=0; i<n; i++){
if(board[i][j]==tempc){
temp++;
}
else{
max= max > temp ? max:temp;
temp=1;
tempc=board[i][j];
}
}
max= max > temp ? max:temp;
}
return max;
}
}
//조건을 제대로 안읽어서 오랜 헤맨문제,,, 연속하는 사탕을 먹는거였음
'백준공부 > java' 카테고리의 다른 글
[백준] 1463번 1로 만들기 문제! (silver 3 (0) | 2023.04.29 |
---|---|
[백준] 2578번 빙고 문제! (silver 4 (0) | 2023.04.28 |
[백준] 1912번 연속합 문제! (silver 2 (0) | 2023.04.26 |
[백준] 17299번 오등큰수 문제! (gold 3 (0) | 2023.04.26 |
[백준] 2748번 피보나치 수 2 (bronze 1 (0) | 2023.04.21 |