๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 4 BOJ #17406

by seolhee2750 2022. 8. 10.
๋ฌธ์ œ

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

 

17406๋ฒˆ: ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 4

ํฌ๊ธฐ๊ฐ€ N×M ํฌ๊ธฐ์ธ ๋ฐฐ์—ด A๊ฐ€ ์žˆ์„๋•Œ, ๋ฐฐ์—ด A์˜ ๊ฐ’์€ ๊ฐ ํ–‰์— ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜์˜ ํ•ฉ ์ค‘ ์ตœ์†Ÿ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค. ๋ฐฐ์—ด A๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ 1ํ–‰์˜ ํ•ฉ์€ 6, 2ํ–‰์˜ ํ•ฉ์€ 4, 3ํ–‰์˜ ํ•ฉ์€ 15์ด๋‹ค. ๋”ฐ๋ผ์„œ, ๋ฐฐ์—ด A์˜

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	
	static int n, m, k;
	static int[][] nums;
	static int[][] cals;
	static int min;
	static int[] dx = {1, 0, -1, 0};
	static int[] dy = {0, 1, 0, -1};
	static int[][] numsTmp;
	static int cnt = 0;
	
	public static int turn(int[][] res) {
		numsTmp = new int[n][m];
		for(int i = 0; i < n; i++) { // ๊นŠ์€ ๋ณต์‚ฌ
			for(int j = 0; j < m; j++) {
				numsTmp[i][j] = nums[i][j];
			}
		}
		
		for(int[] now: res) {
			int r = now[0];
			int c = now[1];
			int s = now[2];
			int[] point1 = {r-s-1, c-s-1}; // ๊ฐ€์žฅ ์ขŒ์ธก ์ƒ๋‹จ ๊ผญ์ง€์ 
			int[] point2 = {r+s-1, c+s-1}; // ๊ฐ€์žฅ ์šฐ์ธก ํ•˜๋‹จ ๊ผญ์ง€์ 
			int turnNum = Math.min(point2[0]-point1[0]+1, point2[1]-point1[1]+1) / 2; // ํšŒ์ „ํ•˜๋Š” ๋ ์˜ ๊ฐœ์ˆ˜
			
			for(int i = 0; i < turnNum; i++) { // ๊ฐ€์žฅ ์ขŒ์ธก ์ƒ๋‹จ ๊ผญ์ง€์ ์„ tmp๋กœ ๋‘๊ณ  ์‹œ๊ณ„๋ฐฉํ–ฅ ํšŒ์ „
				int x = point1[0] + i;
				int y = point1[1] + i;
				int startX = x;
				int startY = y;
				int tmp = numsTmp[x][y];
				int dir = 0;
				
				while(dir < 4) {
					int nx = x + dx[dir];
					int ny = y + dy[dir];
					
					if(nx < point1[0]+i || ny < point1[1]+i || nx > point2[0]-i || ny > point2[1]-i) {
						dir++;
						continue;
					}
					
					numsTmp[x][y] = numsTmp[nx][ny];
					x = nx;
					y = ny;
				}
				
				numsTmp[startX][startY+1] = tmp;
			}
		}
		
		int minSum = Integer.MAX_VALUE;
		for(int i = 0; i < n; i++) {
			int minTmp = 0;
			for(int j = 0; j < m; j++) {
				minTmp += numsTmp[i][j];
			}
			minSum = Math.min(minSum, minTmp);
		}
		return minSum; // ํ˜„์žฌ ์ˆ˜์—ด์˜ ํšŒ์ „ ์—ฐ์‚ฐ์„ ๋งˆ์นœ ๋ฐฐ์—ด์˜ ๊ฐ’ ๋ฆฌํ„ด
	}
	
	public static void permu(int[][] res, boolean[] visited, int depth) {
		if(depth == k) {
			min = Math.min(min, turn(res)); // ํ˜„์žฌ ๋งŒ๋“  ์ˆ˜์—ด์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์ „ ์—ฐ์‚ฐ ์ง„ํ–‰
			return;
		}
		
		for(int i = 0; i < k; i++) {
			if(!visited[i]) {
				visited[i] = true;
				res[depth] = cals[i];
				permu(res, visited, depth+1);
				visited[i] = false;
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		k = Integer.parseInt(st.nextToken());
		nums = new int[n][m];
		cals = new int[k][3];
		min = Integer.MAX_VALUE;
		
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(in.readLine());
			for(int j = 0; j < m; j++) {
				nums[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		for(int i = 0; i < k; i++) {
			st = new StringTokenizer(in.readLine());
			cals[i][0] = Integer.parseInt(st.nextToken());
			cals[i][1] = Integer.parseInt(st.nextToken());
			cals[i][2] = Integer.parseInt(st.nextToken());
		}
		
		permu(new int[k][3], new boolean[k], 0); // ์ˆ˜์—ด ๋งŒ๋“ค๋ฉด์„œ, ์ˆ˜์—ด ์™„์„ฑํ•  ๋•Œ๋งˆ๋‹ค ํšŒ์ „ ์—ฐ์‚ฐ
		
		System.out.println(min);
	}
}

๐Ÿ‘‰ ๊ตฌํ˜„

์ž…๋ ฅ๋˜๋Š” ํšŒ์ „ ์—ฐ์‚ฐ์„ ๋ชจ๋‘ ๋ฐ›์€ ๋’ค, ๊ทธ ์ˆœ์„œ์— ๋Œ€ํ•œ ์ˆœ์—ด์„ ๋งŒ๋“ค๊ณ 

ํ•˜๋‚˜์˜ ์ˆœ์—ด์ด ์™„์„ฑ๋  ๋•Œ๋งˆ๋‹ค ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ํšŒ์ „ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๋ฉฐ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐฑ์‹ ํ–ˆ๋‹ค.

(๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2์—์„œ ํšŒ์ „ ์—ฐ์‚ฐ์— ๊ด€ํ•œ ์ •๋ฆฌ๋ฅผ ํ•ด๋†“์•˜์Œ -> https://seolhee2750.tistory.com/226)

 

โœ”๏ธ ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ ์‹œ๋ฆฌ์ฆˆ ์ฝ”๋“œ ์ฐธ๊ณ 

#16926 ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 1

#16927 ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2

#16935 ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 3

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๋ณต์žกํ•ด๋ณด์˜€์ง€๋งŒ, ์ด์ „์— ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 1, 2, 3 ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ
    ๋ฐฐ์—ด ํšŒ์ „์— ๊ด€ํ•ด ๊ณต๋ถ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฝค ์‰ฝ๊ฒŒ(?) ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๋Œ“๊ธ€