๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋ถˆ BOJ #5427

by seolhee2750 2022. 8. 8.
๋ฌธ์ œ

https://www.acmicpc.net/problem/5427

 

5427๋ฒˆ: ๋ถˆ

์ƒ๊ทผ์ด๋Š” ๋นˆ ๊ณต๊ฐ„๊ณผ ๋ฒฝ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฑด๋ฌผ์— ๊ฐ‡ํ˜€์žˆ๋‹ค. ๊ฑด๋ฌผ์˜ ์ผ๋ถ€์—๋Š” ๋ถˆ์ด ๋‚ฌ๊ณ , ์ƒ๊ทผ์ด๋Š” ์ถœ๊ตฌ๋ฅผ ํ–ฅํ•ด ๋›ฐ๊ณ  ์žˆ๋‹ค. ๋งค ์ดˆ๋งˆ๋‹ค, ๋ถˆ์€ ๋™์„œ๋‚จ๋ถ ๋ฐฉํ–ฅ์œผ๋กœ ์ธ์ ‘ํ•œ ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ„๋‹ค. ๋ฒฝ์—

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.StringTokenizer;

public class Main {
	
	static Deque<int[]> que; // {์ƒ๊ทผ or ๋ถˆ ์—ฌ๋ถ€, sec, x์ขŒํ‘œ, y์ขŒํ‘œ} (์ƒ๊ทผ: 0, ๋ถˆ: 1)
	static int[] dx = {0, 0, -1, 1};
	static int[] dy = {-1, 1, 0, 0};
	static int h, w;
	static char[][] map;
	
	public static int bfs() {
		while(!que.isEmpty()) {
			int[] now = que.removeFirst();
			int type = now[0];
			int sec = now[1];
			int x = now[2];
			int y = now[3];
			
			findLoop:
			for(int i = 0; i < 4; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				
				if(type == 0) { // ์ƒ๊ทผ์ด
					if(nx < 0 || ny < 0 || nx >= h || ny >= w) return sec+1; // ํƒˆ์ถœ ์„ฑ๊ณต
					if(map[nx][ny] != '.') continue findLoop; // ๋นˆ ๊ณณ ์•„๋‹ ๋•Œ
					que.add(new int[]{type, sec+1, nx, ny});
					map[nx][ny] = '@';
				}
				else { // ๋ถˆ
					if(nx < 0 || ny < 0 || nx >= h || ny >= w || map[nx][ny] != '.') continue findLoop;
					que.add(new int[]{type, sec+1, nx, ny});
					map[nx][ny] = '*';
				}
			}
		}
		return -1;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuffer out = new StringBuffer();
		
		int tc = Integer.parseInt(in.readLine());
		
		for(int t = 0; t < tc; t++) {
			st = new StringTokenizer(in.readLine());
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			
			int[] startPoint = new int[3]; // ์ƒ๊ทผ์ด ์ •๋ณด ์ž ์‹œ ์ €์žฅํ•  ์šฉ๋„
			map = new char[h][w];
			que = new ArrayDeque<>();
			
			for(int i = 0; i < h; i++) {
				String tmp = in.readLine();
				for(int j = 0; j < w; j++) {
					map[i][j] = tmp.charAt(j);
					if(tmp.charAt(j) == '*') que.add(new int[]{1, 0, i, j});
					else if(tmp.charAt(j) == '@') startPoint = new int[]{0, 0, i, j};
				}
			}
			que.add(startPoint); // ์ƒ๊ทผ์ด๋ฅผ ๋งจ ๋’ค์— ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•จ
			
			int result = bfs();
			if(result == -1) System.out.println("IMPOSSIBLE");
			else System.out.println(result);
		}
	}

}

๐Ÿ‘‰ BFS ๋ฌธ์ œ

 

[ ๋ฌธ์ œ ํ’€๊ธฐ ์ „ ์ฒดํฌ ! ]

โœ”๏ธ ์ƒ๊ทผ์ด๋Š” ๋‹จ ํ•œ ๋ช…์ด๊ณ , ๋ถˆ์ฒ˜๋Ÿผ ํผ์ง€๋Š” ์กด์žฌ๊ฐ€ ์•„๋‹ˆ์ง€๋งŒ, ๋ถˆ์ฒ˜๋Ÿผ ํผ๋œจ๋ ค์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ํƒํ–ˆ๋‹ค.

๊ทธ ์ด์œ ๋Š”! ์ƒ๊ทผ์ด๊ฐ€ ์ง€๋‚˜๊ฐ”๋˜ ์ž๋ฆฌ๋Š” ์ถ”ํ›„์— ์ถ”ํ›„์— ์˜ค๋”๋ผ๋„ ์•„๋ฌด๋Ÿฐ ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฉฐ,

์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€๋Š” ์ž๋ฆฌ๋งˆ๋‹ค @๋ฅผ ํ‘œ์‹œํ•ด์ฃผ์–ด ์ž์ทจ๋ฅผ ๋‚จ๊ธฐ๋ฉด ์ด๋Š” ๋ฐฉ๋ฌธ ์ฒดํฌ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—,

