๋ฐฑ์ค€ 14620 ๐ŸŒธ๊ฝƒ๊ธธ : ์™„์ „ํƒ์ƒ‰

๊ธ๊ธยท2025๋…„ 8์›” 9์ผ

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
11/31
post-thumbnail

๋ฌธ์ œ

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

์˜ˆ์ œ ์ž…๋ ฅ
6
1 0 2 3 3 4
1 1 1 1 1 1
0 0 1 1 1 1
3 9 9 0 1 99
9 11 3 1 0 3
12 3 0 0 0 1
์˜ˆ์ œ ์ถœ๋ ฅ
12

ํ’€์ด

1. ์ž…๋ ฅ ์ฒ˜๋ฆฌ

N๊ณผ arr 2์ฐจ์› ๋ฐฐ์—ด์— ํ™”๋‹จ ๊ฐ€๊ฒฉ ์ž…๋ ฅ ๋ฐ›๊ธฐ

		int N = sc.nextInt();

		int[][] arr = new int[N][N];

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				arr[i][j] = sc.nextInt();
			}
		}

2. ๋ฐฉ๋ฌธ ๋ฐฐ์—ด ์ƒ์„ฑ

์ด๋ฏธ ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ์—๋Š” ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•œ 2์ฐจ์› ๋ฐฐ์—ด visited๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

boolean ๋ฐฐ์—ด๋กœ ์ƒ์„ฑํ•˜์—ฌ ๊ฝƒ์„ ์‹ฌ์—ˆ์œผ๋ฉด true, ์‹ฌ์ง€ ์•Š์•˜์œผ๋ฉด false๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

		boolean[][] visited = new boolean[N][N];

3. ์ตœ์†Œ ๋น„์šฉ ๋ณ€์ˆ˜ ์„ค์ •

๋งค์šฐ ํฐ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”


		int minPrice = 100000;

4. โญ๏ธํ•ต์‹ฌ๋กœ์ง

์„ธ ๊ฐœ์˜ ๊ฝƒ์„ ์‹ฌ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ํƒ์ƒ‰ โ†’ ์‚ผ์ค‘ for๋ฌธ (์‹ค์ œ๋กœ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์ด๋ฏ€๋กœ 6์ค‘ for๋ฌธ ๋А๋‚Œ)

๊ฝƒ์„ ์‹ฌ์„ ๋•Œ:

canPlant ๋ฉ”์„œ๋“œ๋กœ ํ•ด๋‹น ์œ„์น˜์— ์‹ฌ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ

์‹ฌ์„ ์ˆ˜ ์žˆ์œผ๋ฉด visited๋ฅผ true๋กœ ๋ณ€๊ฒฝ

