[BOJ 1103] ์คํํธ๋งํฌ
1. ๋ฌธ์ : https://www.acmicpc.net/problem/1103
2. ํ์ด
+, - ๋ ๋ค queue์ ๋ฃ๊ธฐ
์ง์ ๋ํ๊ฑฐ๋ ๋บ ํ ๋น๊ตํ์ง ์๊ณ ๋ํ / ๋บ ๊ฐ์ผ๋ก ๋น๊ตํ๊ธฐ
3. ์ฝ๋
package baekjoon;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class ์คํํธ๋งํฌ {
static int f, s, g, u, d;
static boolean[] visited;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
f = scan.nextInt();
s = scan.nextInt();
g = scan.nextInt();
u = scan.nextInt();
d = scan.nextInt();
visited = new boolean[f + 1];
int result = bfs();
if(result < 0) System.out.println("use the stairs");
else System.out.println(result);
}
public static int bfs() {
Queue<Node> q = new LinkedList<>();
q.offer(new Node(s, 0));
visited[s] = true;
while(!q.isEmpty()) {
Node current = q.poll();
if(current.x == g) return current.cost;
if(current.x + u <= f && visited[current.x + u] == false) {
q.offer(new Node(current.x + u, current.cost + 1));
visited[current.x + u] = true;
}
if(current.x - d >= 1 && visited[current.x - d] == false) {
q.offer(new Node(current.x - d, current.cost + 1));
visited[current.x - d] = true;
}
}
return -1;
}
public static class Node {
int x;
int cost;
public Node(int x, int cost) {
this.x = x;
this.cost = cost;
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 2206] ๋ฒฝ๋ถ์๊ณ ์ด๋ํ๊ธฐ (0) | 2023.08.21 |
---|---|
[BOJ 16234] ์ธ๊ตฌ์ด๋ (0) | 2023.08.17 |
[BOJ 9466] ํ ํ๋ก์ ํธ (0) | 2023.08.01 |
[BOJ 2668] ์ซ์๊ณ ๋ฅด๊ธฐ (0) | 2023.06.27 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ด๋ฌผ์บ๊ธฐ (์๋ฐ java) (0) | 2023.05.18 |
์ต๊ทผ๋๊ธ