๐Ÿ”‘ Problem Solving/๐Ÿ’ป Samsung SW Expert

[JAVA] BOJ ๋ฐฑ์ค€ 17779 ๊ฒŒ๋ฆฌ๋ฉ˜๋”๋ง2

snowball๐Ÿฐ 2023. 2. 17. 21:35

๋ฌธ์ œ

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

์žฌํ˜„์‹œ๋Š” ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ฒฉ์ž๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฉ์ž์˜ ๊ฐ ์นธ์€ ๊ตฌ์—ญ์„ ์˜๋ฏธํ•˜๊ณ , rํ–‰ c์—ด์— ์žˆ๋Š” ๊ตฌ์—ญ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ตฌ์—ญ์„ ๋‹ค์„ฏ ๊ฐœ์˜ ์„ ๊ฑฐ๊ตฌ๋กœ ๋‚˜๋ˆ ์•ผ ํ•˜๊ณ , ๊ฐ ๊ตฌ์—ญ์€ ๋‹ค์„ฏ ์„ ๊ฑฐ๊ตฌ ์ค‘ ํ•˜๋‚˜์— ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค. ์„ ๊ฑฐ๊ตฌ๋Š” ๊ตฌ์—ญ์„ ์ ์–ด๋„ ํ•˜๋‚˜ ํฌํ•จํ•ด์•ผ ํ•˜๊ณ , ํ•œ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ตฌ์—ญ์€ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ตฌ์—ญ A์—์„œ ์ธ์ ‘ํ•œ ๊ตฌ์—ญ์„ ํ†ตํ•ด์„œ ๊ตฌ์—ญ B๋กœ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ, ๋‘ ๊ตฌ์—ญ์€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์ค‘๊ฐ„์— ํ†ตํ•˜๋Š” ์ธ์ ‘ํ•œ ๊ตฌ์—ญ์€ 0๊ฐœ ์ด์ƒ์ด์–ด์•ผ ํ•˜๊ณ , ๋ชจ๋‘ ๊ฐ™์€ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋œ ๊ตฌ์—ญ์ด์–ด์•ผ ํ•œ๋‹ค.

์„ ๊ฑฐ๊ตฌ๋ฅผ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ๊ธฐ์ค€์  (x, y)์™€ ๊ฒฝ๊ณ„์˜ ๊ธธ์ด d1, d2๋ฅผ ์ •ํ•œ๋‹ค. (d1, d2 ≥ 1, 1 ≤ x < x+d1+d2 ≤ N, 1 ≤ y-d1 < y < y+d2 ≤ N)
  2. ๋‹ค์Œ ์นธ์€ ๊ฒฝ๊ณ„์„ ์ด๋‹ค.
    1. (x, y), (x+1, y-1), ..., (x+d1, y-d1)
    2. (x, y), (x+1, y+1), ..., (x+d2, y+d2)
    3. (x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)
    4. (x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)
  3. ๊ฒฝ๊ณ„์„ ๊ณผ ๊ฒฝ๊ณ„์„ ์˜ ์•ˆ์— ํฌํ•จ๋˜์–ด์žˆ๋Š” ๊ณณ์€ 5๋ฒˆ ์„ ๊ฑฐ๊ตฌ์ด๋‹ค.
  4. 5๋ฒˆ ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋˜์ง€ ์•Š์€ ๊ตฌ์—ญ (r, c)์˜ ์„ ๊ฑฐ๊ตฌ ๋ฒˆํ˜ธ๋Š” ๋‹ค์Œ ๊ธฐ์ค€์„ ๋”ฐ๋ฅธ๋‹ค.
    • 1๋ฒˆ ์„ ๊ฑฐ๊ตฌ: 1 ≤ r < x+d1, 1 ≤ c ≤ y
    • 2๋ฒˆ ์„ ๊ฑฐ๊ตฌ: 1 ≤ r ≤ x+d2, y < c ≤ N
    • 3๋ฒˆ ์„ ๊ฑฐ๊ตฌ: x+d1 ≤ r ≤ N, 1 ≤ c < y-d1+d2
    • 4๋ฒˆ ์„ ๊ฑฐ๊ตฌ: x+d2 < r ≤ N, y-d1+d2 ≤ c ≤ N