๋‹ค์Œ ๊ฝƒ ํƒ์ƒ‰ ํ›„, ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด false๋กœ ๋ณต๊ตฌ (๋ฐฑํŠธ๋ž˜ํ‚น)

		// ์„ธ ๊ฐœ์˜ ์”จ์•—์„ ์‹ฌ๊ธฐ ์œ„ํ•œ 2์ฐจ์› ๋ฐฐ์—ด ์‚ผ์ค‘ for
		for (int i = 1; i < N - 1; i++) {
			for (int j = 1; j < N - 1; j++) {
				// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์—†๋‹ค๋ฉด continue
				if (!canPlant(i, j, visited, N))
					continue;
				// ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ๋Š” visited ๋ฐฐ์—ด์— true ํ•ด์ฃผ๊ธฐ
				visited[i][j] = true;
				visited[i + 1][j] = true;
				visited[i - 1][j] = true;
				visited[i][j + 1] = true;
				visited[i][j - 1] = true;
                
				//...
                //๋ณต๊ตฌ
                visited[i][j] = false;
				visited[i + 1][j] = false;
				visited[i - 1][j] = false;
				visited[i][j + 1] = false;
				visited[i][j - 1] = false;

5.์‹ฌ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ : canPlant

๊ฝƒ์ด ํ”ผ๋Š” ์ž๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ธํƒ€๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ์ž๋ฆฌ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.

๊ฝƒ์ด ์ฐจ์ง€ํ•  5์นธ์ด ๋ฒ”์œ„ ๋ฐ–์ด๊ฑฐ๋‚˜, ์ด๋ฏธ ๋‹ค๋ฅธ ๊ฝƒ์ด ์‹ฌ๊ฒจ ์žˆ์œผ๋ฉด false.


	// ๊ฝƒ ์‹ฌ๋Š” ๋ฒ”์œ„ -> ๋ธํƒ€๋ฅผ ํ™œ์šฉํ•œ ์ด๋™
	static int[] di = { 0, 1, -1, 0, 0 };
	static int[] dj = { 0, 0, 0, 1, -1 };

	// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
	private static boolean canPlant(int x, int y, boolean[][] visited, int N) {
		for (int k = 0; k < 5; k++) {
			int nx = x + di[k];
			int ny = y + dj[k];
			// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์•ˆ๋œ๋‹ค.
			if (nx < 0 || nx >= N || ny < 0 || ny >= N)
				return false;
			// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ด๋ฉด ์•ˆ๋œ๋‹ค.
			if (visited[nx][ny])
				return false;
		}
		return true;
	}

6. ๊ฝƒ ๋น„์šฉ ๊ณ„์‚ฐ (plantCost)

์ค‘์‹ฌ๊ณผ ์ƒํ•˜์ขŒ์šฐ 4์นธ์˜ ๊ฐ€๊ฒฉ์„ ํ•ฉ์‚ฐ

	// ๊ฝƒ์˜ ๊ฐ€๊ฒฉ ๊ตฌํ•˜๊ธฐ
	private static int plantCost(int x, int y, int[][] arr) {
		int sum = 0;
		for (int k = 0; k < 5; k++) {
			sum += arr[x + di[k]][y + dj[k]];

		}
		return sum;
	}

7. ์ตœ์†Œ ๋น„์šฉ ๊ฐฑ์‹ 

์„ธ ๋ฒˆ์งธ ๊ฝƒ์„ ์‹ฌ์—ˆ์„ ๋•Œ ์ด ๋น„์šฉ ๊ณ„์‚ฐ ํ›„ minPrice ๊ฐฑ์‹ 

// 3๊ฐœ์˜ ์”จ์•— ๊ฐ’ ๊ตฌํ•˜๊ธฐ
int cost = plantCost(i, j, arr) + plantCost(x, y, arr) + plantCost(r, c, arr);

//ํ˜„์žฌ ๊ฐ€๊ฒฉ vs minPrice ์ค‘์— ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ
minPrice = Math.min(minPrice, cost);

์ „์ฒด์ฝ”๋“œ

package algorithmstudy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class ๊ฝƒ๊ธธ {

	// ๊ฝƒ ์‹ฌ๋Š” ๋ฒ”์œ„ -> ๋ธํƒ€๋ฅผ ํ™œ์šฉํ•œ ์ด๋™
	static int[] di = { 0, 1, -1, 0, 0 };
	static int[] dj = { 0, 0, 0, 1, -1 };

	// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
	private static boolean canPlant(int x, int y, boolean[][] visited, int N) {
		for (int k = 0; k < 5; k++) {
			int nx = x + di[k];
			int ny = y + dj[k];
			// ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์•ˆ๋œ๋‹ค.
			if (nx < 0 || nx >= N || ny < 0 || ny >= N)
				return false;
			// ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ด๋ฉด ์•ˆ๋œ๋‹ค.
			if (visited[nx][ny])
				return false;
		}
		return true;
	}

	// ๊ฝƒ์˜ ๊ฐ€๊ฒฉ ๊ตฌํ•˜๊ธฐ
	private static int plantCost(int x, int y, int[][] arr) {
		int sum = 0;
		for (int k = 0; k < 5; k++) {
			sum += arr[x + di[k]][y + dj[k]];

		}
		return sum;
	}

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("algorithmstudy/src/input.txt"));
		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();

		int[][] arr = new int[N][N];

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				arr[i][j] = sc.nextInt();
			}
		}

		// ์ด๋ฏธ ์‹ฌ์€ ์ž๋ฆฌ๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•œ visited ๋ฐฐ์—ด
		// boolean ๋ฐฐ์—ด์˜ ๊ธฐ๋ณธ๊ฐ’์€ false
		boolean[][] visited = new boolean[N][N];

		// ์ตœ์†Œ๊ฐ€๊ฒฉ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
		// ์ดˆ๊ธฐ๊ฐ’์€ ์•„์ฃผ ํฐ ์ˆ˜๋กœ ์ง€์ •ํ•˜๊ธฐ
		int minPrice = 100000;

		// ์„ธ ๊ฐœ์˜ ์”จ์•—์„ ์‹ฌ๊ธฐ ์œ„ํ•œ 2์ฐจ์› ๋ฐฐ์—ด ์‚ผ์ค‘ for
		for (int i = 1; i < N - 1; i++) {
			for (int j = 1; j < N - 1; j++) {
				// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์—†๋‹ค๋ฉด continue
				if (!canPlant(i, j, visited, N))
					continue;
				// ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ๋Š” visited ๋ฐฐ์—ด์— true ํ•ด์ฃผ๊ธฐ
				visited[i][j] = true;
				visited[i + 1][j] = true;
				visited[i - 1][j] = true;
				visited[i][j + 1] = true;
				visited[i][j - 1] = true;

				for (int x = 1; x < N - 1; x++) {
					for (int y = 1; y < N - 1; y++) {

						// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์—†๋‹ค๋ฉด continue
						if (!canPlant(x, y, visited, N))
							continue;
						// ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ๋Š” visited ๋ฐฐ์—ด์— true ํ•ด์ฃผ๊ธฐ
						visited[x][y] = true;
						visited[x + 1][y] = true;
						visited[x - 1][y] = true;
						visited[x][y + 1] = true;
						visited[x][y - 1] = true;

						for (int r = 1; r < N - 1; r++) {
							for (int c = 1; c < N - 1; c++) {
								// ๊ฝƒ์„ ์‹ฌ์„ ์ˆ˜ ์—†๋‹ค๋ฉด continue
								if (!canPlant(r, c, visited, N))
									continue;
								// ๊ฝƒ์„ ์‹ฌ์€ ์ž๋ฆฌ๋Š” visited ๋ฐฐ์—ด์— true ํ•ด์ฃผ๊ธฐ
								visited[r][c] = true;
								visited[r + 1][c] = true;
								visited[r - 1][c] = true;
								visited[r][c + 1] = true;
								visited[r][c - 1] = true;

								// 3๊ฐœ์˜ ์”จ์•— ๊ฐ’ ๊ตฌํ•˜๊ธฐ
								int cost = plantCost(i, j, arr) + plantCost(x, y, arr) + plantCost(r, c, arr);
								
								//ํ˜„์žฌ ๊ฐ€๊ฒฉ vs minPrice ์ค‘์— ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ
								minPrice = Math.min(minPrice, cost);
								
								//์„ธ๋ฒˆ์งธ ๊ฝƒ ๋ณต๊ตฌ
								visited[r][c] = false;
								visited[r + 1][c] = false;
								visited[r - 1][c] = false;
								visited[r][c + 1] = false;
								visited[r][c - 1] = false;
							}

						}
						//๋‘๋ฒˆ์งธ ๊ฝƒ ๋ณต๊ตฌ 
						visited[x][y] = false;
						visited[x + 1][y] = false;
						visited[x - 1][y] = false;
						visited[x][y + 1] = false;
						visited[x][y - 1] = false;

					}
				}
				visited[i][j] = false;
				visited[i + 1][j] = false;
				visited[i - 1][j] = false;
				visited[i][j + 1] = false;
				visited[i][j - 1] = false;
			}

		}

		System.out.println(minPrice);

	}

}

0๊ฐœ์˜ ๋Œ“๊ธ€