๋ถˆ์˜ ์˜๋ฏธ์—†๋Š” ํผ์ง์„ ๋ฐฉ์ง€ํ•˜์—ฌ ํšจ์œจ์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

=> visited ๋ฐฐ์—ด ํ•„์š” x

 

โœ”๏ธ ๋ถˆ์ด ์•ž์œผ๋กœ ํผ์ ธ ๋‚˜๊ฐˆ ์˜ˆ์ •์ธ ์ž๋ฆฌ์—๋„ ์ƒ๊ทผ์ด๋Š” ๊ฐˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ,

๋ถˆ์„ ๋จผ์ € ํผ๋œจ๋ฆฐ ํ›„ ์ƒ๊ทผ์ด๋ฅผ ์›€์ง์ด๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

[ ๋ฌธ์ œ ํ’€์ด ]

  • bfs ํƒ์ƒ‰์„ ์œ„ํ•˜์—ฌ que๋ผ๋Š” ๋ฑ์„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๊ณ , intํ˜• ๋ฐฐ์—ด์„ ์š”์†Œ๋กœ ๊ฐ€์ง€๋„๋ก ํ–ˆ๋‹ค.
    ์ €์žฅ๋˜๋Š” intํ˜• ๋ฐฐ์—ด์€ {type, sec, x, y} ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋Š”๋ฐ, ์•„๋ž˜์™€๊ฐ™์ด ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    - type : ์ƒ๊ทผ์ด๋Š” 0, ๋ถˆ์€ 1
    - sec : ํ•ด๋‹น ์ž๋ฆฌ๊นŒ์ง€ ํผ์ง€๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„
    - x, y : ์ขŒํ‘œ
  • ์ฒ˜์Œ ์ง€๋„์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฐ›์„ ๋•Œ, ๋ฐ”๋กœ ๋ถˆ๊ณผ ์ƒ๊ทผ์ด์˜ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•˜๊ณ  que์— ๋„ฃ์–ด์ฃผ์—ˆ๋Š”๋ฐ,
    ์ƒ๊ทผ์ด์˜ ๊ฒฝ์šฐ ๋ถˆ๋ณด๋‹ค ๋‚˜์ค‘์— ์›€์ง์ด๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด
    ์šฐ์„  startPoint ๋ฐฐ์—ด์— ์ž„์‹œ ์ €์žฅ, ๋ชจ๋“  ์ž…๋ ฅ์ด ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ ๋งˆ์ง€๋ง‰์œผ๋กœ  que์— addํ–ˆ๋‹ค.
  • bfs ํƒ์ƒ‰ ์‹œ ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” que ์š”์†Œ๊ฐ€ ์ƒ๊ทผ์ด์ผ ๊ฒฝ์šฐ์™€ ๋ถˆ์ผ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆ„์–ด์ฃผ์—ˆ๋‹ค.
  • ์ƒ๊ทผ์ด์˜ ๊ฒฝ์šฐ map ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ํƒˆ์ถœ์— ์„ฑ๊ณตํ•œ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผ, sec+1์„ returnํ–ˆ๋‹ค.
    ๋นˆ ๊ณณ์„ ๋งŒ๋‚˜์ง€ ๋ชปํ–ˆ์„ ๋•Œ๋Š” ์˜ˆ์™ธ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๊ณ ,
    ๋นˆ ๊ณณ์„ ๋งŒ๋‚ฌ์„ ๋•Œ๋Š” ์ƒˆ๋กœ์šด ํƒ์ƒ‰ ์œ„์น˜๋ฅผ que์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒ๊ทผ์ด์˜ ์›€์ง์ž„์„ map์— ํ‘œ์‹œํ–ˆ๋‹ค.
  • ๋ถˆ์˜ ๊ฒฝ์šฐ map ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ๋นˆ ๊ณณ์„ ๋งŒ๋‚˜์ง€ ๋ชปํ•˜์˜€์„ ๋•Œ ์˜ˆ์™ธ๋กœ ์ฒ˜๋ฆฌํ–ˆ๊ณ ,
    ๋นˆ ๊ณณ์„ ๋งŒ๋‚ฌ์„ ๋•Œ๋Š” que์— ์ƒˆ๋กœ์šด ํƒ์ƒ‰ ์œ„์น˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ถˆ์˜ ์›€์ง์ž„์„ map์— ํ‘œ์‹œํ–ˆ๋‹ค.
  • while๋ฌธ์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ returnํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ƒ๊ทผ์ด๋Š” ํƒˆ์ถœ์— ์‹คํŒจํ–ˆ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ
    -1์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ–ˆ๊ณ , ์ด ๋•Œ๋Š” IMPOSSIBLE์„ ์ถœ๋ ฅํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • BFS์˜ ๋™์ž‘์„ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ™๋‹ค.

๋Œ“๊ธ€