์•„๋ž˜๋Š” ํฌ๊ธฐ๊ฐ€ 7×7์ธ ์žฌํ˜„์‹œ๋ฅผ ๋‹ค์„ฏ ๊ฐœ์˜ ์„ ๊ฑฐ๊ตฌ๋กœ ๋‚˜๋ˆˆ ๋ฐฉ๋ฒ•์˜ ์˜ˆ์‹œ์ด๋‹ค.

x = 2, y = 4, d1 = 2, d2 = 2 x = 2, y = 5, d1 = 3, d2 = 2 x = 4, y = 3, d1 = 1, d2 = 1

๊ตฌ์—ญ (r, c)์˜ ์ธ๊ตฌ๋Š” A[r][c]์ด๊ณ , ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ๋Š” ์„ ๊ฑฐ๊ตฌ์— ํฌํ•จ๋œ ๊ตฌ์—ญ์˜ ์ธ๊ตฌ๋ฅผ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์ด๋‹ค. ์„ ๊ฑฐ๊ตฌ๋ฅผ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ• ์ค‘์—์„œ, ์ธ๊ตฌ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์„ ๊ฑฐ๊ตฌ์™€ ๊ฐ€์žฅ ์ ์€ ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

์ „์ฒด ์ฝ”๋“œ

import java.util.*;
import java.io.*;

public class Main {
	static int N;
	static int[][] city;
	static int result;
	static int x;
	static int y;
	static int d1;
	static int d2;
	
    public static void main(String[] args) throws Exception {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
        
        N = Integer.parseInt(st.nextToken());
        city = new int[N][N];
        for(int i=0; i<N; i++) {
        	st = new StringTokenizer(bf.readLine(), " ");
        	for(int j=0; j<N; j++)
        		city[i][j] = Integer.parseInt(st.nextToken());	
        }
        result = Integer.MAX_VALUE;
        dfs();
        sb.append(result).append("\\n");
        System.out.println(sb.toString());
        
    }
    static void dfs() {
    	for(int i = 0; i<N; i++)
    		for(int j =0 ; j<N; j++)
    			for(int ld = 1; ld<N; ld++)
    				for(int rd = 1; rd<N; rd++) {
    					if(i+ld+rd >= N || j+rd >= N || j-ld <0) continue;
    					x = i; y = j; d1 = ld; d2 = rd;
    					check();
    					
    				}
    	
    }
    static void check() {
    	int[][] check = new int[N][N];
    	//5๋ฒˆ ์„ ๊ฑฐ๊ตฌ
    	int n =0;int j = y; int ld = 0; int rd = 0; int lflag = 0; int rflag = 0;
    	while(true) {
    		for(int k=j-ld; k<j+rd+1; k++) 
    			check[x+n][k] = 5;
    		n++;
    		if(lflag == 0) ld += 1;
    		else ld-=1;
    		if(rflag == 0) rd += 1;
    		else rd-=1;
    		
    		if(ld == d1) lflag = 1;
    		if(rd == d2) rflag = 1;
    		if(n == d1+d2+1) break;
    		
    	}
    	
    	//1๋ฒˆ ์„ ๊ฑฐ๊ตฌ
    	for(int i=0; i<x+d1; i++)
    		for(j=0; j<=y;j++) {
    			if(check[i][j] == 5) continue;
    			check[i][j] = 1;
    	}
    	//2๋ฒˆ ์„ ๊ฑฐ๊ตฌ
    	for(int i=0; i<=x+d2; i++)
    		for(j=y+1; j<N;j++) {
    			if(check[i][j] == 5) continue;
    			check[i][j] = 2;
    	}
    	//3๋ฒˆ ์„ ๊ฑฐ๊ตฌ
    	for(int i=x+d1; i<N; i++)
    		for(j=0; j<=y-d1+d2;j++) {
    			if(check[i][j] == 5) continue;
    			check[i][j] = 3;
    	}
    	
    	//4๋ฒˆ ์„ ๊ฑฐ๊ตฌ
    	for(int i=x+d2+1; i<N; i++)
    		for(j=y-d1+d2; j<N;j++) {
    			if(check[i][j] == 5) continue;
    			check[i][j] = 4;
    	}
    	
    	int[] pop = new int[5];
    	for(int i=0; i<N; i++)
    		for(j=0; j<N; j++)
    			pop[check[i][j]-1] += city[i][j];
    	int max = 0; int min = Integer.MAX_VALUE;
    	for(int i=0; i<5; i++) {
    		if(pop[i] > max) max = pop[i];
    		if(pop[i] < min) min = pop[i];
    	}
    	if(result > max- min) result = max-min;
    	
    }
}

