๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/9204)
< ์ฒด์ค >
๋ฌธ์ ํ์ด
bfs๋ฅผ ํ์ฉํ์ฌ ๋๊ฐ์ ์ผ๋ก ์ด๋๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ํ์ํ๋ค. ์ด๋ํ๋ฉด์ ์ด๋ ์์น๋ ๊ฐ์ด ์ ์ฅํด ๋๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class _9204_ { // ์ฒด์ค
static int dx[] = { -1, -1, 1, 1 };
static int dy[] = { -1, 1, -1, 1 };
static class Bishop {
private int x;
private char y;
private int cnt;
private ArrayList<Bishop> list;
public Bishop(int x, char y, int cnt, ArrayList<Bishop> list) {
this.x = x;
this.y = y;
this.cnt = cnt;
this.list = list;
}
public Bishop(int x, char y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int T = Integer.parseInt(bf.readLine());
for (int i = 0; i < T; i++) {
st = new StringTokenizer(bf.readLine());
char y1 = st.nextToken().charAt(0);
int x1 = Integer.parseInt(st.nextToken());
char y2 = st.nextToken().charAt(0);
int x2 = Integer.parseInt(st.nextToken());
if(x1==x2 && y1==y2) {
bw.write("0 "+y1+" "+x1+"\n");
}else {
ArrayList<Bishop> temp = bfs(x1, y1, x2, y2);
if (temp == null) {
bw.write("Impossible\n");
}else {
bw.write((temp.size()+1) + " "+y1+" "+x1+" ");
for (Bishop bishop : temp) {
bw.write(bishop.y + " " + bishop.x + " ");
}
bw.write(y2+" "+x2+"\n");
}
}
}
bw.flush();
}
private static ArrayList<Bishop> bfs(int x1, char y1, int x2, char y2) {
boolean visited[][] = new boolean[8][8];
Queue<Bishop> queue = new LinkedList<>();
visited[8 - x1][y1 - 'A'] = true;
queue.add(new Bishop(x1, y1, 0, new ArrayList<>()));
int resultX = 8 - x2;
int resultY = y2 - 'A';
while (!queue.isEmpty()) {
Bishop bishop = queue.poll();
for (int i = 0; i < 4; i++) {
int y = bishop.y - 'A';
int x = 8 - bishop.x;
while (x >= 0 && x < 8 && y >= 0 && y < 8) {
x += dx[i];
y += dy[i];
if (x >= 0 && x < 8 && y >= 0 && y < 8 && !visited[x][y]) {
visited[x][y] = true;
ArrayList<Bishop> temp=new ArrayList<>();
for(Bishop b: bishop.list) {
temp.add(b);
}
if (resultX == x && resultY == y) {
return temp;
}
if (bishop.cnt + 1 == 4) {
return null;
}
temp.add(new Bishop(Math.abs(8 - x), (char) (y+'A')));
queue.add(new Bishop(Math.abs(8 - x), (char) (y+'A'), bishop.cnt + 1, temp));
}
}
}
}
return null;
}
}
๋ณ์)
T : ํ ์คํธ ์ผ์ด์ค ๊ฐ์
x1, y1, x2, y2 : ์์ ์์น, ๋์ฐฉ ์์น
temp : ์ด๋ ๊ฒฝ๋ก
Bishop
๋น์ ์์น x, y
์์ง์ธ ํ์ cnt
์ด๋ ๊ฒฝ๋ก ArrayList <Bishop> list
Main
ํ ์คํธ ์ผ์ด์ค ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ํ ์คํธ ์ผ์ด์ค ์๋งํผ ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1) ์์ ์์น, ๋์ฐฉ ์์น ์ ๋ ฅ
2) ์์ ์์น์ ๋์ฐฉ ์์น๊ฐ ๊ฐ๋ค๋ฉด 0๊ณผ ์์น๋ฅผ ์ถ๋ ฅ
3) ์์ ์์น์ ๋์ฐฉ ์์น๊ฐ ๊ฐ์ง ์๋ค๋ฉด bfsํจ์ ํธ์ถ
4) ๋ฐํ ๊ฐ์ด null์ด๋ผ๋ฉด Impossible ์ถ๋ ฅ, null์ด ์๋๋ผ๋ฉด (ArrayList์ ํฌ๊ธฐ+1) ์ถ๋ ฅ ๋ฐ ๊ฒฝ๋ก ์ถ๋ ฅ
bfs(int x1, char y1, int x2, char y2)
x1๊ณผ y1 ์ขํ๋ฅผ ๊ฐ๊ฐ 8-x1, y1-'A'๋ก ๋ฐ๊ฟ์ผ ํ๋ค. ์์ ์์น๋ฅผ ๋ฐฉ๋ฌธ ํ์ ํ queue์ ๊ฐ์ฒด๋ก ์ ์ฅํ๋ค. queue๊ฐ ๋น ๋๊น์ง ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1) queue poll
2) 1๋ฒ ๊ฐ์์ ํ์ฌ ์ขํ๋ฅผ ๊บผ๋ด์ ๊ฐ๊ฐ y-'A', 8-x๋ก ๋ณํ
3) ๋๊ฐ์ ์ผ๋ก ์ด๋ํด์ ์ฒด์คํ ๋ฒ์ ์์ด๋ฉฐ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ณณ์ด๋ฉด ๋ฐฉ๋ฌธ
4) ๋ง์ฝ ๋์ฐฉ ์์น์ ๋์ฐฉํ๋ค๋ฉด ์ด๋ ๊ฒฝ๋ก ๋ฐํ
5) 4๋ฒ ์์ง์ฌ์ ๋์ฐฉ ์์น์ ๋์ฐฉํ์ง ๋ชปํ๋ค๋ฉด null ๋ฐํ
6) ํ์ฌ ์ด๋ ๊ฒฝ๋ก์์ ์๋ก ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐ ๋ฐ queue์ Bishop ๊ฐ์ฒด ์ถ๊ฐ
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 4108_์ง๋ขฐ์ฐพ๊ธฐ (0) | 2024.06.27 |
---|---|
[Baekjoon] 2615_์ค๋ชฉ (0) | 2024.06.26 |
[Baekjoon] 1474_๋ฐ ์ค (1) | 2024.06.24 |
[Baekjoon] 26170_์ฌ๊ณผ ๋นจ๋ฆฌ ๋จน๊ธฐ (0) | 2024.06.21 |
[Baekjoon] 2777_์ซ์ ๋์ด (0) | 2024.06.20 |