아카이브

[백준]1987.알파벳 본문

자료구조&알고리즘

[백준]1987.알파벳

주멘이 2020. 9. 14. 23:29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class Main {
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static int R, C;
    static char[][] map;
    static int max = Integer.MIN_VALUE;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        map = new char[R][C];
        for (int i = 0; i < R; i++) {
            String string = br.readLine();
            for (int j = 0; j < C; j++) {
                map[i][j] = string.charAt(j);
            }
        }
        Set<Character> set = new HashSet<>();
        set.add(map[0][0]);
        DFS(0, 0, 1, set);

        System.out.println(max);


    }

    static void DFS(int x, int y, int depth, Set<Character> set) {

        max = Math.max(max, depth);

        for (int d = 0; d < 4; d++) {
            int nx = x + dx[d];
            int ny = y + dy[d];

            if (!isRange(nx, ny)) continue;
            char next = map[nx][ny];
            if(set.contains(next)) continue;
            set.add(next);
            DFS(nx,ny,depth+1,set);
            set.remove(next);

        }
    }

    static boolean isRange(int x, int y) {
        return 0 <= x && x < R && 0 <= y && y < C;
    }
}
1987번: 알파벳
 
www.acmicpc.net

'자료구조&알고리즘' 카테고리의 다른 글

[백준]18111. 마인크래프트  (0) 2021.01.10
[백준]1874. 스택 수열  (0) 2021.01.09
[백준]13549.숨바꼭질3  (0) 2020.09.14
[백준]1753.최단경로  (0) 2020.09.14
[백준]1238.파티  (0) 2020.09.14