• 문제 링크
25416번: 빠른 숫자 탐색
5 x 5 크기의 보드가 주어진다. 보드는 1 x 1 크기의 정사각형 격자로 이루어져 있다. 보드의 격자에는 -1, 0, 1중 하나의 숫자가 적혀 있다. 격자의 위치는 (r, c)로 표시한다. r은 행 번호, c는 열 번호
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int[][] arr = new int[5][5];
static int[] dy = {-1, 1, 0, 0}, dx = {0, 0, -1, 1};
static int cnt = -1;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++) arr[i][j] = read();
bfs(new Node(read(), read(), 0));
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void bfs(Node src) {
Queue<Node> q = new LinkedList<>();
q.offer(src);
arr[src.y][src.x] = -1;
while (!q.isEmpty()) {
Node cur = q.poll();
for (int i = 0; i < 4; i++) {
int ny = cur.y + dy[i], nx = cur.x + dx[i];
if (ny < 0 || ny > 4 || nx < 0 || nx > 4 || arr[ny][nx] == -1) continue;
if (arr[ny][nx] == 1) {
cnt = cur.cnt + 1;
return;
}
q.offer(new Node(ny, nx, cur.cnt + 1));
arr[ny][nx] = -1;
}
}
}
private static class Node {
int y, x, cnt;
public Node(int y, int x, int cnt) {
this.y = y;
this.x = x;
this.cnt = cnt;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
boolean flag = n == 13;
if (flag) n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return flag ? ~n + 1 : n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 3098 소셜네트워크 - Graph Theory / Java (0) | 2023.08.04 |
---|---|
[백준] 9204 체스 - Graph Theory / Java (0) | 2023.08.03 |
[백준] 26111 Parentheses Tree - Data Structure / Java (0) | 2023.08.01 |
[백준] 24393 조커 찾기 - Data Structure / Java (0) | 2023.07.31 |
[백준] 4848 집합 숫자 표기법 - Data Structure / Java (0) | 2023.07.30 |
댓글