Skip to content

Commit ea4c142

Browse files
committed
BOJ_1987 : 알파벳
1 parent 8edb0c5 commit ea4c142

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

week14/BOJ_1987(알파벳).java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
private static int R, C;
7+
private static char[][] board;
8+
private static boolean[][] visited;
9+
private static boolean[] check;
10+
private static int[] dh = {-1, 0, 1, 0};
11+
private static int[] dw = {0, 1, 0, -1};
12+
private static int answer = 0;
13+
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
R = Integer.parseInt(st.nextToken());
18+
C = Integer.parseInt(st.nextToken());
19+
board = new char[R][C];
20+
visited = new boolean[R][C];
21+
check = new boolean[30];
22+
for(int i=0; i<R; i++) {
23+
String input = br.readLine();
24+
for(int j=0; j<C; j++) {
25+
board[i][j] = input.charAt(j);
26+
}
27+
}
28+
visited[0][0] = true;
29+
check[board[0][0] - 'A'] = true;
30+
dfs(0, 0, 1);
31+
System.out.println(answer);
32+
}
33+
34+
public static void dfs(int h, int w, int depth) {
35+
if(depth > R*C) {
36+
return;
37+
}
38+
answer = Math.max(answer, depth);
39+
40+
for(int i=0; i<4; i++) {
41+
int nextH = h + dh[i];
42+
int nextW = w + dw[i];
43+
if(nextH >= 0 && nextH < R && nextW >=0 && nextW < C) {
44+
int index = board[nextH][nextW] - 'A';
45+
if(!visited[nextH][nextW] & !check[index]) {
46+
check[index] = true;
47+
dfs(nextH, nextW, depth+1);
48+
check[index] = false;
49+
}
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)