Lined Notebook

[Gold 4] [Java] 알파벳 (1987번)

by HeshAlgo

Java Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.baekjoon.java;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    static int R, C, answer;
    static char[][] map;
    static int[] dx = {-1010};
    static int[] dy = {010-1};
    static boolean[] alphabetCheck;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] sizeInput = br.readLine().split(" ");
        R = Integer.parseInt(sizeInput[0]);
        C = Integer.parseInt(sizeInput[1]);
        map = new char[R][C];
        alphabetCheck = new boolean[26];
        // 맵 입력
        for (int row = 0; row < R; row++) {
            String input = br.readLine();
            for (int col = 0; col < C; col++) {
                map[row][col] = input.charAt(col);
            }
        }
        
        // 현재 위치 방문 체크
        alphabetCheck[map[0][0- 'A'= true;
        
        dfs(001);    
        
        System.out.println(answer);
        
    }
 
    private static void dfs(int x, int y, int cnt) {
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if (check(nx, ny)) {
                // 중복되는 알파벳을 만난 경우
                if (alphabetCheck[map[nx][ny] - 'A']) {
                    answer = Math.max(cnt, answer);
                } else {
                    alphabetCheck[map[nx][ny] - 'A'= true;    
                    dfs(nx, ny, cnt + 1);
                    alphabetCheck[map[nx][ny] - 'A'= false;
                }
            }
            
        }
        
    }
    
    // 맵의 범위 체크
    private static boolean check(int x, int y) {
        if (0 <= x && x < R && 0 <= y && y < C) {
            return true;
        }
        
        return false;
    }
 
}
 
 
cs

 

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기