๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[Java] ๋ฐฑ์ค€ 16946 - ๋ฒฝ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ 4

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

 

ํ’€์ด

 

1. ๋ฒฝ์œผ๋กœ ๋‚˜๋ˆ ์ง„ ๊ตฌ์—ญ๋“ค์— ๋ฒˆํ˜ธ๋ฅผ ๋งค๊น€ (๋™์‹œ์— ๊ฐ ๊ตฌ์—ญ์˜ ์นธ ์ˆ˜๋„ ์ €์žฅํ•ด๋‘ )
2. ํŠน์ • ์นธ์˜ ๋ฒฝ์„ ์—†์•ค๋‹ค๋ฉด ์–ด๋–ค ๊ตฌ์—ญ๋“ค์ด ์—ฐ๊ฒฐ๋˜๋Š”์ง€ ๋ณด๊ณ  1๋ฒˆ์—์„œ ๊ตฌํ•ด๋‘” ์นธ ์ˆ˜ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•ด์„œ ์—ฐ๊ฒฐ๋˜๋Š” ์นธ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

 

์˜ˆ๋ฅผ๋“ค์–ด, X๊ฐ€ ๋ฒฝ์ด๋ผ๊ณ  ์น˜๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋ˆ ์ง„ ๊ตฌ์—ญ์— ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธฐ๊ณ 

XX00X		XX11X
00XXX	=>	22XXX
0X0X0		2X3X4
X0X0X		X5X6X

 

๋ฐฐ์—ด์— ๊ฐ ๊ตฌ์—ญ์˜ ์นธ ์ˆ˜๋ฅผ ์ €์žฅํ•ด๋‘๊ธฐ.

[2, 3, 1, 1, 1, 1]

 

 

1๋ฒˆ ๊ณผ์ •์€ BFS (flood-fill)์„ ์‚ฌ์šฉํ•˜๊ณ , 2๋ฒˆ ๊ณผ์ •์€ ๊ทธ๋ƒฅ ๋ฒฝ์ด ์žˆ๋Š” ์นธ์˜ ์ƒํ•˜์ขŒ์šฐ๋งŒ ํ™•์ธํ•ด์ฃผ๋ฉด ๋จ.

 

์ฝ”๋“œ
๋”๋ณด๊ธฐ
import java.io.*;
import java.util.*;

public class Main {
	public static class Pos {
		public int row, col;

		public Pos(int r, int c) {
			row = r;
			col = c;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int R = Integer.parseInt(st.nextToken());
		int C = Integer.parseInt(st.nextToken());
		boolean[][] wall = new boolean[R][C];

		for (int r = 0; r < R; r++) {
			for (int c = 0; c < C; c++) {
				wall[r][c] = (br.read() == '1');
			}
			br.readLine();
		}

		int[][] map = new int[R][C];
		int areaNum = 0;
		int[] areaSize = new int[R * C + 1];
		int[] dirR = { -1, 1, 0, 0 };
		int[] dirC = { 0, 0, -1, 1 };

		for (int r = 0; r < R; r++) {
			for (int c = 0; c < C; c++) {
				if (map[r][c] == 0 && !wall[r][c]) {
					areaNum++;
					areaSize[areaNum] = 1;
					Queue<Pos> que = new LinkedList<>();
					que.add(new Pos(r, c));
					map[r][c] = areaNum;

					while (!que.isEmpty()) {
						Pos cur = que.remove();

						for (int dir = 0; dir < 4; dir++) {
							int row = cur.row + dirR[dir];
							int col = cur.col + dirC[dir];

							if (row < 0 || row >= R || col < 0 || col >= C) {
								continue;
							}

							if (wall[row][col] || map[row][col] > 0) {
								continue;
							}

							map[row][col] = areaNum;
							areaSize[areaNum]++;
							que.add(new Pos(row, col));
						}
					}
				}
			}
		}

		boolean[] checked = new boolean[areaNum + 1];
		int[][] answer = new int[R][C];

		for (int r = 0; r < R; r++) {
			for (int c = 0; c < C; c++) {
				if (wall[r][c]) {
					answer[r][c] = 1;

					for (int dir = 0; dir < 4; dir++) {
						int row = r + dirR[dir];
						int col = c + dirC[dir];

						if (row < 0 || row >= R || col < 0 || col >= C) {
							continue;
						}

						if (checked[map[row][col]]) {
							continue;
						}

						answer[r][c] += areaSize[map[row][col]];
						checked[map[row][col]] = true;
					}

					for (int dir = 0; dir < 4; dir++) {
						int row = r + dirR[dir];
						int col = c + dirC[dir];

						if (row < 0 || row >= R || col < 0 || col >= C) {
							continue;
						}

						checked[map[row][col]] = false;
					}
				}
			}
		}

		StringBuilder sb = new StringBuilder();

		for (int r = 0; r < R; r++) {
			for (int c = 0; c < C; c++) {
				sb.append(answer[r][c] % 10);
			}
			sb.append('\n');
		}

		System.out.println(sb);
		br.close();
	}
}