01-09 18:44
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 2146 :: ๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ 2146 :: ๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ

miinsun 2022. 4. 4. 22:30

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

์—ฌ๋Ÿฌ ์„ฌ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ๊ฐ€ ์žˆ๋‹ค. ์ด ๋‚˜๋ผ์˜ ๋Œ€ํ†ต๋ น์€ ์„ฌ์„ ์ž‡๋Š” ๋‹ค๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ฒ ๋‹ค๋Š” ๊ณต์•ฝ์œผ๋กœ ์ธ๊ธฐ๋ชฐ์ด๋ฅผ ํ•ด ๋‹น์„ ๋  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋ง‰์ƒ ๋Œ€ํ†ต๋ น์— ์ทจ์ž„ํ•˜์ž, ๋‹ค๋ฆฌ๋ฅผ ๋†“๋Š”๋‹ค๋Š” ๊ฒƒ์ด ์•„๊น๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ๊ทธ๋Š”, ์ƒ์ƒ‰๋‚ด๋Š” ์‹์œผ๋กœ ํ•œ ์„ฌ๊ณผ ๋‹ค๋ฅธ ์„ฌ์„ ์ž‡๋Š” ๋‹ค๋ฆฌ ํ•˜๋‚˜๋งŒ์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ•˜์˜€๊ณ ,
๊ทธ ๋˜ํ•œ ๋‹ค๋ฆฌ๋ฅผ ๊ฐ€์žฅ ์งง๊ฒŒ ํ•˜์—ฌ ๋ˆ์„ ์•„๋ผ๋ ค ํ•˜์˜€๋‹ค.

์ด ๋‚˜๋ผ๋Š” N×Nํฌ๊ธฐ์˜ ์ด์ฐจ์› ํ‰๋ฉด์ƒ์— ์กด์žฌํ•œ๋‹ค. ์ด ๋‚˜๋ผ๋Š” ์—ฌ๋Ÿฌ ์„ฌ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์„ฌ์ด๋ž€ ๋™์„œ๋‚จ๋ถ์œผ๋กœ ์œก์ง€๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๋ฉ์–ด๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค. ๋‹ค์Œ์€ ์„ธ ๊ฐœ์˜ ์„ฌ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ์˜ ์ง€๋„์ด๋‹ค.
์œ„์˜ ๊ทธ๋ฆผ์—์„œ ์ƒ‰์ด ์žˆ๋Š” ๋ถ€๋ถ„์ด ์œก์ง€์ด๊ณ , ์ƒ‰์ด ์—†๋Š” ๋ถ€๋ถ„์ด ๋ฐ”๋‹ค์ด๋‹ค. ์ด ๋ฐ”๋‹ค์— ๊ฐ€์žฅ ์งง์€ ๋‹ค๋ฆฌ๋ฅผ ๋†“์•„ ๋‘ ๋Œ€๋ฅ™์„ ์—ฐ๊ฒฐํ•˜๊ณ ์ž ํ•œ๋‹ค. ๊ฐ€์žฅ ์งง์€ ๋‹ค๋ฆฌ๋ž€, ๋‹ค๋ฆฌ๊ฐ€ ๊ฒฉ์ž์—์„œ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๋‹ค๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค.

๋‹ค์Œ ๊ทธ๋ฆผ์—์„œ ๋‘ ๋Œ€๋ฅ™์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹ค๋ฆฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
๋ฌผ๋ก  ์œ„์˜ ๋ฐฉ๋ฒ• ์™ธ์—๋„ ๋‹ค๋ฆฌ๋ฅผ ๋†“๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์žˆ์œผ๋‚˜,
์œ„์˜ ๊ฒฝ์šฐ๊ฐ€ ๋†“๋Š” ๋‹ค๋ฆฌ์˜ ๊ธธ์ด๊ฐ€ 3์œผ๋กœ ๊ฐ€์žฅ ์งง๋‹ค(๋ฌผ๋ก  ๊ธธ์ด๊ฐ€ 3์ธ ๋‹ค๋ฅธ ๋‹ค๋ฆฌ๋ฅผ ๋†“์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ๋ช‡ ๊ฐ€์ง€ ์žˆ๋‹ค).

์ง€๋„๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€์žฅ ์งง์€ ๋‹ค๋ฆฌ ํ•˜๋‚˜๋ฅผ ๋†“์•„ ๋‘ ๋Œ€๋ฅ™์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ ์ค„์—๋Š” ์ง€๋„์˜ ํฌ๊ธฐ N(100์ดํ•˜์˜ ์ž์—ฐ์ˆ˜)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ๊ทธ ๋‹ค์Œ N์ค„์—๋Š” N๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง€๋ฉฐ, 0์€ ๋ฐ”๋‹ค, 1์€ ์œก์ง€๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • ํ•ญ์ƒ ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ฌ์ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„์— ๊ฐ€์žฅ ์งง์€ ๋‹ค๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

