-
[백준][자바][5017]스타트링크코딩/알고리즘 2019. 10. 1. 21:49
bfs로 풀었다 .
bfs는 상태를 가지고 있다고 생각하면 쉽다 .
올라갔을때, 내려갔을때를 분기로 방문처리를 하면서 풀면 간단하다.
package back; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; class StartLink{ int y; int count; StartLink(int y, int count){ this.y = y; this.count = count; } } public class Main_backjoon_스타트링크 { static Queue q = new LinkedList(); static boolean VISITED[][] = new boolean[1000001][2]; static int F; static int G; static int S; static int U; static int D; public static void main(String[] args) { Scanner sc = new Scanner(System.in); F = sc.nextInt(); S = sc.nextInt(); G = sc.nextInt(); U = sc.nextInt(); D = sc.nextInt(); q.add(new StartLink(S, 0)); bfs(); } public static void bfs() { while(!q.isEmpty()) { int qsize = q.size(); for(int size = 0; size< qsize; size++) { StartLink s = q.poll(); if( s.y == G ) { System.out.println(s.count); return; } int yd = s.y - D; int yu = s.y + U; //System.out.println(yd + " " + yu); //내려 갈 수 있으면 if(isRange(yd) && !VISITED[yd][0]) { VISITED[yd][0] = true; q.add(new StartLink( yd, s.count + 1 )); } //올라 갈 수 있으면 if(isRange(yu) && !VISITED[yu][0]) { VISITED[yu][1] = true; q.add(new StartLink( yu, s.count + 1 )); } } } System.out.println("use the stairs"); } public static boolean isRange(int yy) { if( yy >= 0 && yy <= F) { return true; }else { return false; } } }
'코딩 > 알고리즘' 카테고리의 다른 글
[백준][자바][17135]캐슬디펜스 (0) 2019.10.23 [백준][자바][9205]맥주 마시면서 걸어가기 (0) 2019.10.02 [백준][자바][14442]벽부수고 이동하기 2 (0) 2019.09.11 [ 백준 ] [ 자바 ][ 1194] 달이 차오른다 가자 (0) 2019.09.08 프로그래머스 타켓넘버 java (0) 2019.09.02