[코드트리 챌린지] (2주차를 건너뛰고)3주차

헨리·2023년 9월 25일
0

코드트리

목록 보기
2/7
post-thumbnail

2주차는 바빠서 어쩌다보니 스킵하게 되었는데

실력진단 결과가 올랐다 ! 신기하다.

DFS, BFS 공부가 부족하긴 하다 ㅜ

[코드트리]두 방향 탈출 가능 여부 판별하기

https://www.codetree.ai/missions/2/problems/determine-escapableness-with-2-ways?&utm_source=clipboard&utm_medium=text

풀었던 문제이긴 한데 다시 보니까 막막해서 한번 더 풀어봤다.

import java.util.*;
public class Main {
    public static int N, M, map[][];
    public static int[] dr = {0,1};
    public static int[] dc = {1,0};
    public static boolean answer, visited[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        map = new int[N][M];
        visited = new boolean[N][M];
        for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                map[i][j] = sc.nextInt();
            }
        }

        DFS(0,0);

        if(answer){
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }

    public static void DFS(int x,int y){
        if(x == N-1 && y == M-1){
            answer=true;
            return;
        }
        for(int d=0;d<2;d++){
            if (canGo(x+dr[d],y+dc[d]) && !visited[x+dr[d]][y+dc[d]]){
                visited[x+dr[d]][y+dc[d]] = true;
                DFS(x+dr[d],y+dc[d]);
            }
        }
    }

    public static boolean isRange(int x, int y){
        return 0 <= x && x < N && 0 <= y && y < M;
    }

    public static boolean canGo(int x,int y){
        if (!isRange(x,y)){return false;}
        else if(map[x][y]==0) {return false;}
        return true;
    }
}

정답을 맞추고 해설과 비교해보니 덜 다듬어진 코드라는 생각이 들었다 ㅜ 이번주동안 열심히 풀어봐야겠음 ㅜ

10/12 시험이라
조금 앞당겨야하는데 ,, 과연 !

profile
개발자?

0개의 댓글