[코드트리 챌린지] 8주차 ,...

헨리·2023년 10월 30일
0

코드트리

목록 보기
7/7

다음엔 확실히 올라가서 우상향 그래프로 보이겠지 ?.. 아 하필 dp에 걸려서 마지막 챌린지를 지난주와 동점으로 마무리해서 아쉽긴한데 뭐 괜찮다!
생각보다 매주 글 쓰는게 어렵지 않아서 가능하다면 비정기적으로도 이어갈 것 같음


이렇게 그래프를 보니까 확실히 감을 잃지는 않고 있다는걸 느끼고 있음
빠른 시일내에 900을 넘어보겠다!

마지막 문제는 비를 피하기 !

https://www.codetree.ai/missions/2/problems/stay-out-of-rain?&utm_source=clipboard&utm_medium=text

마지막으로 푼 문제는 아니지만 ..나는 BFS로 시작해서 BFS로 끝나는 사람인듯

처음 작성했던 코드는 이렇다.


import java.util.*;

public class Main {
    public static int N, H, M, now, answer[][], map[][], step[][];
    public static int[] dr = {-1,0,1,0};
    public static int[] dc = {0,1,0,-1};
    public static boolean goal, visited[][];
    public static Queue<Node> q = new LinkedList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        H = sc.nextInt();
        M = sc.nextInt();
        map = new int[N][N];
        answer = new int[N][N];
        step = new int[N][N];
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                map[i][j] = sc.nextInt();
            }
        }

        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(map[i][j]==2){
                    visited = new boolean[N][N];
                    goal=false;
                    push(i,j,0);
                    if(goal){
                        answer[i][j] = now;
                    }
                }
            }
        }

        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                System.out.print(answer[i][j]+" ");
            }
            System.out.println();
        }

    }

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

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

    public static void push(int x,int y,int c){
        map[x][y] = c;
        if(map[x][y]==3){
            goal = true;
            now = c;
        }
        visited[x][y] = true;
        q.add(new Node(x,y));
    }

    public static void BFS(){
        while(!q.isEmpty()){
            Node now = q.poll();
            for(int d=0;d<4;d++){
                int nx = now.x + dr[d];
                int ny = now.y + dc[d];
                if(canGo(nx,ny)){
                    push(nx,ny,step[now.x][now.y]+1);
                }
            }
        }
    }

    static class Node{
        int x,y;
        public Node(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
}

탈출조건을 push에 넣었던게 문제고
최단거리를 step에 갱신했어야 했는데 map에 자꾸 넣어서 틀렸었다.
조금만 길게 코드를 썼을 뿐인데 (사실 긴 것도 아님)
머리에 과부하가 오다니 ,,
아무튼 클리어

import java.util.*;

public class Main {
    public static int N, H, M, num, answer[][], map[][], step[][];
    public static int[] dr = {-1,0,1,0};
    public static int[] dc = {0,1,0,-1};
    public static boolean goal, visited[][];
    public static Queue<Node> q = new LinkedList<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        H = sc.nextInt();
        M = sc.nextInt();
        map = new int[N][N];
        answer = new int[N][N];
        step = new int[N][N];
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                map[i][j] = sc.nextInt();
            }
        }

        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(map[i][j]==2){
                    visited = new boolean[N][N];
                    step = new int[N][N];
                    goal=false;
                    push(i,j,0);
                    answer[i][j] = -1;
                    BFS();
                    if(goal){
                        answer[i][j] = num;
                    }
                    q = new LinkedList<>();
                }
            }
        }

        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                System.out.print(answer[i][j]+" ");
            }
            System.out.println();
        }

    }

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

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

    public static void push(int x,int y,int c){
        step[x][y] = c;
        visited[x][y] = true;
        q.add(new Node(x,y));
    }

    public static void BFS(){
        while(!q.isEmpty()){
            Node now = q.poll();
            for(int d=0;d<4;d++){
                int nx = now.x + dr[d];
                int ny = now.y + dc[d];
                if(canGo(nx,ny)){
                    if(map[nx][ny] == 3){
                        goal = true;
                        num = step[now.x][now.y] + 1;
                        return;
                    }
                    push(nx,ny,step[now.x][now.y]+1);
                }
            }
        }
    }

    static class Node{
        int x,y;
        public Node(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
}`
profile
개발자?

0개의 댓글