๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9204_์ฒด์Šค

๋ฟŒ์•ผ._. 2024. 6. 25. 15:05

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: 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 ๊ฐ์ฒด ์ถ”๊ฐ€