본문 바로가기
알고리즘/DFS

[백준 17090] 미로 탈출 / 자바 / dfs(dfs 코드구현 연습)

by 순원이 2024. 8. 16.

#문제         

레벨: G5
알고리즘:  dfs(dfs 코드구현 연습)

풀이시간: 
힌트 참조 유무:

https://www.acmicpc.net/problem/17090


#문제 풀이        

visted이 0,1,2(방문x,       방문O && 정답아님,     방문O && 정답임) 세가지 상태를 담을 때

import java.util.*;

public class Main {
    static int N, M;
    static char[][] maze;
    static int[][] visited;
    static int[] dx = {-1, 0, 1, 0}; // U, R, D, L
    static int[] dy = {0, 1, 0, -1}; // U, R, D, L

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        sc.nextLine();
        maze = new char[N][M];
        visited = new int[N][M];

        for (int i = 0; i < N; i++) {
            String line = sc.nextLine();
            for (int j = 0; j < M; j++) {
                maze[i][j] = line.charAt(j);
                visited[i][j] = -1; 
            }
        }

        int escapeCount = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (canEscape(i, j)) {
                    escapeCount++;
                }
            }
        }

        System.out.println(escapeCount);
    }

    static boolean canEscape(int x, int y) {
        if (x < 0 || x >= N || y < 0 || y >= M) {
            return true; 
        }
        if (visited[x][y] != -1) {
            return visited[x][y] == 1;
        }

        visited[x][y] = 0

        int dir = getDirection(maze[x][y]);
        int nx = x + dx[dir];
        int ny = y + dy[dir];

        if (canEscape(nx, ny)) {
            visited[x][y] = 1;
            return true;
        } else {
            visited[x][y] = 0; 
            return false;
        }
    }

    static int getDirection(char c) {
        switch (c) {
            case 'U': return 0;
            case 'R': return 1;
            case 'D': return 2;
            case 'L': return 3;
        }
        return -1; 
    }
}

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int[][] map, visited;
    static boolean[] successCheck;
    static int N, M, num = 1, ans = 0;
    static int[][] dt = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        map = new int[N][M];
        visited = new int[N][M];
        successCheck = new boolean[N * M + 1];
        for (int i = 0; i < N; i++) {
            String input = br.readLine();
            for (int j = 0; j < M; j++) {
                char c = input.charAt(j);
                if (c == 'U') map[i][j] = 0;
                else if (c == 'R') map[i][j] = 1;
                else if (c == 'D') map[i][j] = 2;
                else map[i][j] = 3;
            }
        }

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (visited[i][j] != 0) continue;
                dfs(i, j, 0);
                num++;
            }
        }

        System.out.println(ans);
        br.close();
    }

    public static void dfs(int x, int y, int count) {

        if (check(x, y)) {
            successCheck[num] = true;
            ans += count;
            return;
        } else if (visited[x][y] != 0) {
            if (successCheck[visited[x][y]]) {
                successCheck[num] = true;
                ans += count;
            }
            return;
        }

        visited[x][y] = num;
        dfs(x + dt[map[x][y]][0],  y + dt[map[x][y]][1], count + 1);
    }

    public static boolean check(int x, int y) {
        return x < 0 || x >= N || y < 0 || y >= M;
    }
}