๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 6601_Knight Moves

๋ฟŒ์•ผ._. 2024. 11. 19. 12:31
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/6601)

< Knight Moves >

 

๋ฌธ์ œ ํ’€์ด 

 

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.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class _6601_ { // Knight Moves
	static int dx[] = { -1, -2, -2, -1, 1, 2, 2, 1 };
	static int dy[] = { -2, -1, 1, 2, 2, 1, -1, -2 };
    
	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;
        
		String str = "";
		while ((str = bf.readLine()) != null) {
			st = new StringTokenizer(str);
            
			String start = st.nextToken();
			String end = st.nextToken();
            
			int x1 = 8 - (start.charAt(1) - '0');
			int y1 = start.charAt(0) - 'a';
            
			int x2 = 8 - (end.charAt(1) - '0');
			int y2 = end.charAt(0) - 'a';
            
			if (x1 == x2 && y1 == y2) {
				bw.write("To get from " + start + " to " + end + " takes 0 knight moves.\n");
			} else {
				bw.write("To get from " + start + " to " + end + " takes " + bfs(x1, y1, x2, y2) + " knight moves.\n");
			}
		}
		bw.flush();
	}
    
	private static int bfs(int x1, int y1, int x2, int y2) {
		boolean visited[][] = new boolean[8][8];
		visited[x1][y1] = true;
        
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { x1, y1, 0 });
        
		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
			for (int i = 0; i < 8; i++) {
				int x = temp[0] + dx[i];
				int y = temp[1] + dy[i];
				if (x >= 0 && x < 8 && y >= 0 && y < 8 && !visited[x][y]) {
					if (x == x2 && y == y2) {
						return temp[2] + 1;
					}
					visited[x][y] = true;
					queue.add(new int[] { x, y, temp[2] + 1 });
				}
			}
		}
		return -1;
	}
}
๋ณ€์ˆ˜)
x1, y1, x2, y2 : ์ถœ๋ฐœ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜๋ฅผ ์ขŒ์ƒ๋‹จ์— ๋งž์ถฐ ๋ณ€๊ฒฝ
dx, dy : ์ด๋™ ๋ฐฉํ–ฅ
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

 

์ถœ๋ฐœ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ˜„์žฌ ์ž…๋ ฅ๋œ ์œ„์น˜๋Š” ์ขŒํ•˜๋‹จ ๊ธฐ์ค€์ด๋ฏ€๋กœ ์ขŒ์ƒ๋‹จ์— ๋งž์ถฐ ๋ณ€๊ฒฝํ•˜์—ฌ x1, y1, x2, y2์— ์ €์žฅํ•œ๋‹ค. ์ถœ๋ฐœ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์›€์ง์ด๋Š” ํšŸ์ˆ˜๋ฅผ 0์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ  ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด bfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

bfs(int x1, int y1, int x2, int y2)

Queue์— ์‹œ์ž‘ ์œ„์น˜์™€ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋ฉฐ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•œ๋‹ค. Queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) Queue poll

2) 8๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ ํ›„ ๋ฐฐ์—ด ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œ ๋ฐ Queue์— ์ถ”๊ฐ€. ๋งŒ์•ฝ ์ด๋™ ์œ„์น˜๊ฐ€ ๋„์ฐฉ ์œ„์น˜๋ผ๋ฉด temp[2]+1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 14145_ลฝetva  (0) 2024.11.21
[Baekjoon] 6004_The Chivalrous Cow  (1) 2024.11.20
[Baekjoon] 6798_Knight Hop  (0) 2024.11.18
[Baekjoon] 15242_Knight  (1) 2024.11.15
[Baekjoon] 16390_Shebaโ€™s Amoebas  (0) 2024.11.14