16173번: 점프왕 쩰리 (Small)
쩰리는 맨 왼쪽 위의 칸에서 출발해 (행, 열)로 나타낸 좌표계로, (1, 1) -> (2, 1) -> (3, 1) -> (3, 3)으로 이동해 게임에서 승리할 수 있다.
www.acmicpc.net
👇 내가 작성한 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int N;
static int[] dr = {1, 0};
static int[] dc = {0, 1};
static int[][] game;
static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st;
game = new int[N][N];
visited = new boolean[N][N];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N; j++) {
game[i][j] = Integer.parseInt(st.nextToken());
}
}
BFS(0, 0);
System.out.println(visited[N - 1][N - 1] == true ? "HaruHaru" : "Hing");
}
public static void BFS(int x, int y) {
Queue<int[]> q = new LinkedList<>();
visited[x][y] = true;
q.offer(new int[]{x, y});
if(x == N - 1 && y == N - 1) {
visited[N - 1][N - 1] = true;
return;
}
while(!q.isEmpty()) {
int[] cur = q.poll();
int curX = cur[0];
int curY = cur[1];
int jump = game[curX][curY];
for(int d = 0; d < 2; d++) {
int nx = curX + dr[d] * jump;
int ny = curY + dc[d] * jump;
if(nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny]) {
visited[nx][ny] = true;
q.offer(new int[]{nx, ny});
}
}
}
}
}
느낀 점 및 정리 ✍️
1. BFS를 이용하여 풀었다
'Algorithm & SQL > BOJ' 카테고리의 다른 글
[백준 1972번 / Java] BOJ 1972 : 놀라운 문자열 (0) | 2024.03.26 |
---|---|
[백준 2210번 / Java] BOJ 2210 : 숫자판 점프(DFS) (0) | 2024.03.24 |
[백준 2309번 / Java] BOJ 2309 : 일곱 난쟁이 (0) | 2024.03.16 |
[백준 3649번 / Java] BOJ 3649 : 로봇 프로젝트(투 포인터) (2) | 2024.03.12 |
[백준 2343번 / Java] BOJ 2343 : 기타 레슨(이분 탐색) (0) | 2024.03.10 |