β¨ BFS β λμμ μ΄λμν€κΈ°
https://www.acmicpc.net/problem/4179
4179λ²: λΆ!
μ λ ₯μ 첫째 μ€μλ 곡백μΌλ‘ ꡬλΆλ λ μ μ Rκ³Ό Cκ° μ£Όμ΄μ§λ€. λ¨, 1 ≤ R, C ≤ 1000 μ΄λ€. Rμ λ―Έλ‘ νμ κ°μ, Cλ μ΄μ κ°μμ΄λ€. λ€μ μ λ ₯μΌλ‘ Rμ€λμ κ°κ°μ λ―Έλ‘ νμ΄ μ£Όμ΄μ§λ€. κ°κ°μ λ¬Έμ
www.acmicpc.net
π κ³ λ €ν΄μΌν μ
- λΆκ³Ό μ§νμ΄λ₯Ό λμμ μ΄λμμΌμΌνλ€.
- λμμ μ΄λμν€κΈ° μν΄μ λ κ°μ νλ₯Ό μ¬μ©νλ€.
- λΆμ μ§νμ΄μ μ‘΄μ¬ μ¬λΆμ λν΄ μν₯μ λ°μ§ μλλ€. (μ§νμ΄κ° μκ±°λ μμ΄λ μΈμ λ μ§ λ²½λ§ μλλΌλ©΄ μΈμ λ μ§ μ ν κ°λ₯)
- μ§νμ΄λ λΆμ μ‘΄μ¬ μ¬λΆμ λν΄ μν₯μ λ°λλ€. (λΆμ΄ μ‘΄μ¬νλ€λ©΄ μ΄λ λΆκ°)
- μ§νμ΄κ° κ°μ₯μ리μ λλ¬νλ©΄ λ―Έλ‘λ₯Ό ν΅κ³Όν μ μλ€.
πΉοΈ νμ΄κ³Όμ
1οΈβ£ λ―Έλ‘μ μ 보λ₯Ό 2μ°¨μ λ°°μ΄μ μ μ₯νκ³ , μ§νμ΄μ μμΉμ λΆμ μμΉλ₯Ό κ° ν(jQ, fQ)μ μ μ₯νλ€.
2οΈβ£ λΆμ λ¨Όμ 4 λ°©ν₯μ μ νμν¨λ€. (λ°©λ¬Έ μ¬λΆλ ν΄λΉ 격μμ μλ λ¬ΈμνμΌλ‘λ νλ¨ν μ μκΈ° λλ¬Έμ visit[][] λ°°μ΄μ ꡬν X)
3οΈβ£ μ§νμ΄λ₯Ό 4 λ°©ν₯ μ€ λΆμ΄ μ‘΄μ¬νμ§ μκ³ , λ²½μ΄ μλ '.' κ³³μΌλ‘ μ΄λμν¨λ€.
4οΈβ£ λ§μ½ μ΄λν κ³³μ΄ λ―Έλ‘μ λ²μλ₯Ό λ²μ΄λ κ³³μ΄λΌλ©΄ μ§νμ΄λ νμΆν μ μκΈ° λλ¬Έμ νμΆμκ°μ μΆλ ₯νκ³ λλΈλ€.
5οΈβ£ λμ΄μ μ§νμ΄κ° μ΄λν μ μλ€λ©΄ ν(jQ)κ° λΉμ΄μκΈ° λλ¬Έμ "IMPOSSIBLE"μ μΆλ ₯νκ³ λλΈλ€.
6οΈβ£ λ―Έλ‘μ λ²μλ₯Ό λ²μ΄λμ§ μμλ€λ©΄ jQμ μ΄λν κ³³μ λ£κ³ μ’ λ£ μ‘°κ±΄μ λ§μ‘±ν λκΉμ§ λ°λ³΅νλ€.
π©π» μ 체 μ½λ
public class BOJ_4179_λΆ {
static int r, c, cnt;
static char[][] maze;
static int[] dx = {0, 0, 1, -1};
static int[] dy = {1, -1, 0, 0};
static Queue<Info> jQ = new LinkedList<>();
static Queue<Info> fQ = new LinkedList<>();
static class Info{
int x, y;
public Info(int x, int y){
this.x=x;
this.y=y;
}
}
static void bfs(){
while(true){
cnt++; // νμΆ μμ μκ°
int fs = fQ.size();
// λΆ μ νμν€κΈ°
while(fs-->0){
Info fN = fQ.poll();
// λ€ λ°©ν₯μΌλ‘ λΆ μ ν
for(int i=0; i<4; i++){
int fnx = fN.x + dx[i];
int fny = fN.y + dy[i];
if(fnx>=0 && fny>=0 && fnx<r && fny<c){
if(maze[fnx][fny]=='J' || maze[fnx][fny]=='.'){
maze[fnx][fny]='F';
fQ.add(new Info(fnx, fny));
}
}
}
}
// μ§νμ΄ μ΄λμν€κΈ°
int js = jQ.size();
while(js-->0){
Info jN = jQ.poll();
for(int i=0; i<4; i++){
int jnx = jN.x + dx[i];
int jny = jN.y + dy[i];
if(jnx>=0 && jny>=0 && jnx<r && jny<c){
if(maze[jnx][jny]=='.'){
maze[jnx][jny] = 'J';
jQ.add(new Info(jnx, jny));
}
}
// μ§νμ΄κ° λ―Έλ‘ λ²μλ₯Ό λ²μ΄λ¬λ€λ©΄ ν΅κ³ΌνμΌλ―λ‘ μΆλ ₯ ν λλ΄κΈ°
else{
System.out.println(cnt);
return;
}
}
}
// μ§νμ΄κ° μ΄λν κ³³μ΄ μλ€λ©΄ IMPOSSIBLE μΆλ ₯
if(jQ.isEmpty()){
System.out.println("IMPOSSIBLE");
return;
}
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
maze = new char[r][c];
for(int i=0; i<r; i++){
String str = br.readLine();
for(int j=0; j<c; j++){
maze[i][j] = str.charAt(j);
if(maze[i][j] == 'J'){
jQ.add(new Info(i, j));
} else if(maze[i][j]=='F'){
fQ.add(new Info(i,j));
}
}
}
bfs();
}
}
'μκ³ λ¦¬μ¦ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 1504 νΉμ ν μ΅λ¨ κ²½λ‘ - Java (0) | 2023.12.05 |
---|---|
[BOJ] 18352 νΉμ 거리μ λμ μ°ΎκΈ° - Java (0) | 2023.12.04 |
[BOJ] 14502 μ°κ΅¬μ - Java (0) | 2023.09.28 |
[BOJ] 3190 λ± (0) | 2023.09.25 |
[BOJ] 11657 νμλ¨Έμ (1) | 2023.09.08 |