Problem Solving/BOJ
[Silver 3] 3085번 사탕 게임
kmkunk
2022. 4. 1. 14:21
import java.io.*;
public class Main {
public static String[][] board;
public static int n;
public static int max;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
board = new String[n+1][n+1];
max = 1;
for(int i=1; i<=n; i++) {
String[] arr = br.readLine().split("");
for(int j=1; j<=n; j++) { board[i][j] = arr[j-1]; }
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
if((i==n&&j==n) || max==n) { System.out.println(max); return; }
countC(i, j);
countR(i, j);
if(i==n) {
swapC(i ,j);
countC(i, j);
countR(i, j);
countR(i, j+1);
swapC(i ,j);
continue;
} else if(j==n) {
swapR(i ,j);
countR(i, j);
countC(i, j);
countC(i+1, j);
swapR(i ,j);
continue;
}
swapC(i ,j);
countC(i, j);
countR(i, j);
countR(i, j+1);
swapC(i ,j);
swapR(i ,j);
countR(i, j);
countC(i, j);
countC(i+1, j);
swapR(i ,j);
}
}
System.out.println(max);
}
public static void countC(int i, int j) {
int count = 1;
for(int k=2; k<=n; k++) {
if(board[i][k].equals(board[i][k-1])) {
count++;
max = Math.max(max, count);
} else { count = 1; }
}
}
public static void countR(int i, int j) {
int count = 1;
for(int k=2; k<=n; k++) {
if(board[k][j].equals(board[k-1][j])) {
count++;
max = Math.max(max, count);
} else { count = 1; }
}
}
public static void swapC(int i, int j) {
String temp = board[i][j];
board[i][j] = board[i][j+1];
board[i][j+1] = temp;
}
public static void swapR(int i, int j) {
String temp = board[i][j];
board[i][j] = board[i+1][j];
board[i+1][j] = temp;
}
}