ํ•œ ์ค„ ์”ฉ ๋ณด์ž!

static int N;
static int[][] city;
static int result;
static int x;
static int y;
static int d1;
static int d2;

N, city๋Š” ๋„์‹œ๋ฐฐ์—ด์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ธ๊ตฌ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์„ ๊ฑฐ๊ตฌ์™€ ๊ฐ€์žฅ ์ ์€ ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ฐ’์€ result์— ๋‹ด์•„ ๊ฐฑ์‹ ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

x, y๋Š” ๊ธฐ์ค€์  d1, d2๋Š” ๊ฒฝ๊ณ„์˜ ๊ธธ์ด๋ฅผ ๋‹ด์•„์ค„ ๊ฒƒ์ด๋‹ค.

static void dfs() {
	for(int i = 0; i<N; i++)
		for(int j =0 ; j<N; j++)
			for(int ld = 1; ld<N; ld++)
				for(int rd = 1; rd<N; rd++) {
					if(i+ld+rd >= N || j+rd >= N || j-ld <0) continue;
					x = i; y = j; d1 = ld; d2 = rd;
					check();
				}
}

dfs()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ 4์ค‘ for๋ฌธ์œผ๋กœ 5๋ฒˆ ์„ ๊ฑฐ ๊ตฌ์—ญ์˜ d1, d2๋ฅผ ๊ฒฐ์ •ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ d1๊ณผ d2์˜ ์ œํ•œ ์กฐ๊ฑด์œผ๋กœ if๋ฌธ์—์„œ ์ œํ•œ ์กฐ๊ฑด์— ์œ„๋ฐฐ๋˜๋Š” ๊ฒฝ์šฐ๋Š” continue์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

