풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static int[] changeX = {1, -1, 0, 0};
static int[] changeY = {0, 0, 1, -1};
static int[][] arr;
static int x = 0;
static int y = 0;
static boolean[] check = new boolean[26];
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String[] size = str.split(" ");
x = Integer.parseInt(size[0]);
y = Integer.parseInt(size[1]);
arr = new int[x][y];
for(int i=0; i<x; i++){
str = br.readLine();
for(int j=0; j<y; j++){
arr[i][j] = (int)str.charAt(j) - 65;
}
}
System.out.println(Count(0, 0));
}
private static int Count(int a, int b) { // a : x좌표, b : y좌표
int[] answer = new int[4];
for(int i=0; i<answer.length; i++) {
answer[i] = 1;
}
check[arr[a][b]] = true;
for(int i=0; i<4; i++) {
int chX = a + changeX[i];
int chY = b + changeY[i];
if(chX < x && chY < y && chX >= 0 && chY >= 0) {
if(check[arr[chX][chY]] != true) {
answer[i] += Count(chX, chY);
}
}
}
Arrays.sort(answer);
check[arr[a][b]] = false;
return answer[3];
}
}
'코딩 문제 > 백준' 카테고리의 다른 글
백준 - 28086번 (0) | 2023.06.18 |
---|---|
백준 - 16437번 (1) | 2021.03.25 |
백준 - 10026번 (0) | 2021.03.25 |
백준 - 2468번 (0) | 2021.03.25 |
백준 - 2667번 (0) | 2021.03.25 |