문제
입력 및 출력
풀이
import java.io.*;
import java.util.*;
class Main {
public static int N;
public static int maxCandy = 0;
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
String[][] map = new String[N][N];
for(int i = 0; i < N; i++) {
String[] temp = br.readLine().split("");
for(int j = 0; j < temp.length; j++) {
map[i][j] = temp[j];
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N-1; j++) {
String temp = map[i][j];
map[i][j] = map[i][j+1];
map[i][j+1] = temp;
checkCount(map);
temp = map[i][j];
map[i][j] = map[i][j+1];
map[i][j+1] = temp;
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N-1; j++) {
String temp = map[j][i];
map[j][i] = map[j+1][i];
map[j+1][i] = temp;
checkCount(map);
temp = map[j][i];
map[j][i] = map[j+1][i];
map[j+1][i] = temp;
}
}
System.out.println(maxCandy);
}
public static void checkCount(String[][] map) {
for(int i = 0; i < N; i++) {
int count = 1;
for(int j = 0; j < N-1; j++) {
if(map[i][j].equals(map[i][j+1])) {
count++;
}else{
count = 1;
}
maxCandy = Math.max(maxCandy, count);
}
}
for(int i = 0; i < N; i++) {
int count = 1;
for(int j = 0; j < N-1; j++) {
if(map[j][i].equals(map[j+1][i])) {
count++;
}else{
count = 1;
}
maxCandy = Math.max(maxCandy, count);
}
}
}
public static void printMap(String[][] map){
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
}
결과 및 해결방법
[결과]