static void check() {
	int[][] check = new int[N][N];
	//5๋ฒˆ ์„ ๊ฑฐ๊ตฌ
	int n =0;int j = y; int ld = 0; int rd = 0; int lflag = 0; int rflag = 0;
	while(true) {
		for(int k=j-ld; k<j+rd+1; k++) 
			check[x+n][k] = 5;
		n++;
		if(lflag == 0) ld += 1;
		else ld-=1;
		if(rflag == 0) rd += 1;
		else rd-=1;
		
		if(ld == d1) lflag = 1;
		if(rd == d2) rflag = 1;
		if(n == d1+d2+1) break;
		
	}

์„ ๊ฑฐ๊ตฌ๊ฐ€ ํ™•์ •๋˜๋ฉด ๊ฐ ์นธ์„ ํ˜„์žฌ ํฌํ•จ๋œ ์„ ๊ฑฐ๊ตฌ์˜ ์ˆซ์ž๋กœ ๋ณ€๊ฒฝํ•ด์ค„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ check๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.

while๋ฌธ์„ ํ†ตํ•ด 5๋ฒˆ ๊ตฌ์—ญ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ™•์ •ํ•œ๋‹ค.

  1. n์„ ์ฆ๊ฐ€์‹œ์ผœ ๊ฐ€๋ฉด์„œ ํ•˜๋‚˜์˜ ํ–‰์„ ๊ฒ€์‚ฌํ•ด๋‚˜๊ฐ„๋‹ค.
  2. ํ˜„์žฌ ์œ„์น˜(y)์—์„œ ์™ผ์ชฝ์œผ๋กœ ๋–จ์–ด์ง„ ์นธ์˜ ์ˆ˜๋ฅผ ld, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋–จ์–ด์ง„ ์นธ์˜ ์ˆ˜๋ฅผ rd์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  3. ld, rd๊ฐ€ ์ œํ•œ ์กฐ๊ฑด(d1, d2)์— ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด flag๋ฅผ 1๋กœ ๋ฐ”๊พธ์–ด ์ฆ๊ฐ ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ€์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  4. n์ด 5๋ฒˆ ๊ตฌ์—ญ ํ–‰์„ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋˜๋ฉด ์ข…๋ฃŒ ํ•œ๋‹ค.

ํ•˜๋‚˜์˜ ์˜ˆ์‹œ๋ฅผ ์†Œ๊ฐœํ•˜๊ฒ ๋‹ค.

	//1๋ฒˆ ์„ ๊ฑฐ๊ตฌ
	for(int i=0; i<x+d1; i++)
		for(j=0; j<=y;j++) {
			if(check[i][j] == 5) continue;
			check[i][j] = 1;
	}
	//2๋ฒˆ ์„ ๊ฑฐ๊ตฌ
	for(int i=0; i<=x+d2; i++)
		for(j=y+1; j<N;j++) {
			if(check[i][j] == 5) continue;
			check[i][j] = 2;
	}
	//3๋ฒˆ ์„ ๊ฑฐ๊ตฌ
	for(int i=x+d1; i<N; i++)
		for(j=0; j<=y-d1+d2;j++) {
			if(check[i][j] == 5) continue;
			check[i][j] = 3;
	}
	
	//4๋ฒˆ ์„ ๊ฑฐ๊ตฌ
	for(int i=x+d2+1; i<N; i++)
		for(j=y-d1+d2; j<N;j++) {
			if(check[i][j] == 5) continue;
			check[i][j] = 4;
	}

5๋ฒˆ ๊ตฌ์—ญ์„ ํ™•์ •ํ–ˆ์œผ๋ฏ€๋กœ ๋‚จ์€ ๊ฒƒ์€ ๊ฐ„๋‹จํ•˜๋‹ค!

์ œํ•œ ์กฐ๊ฑด์„ ๊ผผ๊ผผํ•˜๊ฒŒ ํ™•์ธํ•˜๊ณ  ๊ฐ ๊ตฌ์—ญ์„ ํ™•์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

	int[] pop = new int[5];
	for(int i=0; i<N; i++)
		for(j=0; j<N; j++)
			pop[check[i][j]-1] += city[i][j];
	int max = 0; int min = Integer.MAX_VALUE;
	for(int i=0; i<5; i++) {
		if(pop[i] > max) max = pop[i];
		if(pop[i] < min) min = pop[i];
	}
	if(result > max- min) result = max-min;

pop ๋ฐฐ์—ด์— ๊ฐ ๊ตฌ์—ญ์˜ ์ธ๊ตฌ์ˆ˜๋ฅผ ์กฐ์‚ฌํ•˜์—ฌ ๋„ฃ์–ด์ค€๋‹ค. check๋ฐฐ์—ด์— ๊ฐ ๊ตฌ์—ญ์˜ ์ˆ˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์ธ๋ฑ์Šคํ™” ํ•˜์—ฌ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์˜ ์ฐจ๋ฅผ ๊ตฌํ•ด result๋ฅผ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋์ด๋‹ค!