[JAVA] BOJ ๋ฐฑ์ค 17779 ๊ฒ๋ฆฌ๋ฉ๋๋ง2
๋ฌธ์
์ฌํ์์ ์์ฅ ๊ตฌ์ฌํ์ ์ง๋ ๋ช ๋ ๊ฐ ๊ฒ๋ฆฌ๋งจ๋๋ง์ ํตํด์ ์์ ์ ๋น์๊ฒ ์ ๋ฆฌํ๊ฒ ์ ๊ฑฐ๊ตฌ๋ฅผ ํ์ ํ๋ค. ๊ฒฌ์ ํ ๊ถ๋ ฅ์ด ์์ด์ง ๊ตฌ์ฌํ์ ๊ถ๋ ฅ์ ๋งค์ฐ ๋ถ๋นํ๊ฒ ํ์ฌํ๊ณ , ์ฌ์ง์ด๋ ์์ ์ด๋ฆ๋ ์ฌํ์๋ก ๋ณ๊ฒฝํ๋ค. ์ด๋ฒ ์ ๊ฑฐ์์๋ ์ต๋ํ ๊ณตํํ๊ฒ ์ ๊ฑฐ๊ตฌ๋ฅผ ํ์ ํ๋ ค๊ณ ํ๋ค.
์ฌํ์๋ ํฌ๊ธฐ๊ฐ N×N์ธ ๊ฒฉ์๋ก ๋ํ๋ผ ์ ์๋ค. ๊ฒฉ์์ ๊ฐ ์นธ์ ๊ตฌ์ญ์ ์๋ฏธํ๊ณ , rํ c์ด์ ์๋ ๊ตฌ์ญ์ (r, c)๋ก ๋ํ๋ผ ์ ์๋ค. ๊ตฌ์ญ์ ๋ค์ฏ ๊ฐ์ ์ ๊ฑฐ๊ตฌ๋ก ๋๋ ์ผ ํ๊ณ , ๊ฐ ๊ตฌ์ญ์ ๋ค์ฏ ์ ๊ฑฐ๊ตฌ ์ค ํ๋์ ํฌํจ๋์ด์ผ ํ๋ค. ์ ๊ฑฐ๊ตฌ๋ ๊ตฌ์ญ์ ์ ์ด๋ ํ๋ ํฌํจํด์ผ ํ๊ณ , ํ ์ ๊ฑฐ๊ตฌ์ ํฌํจ๋์ด ์๋ ๊ตฌ์ญ์ ๋ชจ๋ ์ฐ๊ฒฐ๋์ด ์์ด์ผ ํ๋ค. ๊ตฌ์ญ A์์ ์ธ์ ํ ๊ตฌ์ญ์ ํตํด์ ๊ตฌ์ญ B๋ก ๊ฐ ์ ์์ ๋, ๋ ๊ตฌ์ญ์ ์ฐ๊ฒฐ๋์ด ์๋ค๊ณ ํ๋ค. ์ค๊ฐ์ ํตํ๋ ์ธ์ ํ ๊ตฌ์ญ์ 0๊ฐ ์ด์์ด์ด์ผ ํ๊ณ , ๋ชจ๋ ๊ฐ์ ์ ๊ฑฐ๊ตฌ์ ํฌํจ๋ ๊ตฌ์ญ์ด์ด์ผ ํ๋ค.
์ ๊ฑฐ๊ตฌ๋ฅผ ๋๋๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ๊ธฐ์ค์ (x, y)์ ๊ฒฝ๊ณ์ ๊ธธ์ด d1, d2๋ฅผ ์ ํ๋ค. (d1, d2 ≥ 1, 1 ≤ x < x+d1+d2 ≤ N, 1 ≤ y-d1 < y < y+d2 ≤ N)
- ๋ค์ ์นธ์ ๊ฒฝ๊ณ์ ์ด๋ค.
- (x, y), (x+1, y-1), ..., (x+d1, y-d1)
- (x, y), (x+1, y+1), ..., (x+d2, y+d2)
- (x+d1, y-d1), (x+d1+1, y-d1+1), ... (x+d1+d2, y-d1+d2)
- (x+d2, y+d2), (x+d2+1, y+d2-1), ..., (x+d2+d1, y+d2-d1)
- ๊ฒฝ๊ณ์ ๊ณผ ๊ฒฝ๊ณ์ ์ ์์ ํฌํจ๋์ด์๋ ๊ณณ์ 5๋ฒ ์ ๊ฑฐ๊ตฌ์ด๋ค.
- 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๋ฒ ๊ตฌ์ญ์ ๋ค์๊ณผ ๊ฐ์ด ํ์ ํ๋ค.
- n์ ์ฆ๊ฐ์์ผ ๊ฐ๋ฉด์ ํ๋์ ํ์ ๊ฒ์ฌํด๋๊ฐ๋ค.
- ํ์ฌ ์์น(y)์์ ์ผ์ชฝ์ผ๋ก ๋จ์ด์ง ์นธ์ ์๋ฅผ ld, ์ค๋ฅธ์ชฝ์ผ๋ก ๋จ์ด์ง ์นธ์ ์๋ฅผ rd์ ์ ์ฅํ ์ ์๊ฒ ํ๋ค.
- ld, rd๊ฐ ์ ํ ์กฐ๊ฑด(d1, d2)์ ๋์ด๊ฐ๊ฒ ๋๋ฉด flag๋ฅผ 1๋ก ๋ฐ๊พธ์ด ์ฆ๊ฐ ๋ฐฉํฅ์ ๋ฐ๊ฟ์ ์๊ฒ ํ๋ค.
- 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๋ฅผ ๊ฐฑ์ ํด์ฃผ๋ฉด ๋์ด๋ค!