๋ฌธ์
ํ์ด
๋จผ์ ๊ฐ ์ฌ์ ๊ตฌ๋ถํ๊ธฐ ์ํด 1์ด ์๋ ์(2,3,4...)๋ก ์ด๊ธฐํ ์์ผ์ฃผ์๋ค. ๊ทธ๋ฐ ๋ค์ ๊ฐ ์ฌ๋ง๋ค ํ์์ ์งํํจ์ ์์ด ์์ ์งํฉ์ ์ฌ์ ๊ฒฝ๊ณ (์ฃผ์์ 0์ด ์กด์ฌํ๋ ์นธ)๋ฅผ ์ก๊ณ ๋ค๋ฅธ ์ฌ์ ๋ง๋๊ธฐ ์ ๊น์ง bfs๋ฅผ ์ํํ๋ฉด์ ์ต์๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฐฉ์์ผ๋ก ์งํํ๋ค.
์ฌ๊ธฐ์ distance๊ฐ ์ฆ๊ฐํ๋ ์์ ์ ๋ํด์ ๊ณ ๋ฏผ์ ๋ง์ด ํ๋๋ฐ ์๋ ์ฝ๋์ฒ๋ผ BFS์ Level์ ํ๋จํ๊ธฐ ์ํด ์ด๊ธฐ bounday์ ์ฌ์ด์ฆ๋ฅผ ๊ตฌํ๊ณ ์ด์ ๋ํด์ ๋ฐ๋ณต๋ฌธ์ ๋๋ ๋ฐฉ์์ผ๋ก Level์ ํ๋จํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๊ธฐ ํ์ด๋ 50%๊ฐ ๋์ด๊ฐ ๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋๋ฐ, BFS๋ฅผ ์ํํ ๋ visited๋ฅผ ์ฒดํฌํ์ง ์์์ ๋ง์ ์ค๋ณต์ด ๋ฐ์ํ๋ ๊ฒ์ด ๋ฌธ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ visited๋ฅผ ๊ฐฑ์ ํ๋ ์์น๊ฐ current์ ์์ ์์ ํ ์๋ ์๊ณ next์ ์์ ์์ ํ ์ ์๋๋ฐ ํ์ธ ๊ฒฐ๊ณผ next์ ์์ ์์ ์ฒดํฌํ๋ ๊ฒ์ด ํจ์จ์ ์ด์ด์ ์ฝ๋๋ฅผ ์์ ํ๋ค.
์ต์ข ์ ๋ต ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
int first() {
return x;
}
int second() {
return y;
}
}
private static int number = 2;
private static int[] dx = { 0, 0, -1, 1 };
private static int[] dy = { 1, -1, 0, 0 };
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] map = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
map[i][j] = sc.nextInt();
}
}
// divide island
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[i][j] == 1) {
fillNumber(map, i, j);
number++;
}
}
}
// get Minimum Distance
int num = 2;
int min = Integer.MAX_VALUE;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[i][j] == num) {
min = Integer.min(min, getMinimumDistance(map, i, j));
num++;
}
}
}
System.out.println(min);
}
private static void fillNumber(int[][] map, int x, int y) {
boolean[][] visited = new boolean[map.length][map.length];
Queue<Pair> sq = new LinkedList<Pair>();
sq.add(new Pair(x, y));
while (!sq.isEmpty()) {
Pair current = sq.poll();
map[current.first()][current.second()] = number;
for (int i = 0; i < 4; i++) {
int nextX = current.first() + dx[i];
int nextY = current.second() + dy[i];
if (nextX < 0 || nextX > map.length - 1 || nextY < 0 || nextY > map.length - 1
|| map[nextX][nextY] != 1 || visited[nextX][nextY])
continue;
sq.add(new Pair(nextX, nextY));
visited[nextX][nextY] = true;
}
}
}
private static int getMinimumDistance(int[][] map, int x, int y) {
LinkedList<Pair> boundary = getBoundary(map, map[x][y]);
boolean[][] visited = new boolean[map.length][map.length];
int distance = 1;
while (!boundary.isEmpty()) {
int size = boundary.size();
for (int i = 0; i < size; i++) {
Pair current = boundary.poll();
for (int idx = 0; idx < 4; idx++) {
int nextX = current.first() + dx[idx];
int nextY = current.second() + dy[idx];
if (nextX < 0 || nextX > map.length - 1 || nextY < 0 || nextY > map.length - 1
|| map[nextX][nextY] == map[x][y] || visited[nextX][nextY])
continue;
if (map[nextX][nextY] == 0) {
visited[nextX][nextY] = true;
boundary.add(new Pair(nextX, nextY));
} else {
return distance;
}
}
}
distance++;
}
return distance;
}
private static LinkedList<Pair> getBoundary(int[][] map, int num) {
LinkedList<Pair> ret = new LinkedList<>();
boolean[][] visited = new boolean[map.length][map.length];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map.length; j++) {
if (map[i][j] != num)
continue;
for (int idx = 0; idx < 4; idx++) {
int nextX = i + dx[idx];
int nextY = j + dy[idx];
if (nextX < 0 || nextX > map.length - 1 || nextY < 0 || nextY > map.length - 1 || visited[nextX][nextY])
continue;
if (map[nextX][nextY] == 0) {
visited[nextX][nextY] = true;
ret.add(new Pair(nextX, nextY));
}
}
}
}
return ret;
}
}
'๐ Algorithm > ๐ป ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 11053. ๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด (0) | 2021.02.10 |
---|---|
[๋ฐฑ์ค] 9663. N-Queens (0) | 2021.02.05 |
[๋ฐฑ์ค] 2493. ํ (0) | 2021.02.04 |
[๋ฐฑ์ค] 1436. ์ํ๊ฐ๋ ์ (0) | 2021.02.03 |
[๋ฐฑ์ค] 1018. ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ (0) | 2021.02.02 |
๋๊ธ