• 문제 링크
6087번: 레이저 통신
크기가 1×1인 정사각형으로 나누어진 W×H 크기의 지도가 있다. 지도의 각 칸은 빈 칸이거나 벽이며, 두 칸은 'C'로 표시되어 있는 칸이다. 'C'로 표시되어 있는 두 칸을 레이저로 통신하기 위해서
www.acmicpc.net
• 풀이 과정
재채점에 의해 방문 배열을 3차원으로 변경하고, bfs 탐색 범위를 부분적 수정하였다.
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int w, h;
static char[][] map;
static int[][][] visit;
static int[] dy = {-1, 1, 0, 0};
static int[] dx = {0, 0, -1, 1};
static Node src, dest;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
map = new char[h][w];
visit = new int[h][w][4];
for (int i = 0; i < h; i++) {
map[i] = br.readLine().toCharArray();
for (int j = 0; j < w; j++) {
for (int k = 0; k < 4; k++)
visit[i][j][k] = Integer.MAX_VALUE;
if (map[i][j] == 'C') {
if (src == null)
src = new Node(i, j, -1, 0);
else
dest = new Node(i, j, -1, 0);
}
}
}
bw.write(String.valueOf(bfs()));
bw.flush();
}
private static int bfs() {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(src);
int min = Integer.MAX_VALUE;
while (!pq.isEmpty()) {
Node cur = pq.poll();
if (cur.y == dest.y && cur.x == dest.x) {
min = Math.min(min, cur.cnt);
continue;
}
for (int i = 0; i < 4; i++) {
int ny = cur.y + dy[i];
int nx = cur.x + dx[i];
int nCnt = cur.cnt;
if (ny < 0 || ny >= h || nx < 0 || nx >= w || map[ny][nx] == '*')
continue;
if (cur.dir != i && cur.dir != -1)
nCnt++;
if (visit[ny][nx][i] <= nCnt)
continue;
visit[ny][nx][i] = nCnt;
pq.offer(new Node(ny, nx, i, nCnt));
}
}
return min;
}
private static class Node implements Comparable<Node> {
int y, x, dir, cnt;
public Node(int y, int x, int dir, int cnt) {
this.y = y;
this.x = x;
this.dir = dir;
this.cnt = cnt;
}
@Override
public int compareTo(Node o) {
return cnt - o.cnt;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 10026 적록색약 - Graph Theory / Java (0) | 2022.10.13 |
---|---|
[백준] 1963 소수 경로 - Graph Theory / Java (0) | 2022.10.12 |
[백준] 16954 움직이는 미로 탈출 - Graph Theory / Java (0) | 2022.10.10 |
[백준] 2206 벽 부수고 이동하기 - Graph Theory / Java (0) | 2022.10.09 |
[백준] 12886 돌 그룹 - Graph Theory / Java (0) | 2022.10.08 |
댓글