View

๋ฐ˜์‘ํ˜•

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

 

14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ

์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ

www.acmicpc.net

 

 

package ๋ฐฑ์ค€;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main_14502 {
	static class Point {
		int y, x;

		public Point(int y, int x) {
			super();
			this.y = y;
			this.x = x;
		}

	}

	static int originMap[][];
	static int copyMap[][];
	static int N, M, resultMax = Integer.MIN_VALUE;
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };
	static ArrayList<Point> virus = new ArrayList<>();

	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());
		M = Integer.parseInt(st.nextToken());
		originMap = new int[N][M];

		// ์ž…๋ ฅ๊ฐ’ ๋„ฃ๊ธฐ
		for (int y = 0; y < N; y++) {
			st = new StringTokenizer(br.readLine());
			for (int x = 0; x < M; x++) {
				int tmp = Integer.parseInt(st.nextToken());
				originMap[y][x] = tmp;
				if (tmp == 2) { // ๋ฐ”์ด๋Ÿฌ์Šค ์ขŒํ‘œ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„๋‘ 
					virus.add(new Point(y, x));
				}
			}
		}

		// ๋ฒฝ์„ 3๊ฐœ ์„ธ์šฐ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜(์กฐํ•ฉ)
		combination(0, 0, 0);
		System.out.println(resultMax);

	}

	private static void combination(int y, int x, int cnt) {
		// ๊ธฐ๋Šฅ: ๋ฒฝ 3๊ฐœ๋ฅผ ๋ฝ‘์Œ
		if (cnt == 3) {
			copy();// ๋งต ๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉ ๊ธฐ์กด์˜ ๋งต์€ ๋ณ€ํ™”X
			for (int i = 0; i < virus.size(); i++) {

				spread(virus.get(i).y, virus.get(i).x);// ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆผ
			}
			servive(); // ์‚ด์•„ ๋‚จ์€ ์ง€์—ญ cnt
			return;
		}
		for (int row = 0; row < N; row++) {
			for (int col = 0; col < M; col++) {
				if (originMap[row][col] == 0) { // ๋ฒฝ์ด ์•„๋‹Œ ์ง€์—ญ ๋ฐœ๊ฒฌ ์‹œ
					originMap[row][col] = 1; // ๋ฒฝ์„ ์„ธ์›Œ๋ด„
					combination(row, col, cnt + 1);
					originMap[row][col] = 0; // ๋‹ค์‹œ ์›๋ž˜๋Œ€๋กœ ๋ณต๊ท€
				}
			}
		}

	} // combi-end

	private static void copy() {
		// ๊ธฐ๋Šฅ: ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผ๋œจ๋ฆฌ๊ธฐ ์œ„ํ•ด ๋งต์„ ์ž ์‹œ ์นดํ”ผํ•ด๋†“์Œ
		copyMap = new int[N][M];
		for (int y = 0; y < N; y++) {
			for (int x = 0; x < M; x++) {
				copyMap[y][x] = originMap[y][x];
			}
		}

	}

	private static void servive() {
		// ๊ธฐ๋Šฅ: ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ํผํŠธ๋ฆฐ ๋’ค ์‚ด์•„ ๋‚จ์€ ์—ฐ๊ตฌ์†Œ์˜ ๊ฐœ์ˆ˜ ์นด์šดํŠธ
		// ์นด์šดํŠธ ์ดํ›„ ์ตœ๋Œ“๊ฐ’ ๊ฐฑ์‹ 
		int cnt = 0;
		for (int y = 0; y < N; y++) {
			for (int x = 0; x < M; x++) {
				if (copyMap[y][x] == 0) {
					cnt++;
				}
			}
		} // ์—ฐ๊ตฌ์†Œ ๊ฐœ์ˆ˜ ์นด์šดํŠธ ๋

		if (resultMax < cnt) {
			resultMax = cnt;
		}

	}

	private static void spread(int y, int x) {
		// ๊ธฐ๋Šฅ : 2(๋ฐ”์ด๋Ÿฌ์Šค)๋ฅผ ์ƒํ•˜์ขŒ์šฐ๋กœ ํผ๋œจ๋ฆผ
		Queue<Point> que = new ArrayDeque<>();
		que.add(new Point(y, x));

		while (!que.isEmpty()) {
			Point node = que.poll();

			for (int i = 0; i < 4; i++) {
				int nx = node.x + dx[i];
				int ny = node.y + dy[i];

				if (nx >= 0 && ny >= 0 && nx < M && ny < N) {
					if (copyMap[ny][nx] == 0) {
						copyMap[ny][nx] = 2;
						que.add(new Point(ny, nx));
					}
				}

			}
		}

	}
}

 

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30