10
1 1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1 1
1 0 1 1 0 0 0 0 1 1
0 0 1 1 1 0 0 0 0 1
0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

3

 

โ€‹

๐Ÿ’ป  Main.java

  • DFS์™€ BFS ๋‘˜์„ ํ™œ์šฉํ•ด ํ’€์ดํ•  ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์ง€๋งŒ, ์‹œ๊ฐ„ ํšจ์œจ์„ ์œ„ํ•ด BFS๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
  • ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ํฌ๊ฒŒ 2๊ฐ€์ง€ ์ ˆ์ฐจ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค.
    1. ๊ฐ์ž ๋…๋ฆฝ๋œ ์„ฌ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ธฐ
      • ๊ฐ ๊ฐ์˜ ์„ฌ์„ 1,2,3...์œผ๋กœ ๋„˜๋ฒ„๋งํ•ด์ฃผ์ž
      • ๊ธฐ์กด์˜ BFS๋ฌธ์ œ๋“ค๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ด๊ฑด ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—‡๋‹ค.
    2. ๋…๋ฆฝ๋œ ์„ฌ๊ณผ ์„ฌ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ
      • ์ด๊ฑธ ๊ตฌํ˜„ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ํ•  ์ˆ˜ ์—†์ด ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋‹ค.
      • Point์— ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๊ฐ ์„ฌ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
      • ๊ผญ for๋ฌธ์„ ์‹œ์ž‘ํ• ๋•Œ๋งˆ๋‹ค. check[]๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์ž. (์ฐจ๋งˆ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•ด์„œ ์ฐพ๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค..)
/* ๋ฐฑ์ค€ ๊ทธ๋ž˜ํ”„ - 2146 :: ๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ */

import java.util.*;
import java.io.*;

class Point { 
	public int x, y, cnt; 
	Point (int x, int y, int cnt){ 
		this.x = x; 
		this.y = y; 
		this.cnt = cnt;
	} 
}
	
public class Main {
	static int[][] board, map;
	static int n;
	// ์ธ์ ‘ํ•œ ์นธ(์ขŒ์šฐ ์œ„ ์•„๋ž˜) ์ขŒํ‘œ ์ •๋ณด
	static int[] dx = {-1, 0, 1, 0}; 
	static int[] dy = {0, -1, 0, 1};

		
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		n = Integer.parseInt(st.nextToken());
		board = new int[n][n];
		map = new int[n][n];
		
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0; j < n; j++) {
				board[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		
		Queue<Point> q = new LinkedList<>();
		int order = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				// ๊ฐ๊ฐ์˜ ์„ฌ ๋ฐฉ
				if(board[i][j] == 1) {
					q.offer(new Point(i, j, 0));
					board[i][j] = 0;
					// ์„ฌ์„ ๋ฐฉ๋ฌธ ํ• ๋•Œ๋งˆ๋‹ค ์„ฌ์˜ ์ˆœ์„œ ์ฆ๊ฐ€
					order++;
					map[i][j] = order;

					while(!q.isEmpty()) { 
						Point tmp = q.poll(); 
						for(int k = 0; k < 4; k++) { 
							int nx = tmp.x + dx[k]; 
							int ny = tmp.y + dy[k]; 
							
							// ๊ฒฝ๊ณ„์„  ๊ฒ€์‚ฌ 
							if(nx >= 0 && nx <= n - 1 && ny >= 0 && ny <= n - 1 && board[nx][ny] == 1) {
								board[nx][ny] = 0; 
								q.offer(new Point(nx, ny, 0));
								map[nx][ny] = order; 
							}
						}
						
					}
				}	
			}
		}
		
	    int answer = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (map[i][j] >= 1) {
                    boolean[][] check = new boolean[n][n];
                    Queue<Point> queue = new LinkedList<Point>();
                    queue.offer(new Point(i, j, 0));
                    check[i][j] = true;
                    
                    while (!queue.isEmpty()) {
                        Point point = queue.poll();
                        for (int k = 0; k < 4; k++) {
                            int nx = point.x + dx[k];
                            int ny = point.y + dy[k];
                            if (nx >= 0 && nx < n && ny >= 0 && ny < n && !check[nx][ny] && map[nx][ny] != map[i][j]) {
                                check[nx][ny] = true;
                                if (map[nx][ny] == 0) { //๋ฐ”๋‹ค
                                    queue.offer(new Point(nx, ny, point.cnt + 1));
                                } else { //๋‹ค๋ฅธ ์„ฌ
                                    answer = Math.min(answer, point.cnt);
                                }
                            }
                        }
                    }
                }
            }
        }
		
		System.out.println(answer);
		
		br.close();
		return ;
	}
}
Comments