알고리즘
[Java] 백준 6087번 레이저 통신
J3SUNG
2023. 3. 7. 23:50
728x90
레이저가 다른쪽 레이저를 만나는데 필요한 거울의 수를 구하는 문제입니다.
거울은 레이저를 오른쪽, 왼쪽으로 꺾을 수 있습니다.
한 쪽 레이저에서 BFS로 탐색을 합니다.
레이저를 중심으로 직선방향으로 이동하며 거울이 필요하지 않습니다.
벽이 아닌 빈공간인 경우 다시 큐에 넣어주며 cnt를 1늘려줍니다. (거울이 +1개 필요)
위의 과정을 반복하여 다른 쪽 레이저를 만날때 까지 반복합니다.
다른 쪽 레이저를 만나게 되었을 때 cnt를 출력해주었습니다. (거울의 수)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
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());
Queue<Data> q = new LinkedList<>();
int ans = 0;
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
int[][] map = new int[h][w];
int[] dy = {0, 1, 0, -1};
int[] dx = {1, 0, -1, 0};
int ay = 0;
int ax = 0;
for (int i = 0; i < h; ++i) {
String str = br.readLine();
for (int j = 0; j < w; ++j) {
map[i][j] = (str.charAt(j) == '.' ? -1 : (str.charAt(j) == '*' ? -2 : -3));
if(map[i][j] == -3) {
ay = i;
ax = j;
}
}
}
map[ay][ax] = 0;
q.add(new Data(ay, ax, 0));
while(!q.isEmpty()) {
Data p = q.poll();
for(int i=0; i<4; ++i) {
int nextY = p.y;
int nextX = p.x;
while(true) {
nextY += dy[i];
nextX += dx[i];
if(nextY < 0 || nextX < 0 || nextY >= h || nextX >= w) {
break;
}
if(map[nextY][nextX] == -3) {
ans = p.cnt;
while(!q.isEmpty()) {
q.poll();
}
break;
} else if(map[nextY][nextX] == -2) {
break;
} else if(map[nextY][nextX] == -1) {
map[nextY][nextX] = p.cnt;
q.add(new Data(nextY, nextX, p.cnt + 1));
} else if(map[nextY][nextX] >= 0 && map[nextY][nextX] != p.cnt) {
break;
}
}
}
}
bw.write(ans + "");
bw.close();
}
public static class Data {
public int y;
public int x;
public int cnt;
public Data(int y, int x, int cnt) {
this.y = y;
this.x = x;
this.cnt = cnt;
}
}
}