Dev.baelanche

[백준 5014] 스타트링크 본문

Data Structure & Algorithm/PS - JAVA

[백준 5014] 스타트링크

baelanche 2019. 4. 18. 20:22
반응형

 

1차원 배열에서 방문 이력을 기록하면서 이동하는 문제이다.

변수 D 를 음수로 쓰는것만 까먹지 않으면 된다.

 

 

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int f = sc.nextInt();
        int s = sc.nextInt();
        int g = sc.nextInt();
        int u = sc.nextInt();
        int d = sc.nextInt();
        
        Queue<Integer> queue = new LinkedList<Integer>();
        int a[] = new int[f+1];
        boolean visited[] = new boolean[f+1];
        
        int mx[] = {u, -d};
        boolean success = false;
        
        queue.offer(s);
        a[s]= 0;
        visited[s] = true;
        while(!queue.isEmpty()) {
            int size = queue.size();
            
            while(size-->0) {
                int me = queue.poll();
                if(me == g) success = true;
                for(int i=0; i<2; i++) {
                    int nx = me + mx[i];
                    if(1 <= nx && nx < f+1) {
                        if(!visited[nx]) {
                            visited[nx] = true;
                            queue.offer(nx);
                            a[nx] = a[me] + 1;
                        }
                    }
                }
            }
        }
        System.out.println(success == true ? a[g] : "use the stairs");
    }
}
반응형

'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글

[백준 2864] 5와 6의 차이  (0) 2019.04.18
[백준 1697] 숨바꼭질  (0) 2019.04.18
[백준 7576] 토마토  (0) 2019.04.18
[백준 2206] 벽 부수고 이동하기  (0) 2019.04.18
[백준 3055] 탈출  (0) 2019.04.18
Comments