https://www.acmicpc.net/problem/6087
6087번: 레이저 통신
문제 크기가 1×1인 정사각형으로 나누어진 W×H 크기의 지도가 있다. 지도의 각 칸은 빈 칸이거나 벽이며, 두 칸은 'C'로 표시되어 있는 칸이다. 'C'로 표시되어 있는 두 칸을 레이저로 통신하기 위해서 설치해야 하는 거울 개수의 최솟값을 구하는 프로그램을 작성하시오. 레이저로 통신한다는 것은 두 칸을 레이저로 연결할 수 있음을 의미한다. 레이저는 C에서만 발사할 수 있고, 빈 칸에 거울('/', '\')을 설치해서 방향을 90도 회전시킬 수 있다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
class Pos implements Comparable<Pos>{
int x;
int y;
int d;
int c;
public Pos(int x, int y, int d, int c) {
super();
this.x = x;
this.y = y;
this.d = d;
this.c = c;
}
@Override
public int compareTo(Pos o) {
return Integer.compare(this.c, o.c);
}
}
public class Main_bj_6087_레이저통신 {
static int R,C;
static char[][] map;
static boolean[][][] visit;
static int[] dx= {0,-1,0,1};
static int[] dy= {-1,0,1,0};
//좌상우백
static int[] dr= {1,2,3,0};
static int[] dl= {3,0,1,2};
static int sx,sy,ex,ey;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine()," ");
C=Integer.parseInt(st.nextToken());//열
R=Integer.parseInt(st.nextToken());//행
map=new char[R][C];
visit=new boolean[R][C][4];
boolean f=false;
for(int i=0; i<R; i++) {
String s=br.readLine();
for(int j=0; j<C; j++){
map[i][j]=s.charAt(j);
if(map[i][j]=='C') {
if(f==false) {
f=true;
sx=i; sy=j;
}else {
ex=i; ey=j;
}
map[i][j]='.';
}
}
}//입력끝
PriorityQueue<Pos> pq=new PriorityQueue<Pos>();
for(int i=0; i<4; i++) {
visit[sx][sy][i]=true;
pq.offer(new Pos(sx,sy,i,0));
}
while(!pq.isEmpty()) {
Pos p=pq.poll();
if(p.x==ex && p.y==ey) {
System.out.println(p.c);
break;
}
int nx=p.x+dx[p.d];
int ny=p.y+dy[p.d];
if(nx<0||ny<0||nx>=R||ny>=C||visit[nx][ny][p.d] || map[nx][ny]=='*') {
continue;
}
visit[nx][ny][p.d]=true;
pq.offer(new Pos(nx,ny,dr[p.d],p.c+1));
pq.offer(new Pos(nx,ny,dl[p.d],p.c+1));
pq.offer(new Pos(nx,ny,p.d,p.c));
}
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2225] 합분해(JAVA) (0) | 2020.04.07 |
---|---|
[백준 17404] RGB거리2(JAVA) (0) | 2020.04.04 |
[백준 1072] 게임(JAVA) (2) | 2020.04.02 |
[백준 11725] 트리의부모찾기(JAVA) (0) | 2020.04.01 |
[백준 1405] 미친 로봇(JAVA/C++) (0) | 2020.03.30 |
댓글