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

[Java Algorithm] ๋‘ ๋™์ „ BOJ #16197

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

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

 

16197๋ฒˆ: ๋‘ ๋™์ „

N×M ํฌ๊ธฐ์˜ ๋ณด๋“œ์™€ 4๊ฐœ์˜ ๋ฒ„ํŠผ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒŒ์ž„์ด ์žˆ๋‹ค. ๋ณด๋“œ๋Š” 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์นธ์€ ๋น„์–ด์žˆ๊ฑฐ๋‚˜, ๋ฒฝ์ด๋‹ค. ๋‘ ๊ฐœ์˜ ๋นˆ ์นธ์—๋Š” ๋™์ „์ด ํ•˜๋‚˜์”ฉ ๋†“์—ฌ์ ธ ์žˆ๊ณ ,

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 int n, m;
	static char[][] board;
	static Deque<int[]> que;
	static int[] dx = {0, 0, -1, 1};
	static int[] dy = {-1, 1, 0, 0};
	
	public static int bfs() {
		mainLoop:
		while(true) {
			int[] coin1 = que.removeFirst();
			int[] coin2 = que.removeFirst();
			if(coin1[2] > 10) break;
			
			for(int i = 0; i < 4; i++) {
				int nx1 = coin1[0] + dx[i];
				int ny1 = coin1[1] + dy[i];
				int nx2 = coin2[0] + dx[i];
				int ny2 = coin2[1] + dy[i];
				
				// ๋™์ „์ด ๋‘˜ ๋‹ค ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ
				if((nx1 < 0 || ny1 < 0 || nx1 >= n || ny1 >= m) && (nx2 < 0 || ny2 < 0 || nx2 >= n || ny2 >= m)) continue;
				
				// ๋™์ „์ด ํ•˜๋‚˜๋งŒ ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ
				if(((nx1 < 0 || ny1 < 0 || nx1 >= n || ny1 >= m) && (nx2 >= 0 && ny2 >= 0 && nx2 < n && ny2 < m)) ||
						((nx1 >= 0 && ny1 >= 0 && nx1 < n && ny1 < m) && (nx2 < 0 || ny2 < 0 || nx2 >= n || ny2 >= m))) return coin1[2];
				
				if(board[nx1][ny1] == '#') que.add(new int[]{coin1[0], coin1[1], coin1[2]+1});
				else que.add(new int[]{nx1, ny1, coin1[2]+1});
				
				if(board[nx2][ny2] == '#') que.add(new int[]{coin2[0], coin2[1], coin2[2]+1});
				else que.add(new int[]{nx2, ny2, coin2[2]+1});
			}
		}
		return -1;
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		board = new char[n][m];
		que = new ArrayDeque<>();
		
		for(int i = 0; i < n; i++) {
			String now = in.readLine();
			for(int j = 0; j < m; j++) {
				board[i][j] = now.charAt(j);
				if(now.charAt(j) == 'o') que.add(new int[]{i, j, 1}); // x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฒ„ํŠผ ํด๋ฆญ ํšŸ์ˆ˜ ์นด์šดํŠธ
			}
		}
		
		System.out.println(bfs());
	}

}

๐Ÿ‘‰ BFS ๋ฌธ์ œ

์ด ๋ฌธ์ œ๋Š” ๋‘ ๊ฐœ์˜ ๋™์ „์ด ํ•œ ๋ฒˆ์— ์›€์ง์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์ ,

๊ทธ๋ฆฌ๊ณ  ๋”ฑ ํ•˜๋‚˜์˜ ๋™์ „๋งŒ ๋–จ์–ด์ ธ์•ผ ํ•œ๋‹ค๋Š” ์ ๋งŒ ์ž˜ ๊ณ ๋ คํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ!

  • ๋™์ „์ด ๋‘˜ ๋‹ค ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํƒ์ƒ‰์„ ํ•˜์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค.
  • ๋™์ „์ด ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํƒ์ƒ‰์˜ ์นด์šดํŒ…์„ ๋ฆฌํ„ดํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ข…๋ฃŒํ–ˆ๋‹ค.
    => ๊ฐ€์žฅ ์ฒ˜์Œ ๋ฆฌํ„ด๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐ”๋กœ ์ตœ์†Œ๋กœ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ!
  • ๋ฒฝ์„ ๋งŒ๋‚  ๊ฒฝ์šฐ์—๋Š”, ๋ฒฝ์„ ๋งŒ๋‚œ ๋™์ „๋งŒ ์›€์ง์ด์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์—ˆ๋‹ค.
    => ๋ฒฝ์„ ๋งŒ๋‚˜๋„ ์นด์šดํŒ…์€ ๊ทธ๋Œ€๋กœ ์ฆ๊ฐ€!!

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•œ ์˜ˆ์™ธ๋ฅผ ์ž˜ ๊ณ ๋ คํ•ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ!

๋Œ“๊ธ€