ํน์ดํ bfs๋ฌธ์
ํต์ฌ ๋ก์ง
1. ๊ตฌ์ฌ์ ๋์์ ์์ง์ด๊ธฐ
2. ๋ฒฝ์ ๋ฟ๊ธฐ ์ง์ ๊น์ง ์ต๋ํ ์์ง์ด๊ธฐ
3. ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ, ํ๋ ๊ตฌ์ฌ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌ
1. ๊ตฌ์ฌ์ ๋์์ ์์ง์ด๊ธฐ
๋นจ๊ฐ๊ณต๊ณผ ํ๋๊ณต์ ์ขํ๋ฅผ ๊ฐ์ด ๊ฐ์ง๊ณ ์๋ Marble ํด๋์ค๋ฅผ ๋ง๋ค์๋ค.
class Marble{
private int redRow,redCol,blueRow,blueCol;
public Marble(){}
public Marble(int redRow, int redCol, int blueRow, int blueCol){
this.redRow = redRow;
this.redCol = redCol;
this.blueRow = blueRow;
this.blueCol = blueCol;
}
public int getRedRow(){
return this.redRow;
}
public int getRedCol(){
return this.redCol;
}
public int getBlueRow(){
return this.blueRow;
}
public int getBlueCol(){
return this.blueCol;
}
public void setRedRow(int row){
this.redRow = row;
}
public void setRedCol(int col){
this.redCol = col;
}
public void setBlueRow(int row){
this.blueRow = row;
}
public void setBlueCol(int col){
this.blueCol = col;
}
}
2. ๋ฒฝ์ ๋ฟ๊ธฐ ์ง์ ๊น์ง ์ต๋ํ ์์ง์ด๊ธฐ
ํ์ฌ ๋ฐฉํฅ์ผ๋ก ์ต๋ํ ์์ง์ด๋ก while(true)๋ฌธ์ ํ์ฉํ๋ค.
for(int d=0;d<4;d++){
int nextRedRow = curRedRow + dr[d];
int nextRedCol = curRedCol + dc[d];
int nextBlueRow = curBlueRow + dr[d];
int nextBlueCol = curBlueCol + dc[d];
while(true){ // ๋ฒฝ์ ๋ฟ๊ธฐ ์ง์ ๊น์ง ์์ง์ด๊ธฐ ์ํด์
if(arr[nextRedRow][nextRedCol].equals("#")){ // ๋ฒฝ์ด๋ ๊ฒน์น๋ฉด
nextRedRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
nextRedCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
break;
}
if(arr[nextRedRow][nextRedCol].equals("O")){ // ๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ๋ ์ด์ ์์ง์ผ ํ์๊ฐ ์์
break;
}
nextRedRow += dr[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
nextRedCol += dc[d];// ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
}
while(true){
if(arr[nextBlueRow][nextBlueCol].equals("#")){ // ๋ฒฝ์ด๋ ๊ฒน์น๋ฉด
nextBlueRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
nextBlueCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
break;
}
if(arr[nextBlueRow][nextBlueCol].equals("O")){ // ๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ๋ ์ด์ ์์ง์ผ ํ์๊ฐ ์์
break;
}
nextBlueRow += dr[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
nextBlueCol += dc[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
}
...
}
while(true)์์์ ๋ฒฝ๊ณผ ๊ตฌ์ฌ์ ์ขํ๊ฐ ๊ฒน์น๋ฉด, ํ์นธ ์ง์ ์์น๋ก ์์ง์ฌ ์คฌ๊ณ .
๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ๋ ์ด์ ์์ง์ผ ์๊ฐ ์์ผ๋ break๋ฌธ์ผ๋ก while(true)๋ฌธ์ ๋น ์ ธ๋๊ฐ๋ค.
3. ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ, ํ๋ ๊ตฌ์ฌ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌ
๋ ๋จผ๊ฑฐ๋ฆฌ๋ฅผ ์ด๋ํ ๊ตฌ์ฌ์ด ๋ค์ ์ถ๋ฐํ ๊ตฌ์ฌ์ด๋ค.
์๋ฅผ๋ค์ด ์ด๋ ๋ฐฉํฅ์ด ์ผ์ชฝ์ธ ๊ฒฝ์ฐ
๐
๋นจ๊ฐ๊ณต์ ์ด๋๊ฑฐ๋ฆฌ : 3
ํ๋๊ณต์ ์ด๋๊ฑฐ๋ฆฌ : 4
๋ ๋จผ๊ฑฐ๋ฆฌ๋ฅผ ์ด๋ํ ํ๋๊ณต์ด ๋ค์ ์ถ๋ฐํ ๊ตฌ์ฌ์ด๋ค.
๋ฐ๋ผ์ ํ๋๊ณต์ ์ง์ ์์น๋ก ํ ์นธ ์์ง์ฌ ์ค์ผ ํ๋ค.
์ ๋ด์ฉ์ ์ฝ๋๋ก ๊ตฌํ
if(nextRedRow==nextBlueRow && nextRedCol==nextBlueCol){ // ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ ํ๋ ๊ตฌ์ฌ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ
int redMoved = Math.abs(nextRedRow-curRedRow) + Math.abs(nextRedCol-curRedCol); // ๋นจ๊ฐ ๊ตฌ์ฌ์ด ์์ง์ธ ๊ฑฐ๋ฆฌ
int blueMoved = Math.abs(nextBlueRow-curBlueRow) + Math.abs(nextBlueCol-curBlueCol); // ํ๋ ๊ตฌ์ฌ์ด ์์ง์ธ ๊ฑฐ๋ฆฌ
if(redMoved > blueMoved){ // ๋ ๋จผ๊ฑฐ๋ฆฌ๋ฅผ ์์ง์ธ ๊ตฌ์ฌ์ด ์งํ ๋ฐฉํฅ์์ ๋ดค์ ๋ ๋ค์์ ์ถ๋ฐํ ๊ตฌ์ฌ์
nextRedRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
nextRedCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
}else{
nextBlueRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
nextBlueCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
}
}
๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ์ํด 4์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉ, ๊ตฌ์ฌ์ ๊ฐ์ด ์ด๋, ํ ์นธ์ฉ ์์ง์ด๋๊ฒ ์๋ ์ต๋ํ ์์ง์ด๊ฒ ํ๋ค๋ ์ ์์ ์๋ก์ด bfs๋ฌธ์ ์๋ค.
์ ์ฒด์ฝ๋
package com.mincheolsong;
import java.io.*;
import java.util.*;
// ๊ตฌ์ฌ(ํ๊บผ๋ฒ์) ๊ธฐ์ธ์ด๊ธฐ
// ์ด๋ํ ๋ ํ์ ์นด์ดํธํ๊ธฐ
// ๊ฐ์ด ๊ตฌ๋ฉ์ ๋น ์ง๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
class Marble{
private int redRow,redCol,blueRow,blueCol;
public Marble(){}
public Marble(int redRow, int redCol, int blueRow, int blueCol){
this.redRow = redRow;
this.redCol = redCol;
this.blueRow = blueRow;
this.blueCol = blueCol;
}
public int getRedRow(){
return this.redRow;
}
public int getRedCol(){
return this.redCol;
}
public int getBlueRow(){
return this.blueRow;
}
public int getBlueCol(){
return this.blueCol;
}
public void setRedRow(int row){
this.redRow = row;
}
public void setRedCol(int col){
this.redCol = col;
}
public void setBlueRow(int row){
this.blueRow = row;
}
public void setBlueCol(int col){
this.blueCol = col;
}
}
public class Main {
static int[] dr = {-1,0,1,0};
static int[] dc = {0,1,0,-1};
static int N,M;
static String[][] arr;
static Marble marble; // ๋นจ๊ฐ๊ณต๊ณผ ํ๋๊ณต์ ์ขํ๋ฅผ ๋์์ ๊ฐ์ง๊ณ ์๋ Marble ๊ฐ์ฒด
static boolean[][][][] visited; // ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ์ํ 4์ฐจ์ ๋ฐฐ์ด (๋นจ๊ฐ๊ณต row, ๋นจ๊ฐ๊ณต col, ํ๋๊ณต row, ํ๋๊ณต col)
static void solve(){
Deque<Marble> q = new ArrayDeque();
q.offer(marble);
visited[marble.getRedRow()][marble.getRedCol()][marble.getBlueRow()][marble.getBlueCol()] = true;
int count = 0;
while(!q.isEmpty()){
int size = q.size(); // bfs๋ฅผ ํ ํด์ฉ ์ํํ๊ธฐ ์ํด์
for(int i=0;i<size;i++){ // ํ ํด์ฉ(size๋งํผ) ์ํ
Marble cur = q.pollFirst();
if(arr[cur.getRedRow()][cur.getRedCol()].equals("O")){ // bfs์ ํน์ฑ์ ์ฒ์ ๋นจ๊ฐ ๊ณต์ด ๊ตฌ๋ฉ์ ๋น ์ง๋ ๊ฒฝ์ฐ๊ฐ ์ต๋จ ์๊ฐ
System.out.println(count);
return;
}
int curRedRow = cur.getRedRow();
int curRedCol = cur.getRedCol();
int curBlueRow = cur.getBlueRow();
int curBlueCol = cur.getBlueCol();
for(int d=0;d<4;d++){
int nextRedRow = curRedRow + dr[d];
int nextRedCol = curRedCol + dc[d];
int nextBlueRow = curBlueRow + dr[d];
int nextBlueCol = curBlueCol + dc[d];
while(true){ // ๋ฒฝ์ ๋ฟ๊ธฐ ์ง์ ๊น์ง ์์ง์ด๊ธฐ ์ํด์
if(arr[nextRedRow][nextRedCol].equals("#")){ // ๋ฒฝ์ด๋ ๊ฒน์น๋ฉด
nextRedRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
nextRedCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
break;
}
if(arr[nextRedRow][nextRedCol].equals("O")){ // ๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ๋ ์ด์ ์์ง์ผ ํ์๊ฐ ์์
break;
}
nextRedRow += dr[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
nextRedCol += dc[d];// ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
}
while(true){
if(arr[nextBlueRow][nextBlueCol].equals("#")){ // ๋ฒฝ์ด๋ ๊ฒน์น๋ฉด
nextBlueRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
nextBlueCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์ฎ๊ธฐ๊ธฐ
break;
}
if(arr[nextBlueRow][nextBlueCol].equals("O")){ // ๊ตฌ๋ฉ์ ๋น ์ง๋ฉด ๋ ์ด์ ์์ง์ผ ํ์๊ฐ ์์
break;
}
nextBlueRow += dr[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
nextBlueCol += dc[d]; // ํ์ฌ ๋ฐฉํฅ(d)๋ก ๊ณ์ ์์ง์ด๊ธฐ
}
if(arr[nextBlueRow][nextBlueCol].equals("O")){ // ๋ง์ฝ ํ๋ ๊ตฌ์ฌ์ด ๊ตฌ๋ฉ์ ๋น ์ง๋ ๊ฒฝ์ฐ๋ ์๋ชป๋ ๊ฒฝ์ฐ. ํ์ ๋ฃ์ง ์๋๋ค.
continue;
}
if(nextRedRow==nextBlueRow && nextRedCol==nextBlueCol){ // ๋นจ๊ฐ ๊ตฌ์ฌ๊ณผ ํ๋ ๊ตฌ์ฌ์ด ๊ฒน์น๋ ๊ฒฝ์ฐ
int redMoved = Math.abs(nextRedRow-curRedRow) + Math.abs(nextRedCol-curRedCol); // ๋นจ๊ฐ ๊ตฌ์ฌ์ด ์์ง์ธ ๊ฑฐ๋ฆฌ
int blueMoved = Math.abs(nextBlueRow-curBlueRow) + Math.abs(nextBlueCol-curBlueCol); // ํ๋ ๊ตฌ์ฌ์ด ์์ง์ธ ๊ฑฐ๋ฆฌ
if(redMoved > blueMoved){ // ๋ ๋จผ๊ฑฐ๋ฆฌ๋ฅผ ์์ง์ธ ๊ตฌ์ฌ์ด ์งํ ๋ฐฉํฅ์์ ๋ดค์ ๋ ๋ค์์ ์ถ๋ฐํ ๊ตฌ์ฌ์
nextRedRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
nextRedCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
}else{
nextBlueRow -= dr[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
nextBlueCol -= dc[d]; // ํ์นธ ์ง์ ์์น๋ก ์์ง์ด๊ธฐ
}
}
if(!visited[nextRedRow][nextRedCol][nextBlueRow][nextBlueCol]) { // ์ด๋ฏธ ๋ฐฉ๋ฌธํ๋ (๋นจ๊ฐ๊ณตrow, ๋นจ๊ฐ๊ณตcol, ํ๋๊ณตrow, ํ๋๊ณตcol) ์์น๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง
q.offer(new Marble(nextRedRow, nextRedCol, nextBlueRow, nextBlueCol)); // ํ์ ๋ฃ๊ณ
visited[nextRedRow][nextRedCol][nextBlueRow][nextBlueCol] = true; // ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
}
}
}
count+=1; // ์์ง์ธ ํ์ ์นด์ดํธ
if(count==11){ // 11๋ฒ ์์ง์ธ ๊ฒฝ์ฐ๋ผ๋ฉด, ๋นผ๋ผ ์ ์๋ ๊ฒฝ์ฐ
System.out.println(-1);
return;
}
}
System.out.println(-1); // ์ต๋ํ ์์ง์ธ ํ์๊ฐ 11๋ฒ ๋ฏธ๋ง์ด๊ณ , ๊ตฌ๋ฉ์ผ๋ก ๋นผ๋ด์ง ๋ชปํ์ผ๋ฉด -1์ถ๋ ฅ
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new String[N][M];
marble = new Marble();
visited = new boolean[N][M][N][M];
for(int i=0;i<N;i++){
String input = br.readLine();
int j=0;
for(char ch : input.toCharArray()){
arr[i][j] = ch + "";
if(arr[i][j].equals("R")){
marble.setRedRow(i);
marble.setRedCol(j);
}else if(arr[i][j].equals("B")){
marble.setBlueRow(i);
marble.setBlueCol(j);
}
j+=1;
}
}
solve();
}
}
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑํธ๋ํน(14890 ๊ฒฝ์ฌ๋ก) (2) | 2024.02.14 |
---|---|
BFS (๋ฐฑ์ค 16234 ์ธ๊ตฌ์ด๋) (1) | 2024.02.09 |
๋ฒจ๋ง ํฌ๋ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.01.12 |
์๋ก์ ์งํฉ ์๋ฃ๊ตฌ์กฐ (Union Find) (๋ฐฑ์ค 1043๋ฒ) (0) | 2024.01.03 |
๋ค์ต์คํธ๋ผ (boj 12851 ์จ๋ฐ๊ผญ์ง2) (0) | 2024.01.02 |