๋ฐ์ํ
https://www.acmicpc.net/problem/17779
๋ฌธ์ ์ค๋ช
์ฌํ์ ๐ NxN ํฌ๊ธฐ, ๊ตฌ์ญ์ 5๊ฐ์ ์ ๊ฑฐ๊ตฌ๋ก ๋๋์ด์ผํจ
์ ๊ฑฐ๊ตฌ๋ฅผ ๋๋๋ ๋ฐฉ๋ฒ
- ๊ธฐ์ค์ (x,y)์ ๊ฒฝ๊ณ์ ๊ธธ์ด $d_1$,$d_2$
($d_1$,$d_2$ >= 1, 1 <= x < x+$d_1$+$d_2$ <= N, 1 <= y-$d_1$ < y < y+$d_2$ <= N) - ๊ฒฝ๊ณ์
1) (x,y),....,(x+$d_1$,y-$d_1$)
2) (x,y),....,(x+$d_2$,y+$d_2$)
3) (x+$d_1$,y-$d_1$),...,(x+$d_1$+$d_2$,y-$d_1$+$d_2$)
4) (x+$d_2$,y+$d_2$),...,(x+$d_2$+$d_1$,y+$d_2$-$d_1$) - ๊ฒฝ๊ณ์ ๊ณผ ์์ ํฌํจ๋ ๊ตฌ์ญ์ 5๋ฒ ์ ๊ฑฐ๊ตฌ
- 5๋ฒ ์ ๊ฑฐ๊ตฌ์ ํฌํจ๋์ง ์์ ๊ตฌ์ญ (r,c)๋
1๋ฒ ์ ๊ฑฐ๊ตฌ) 1 <= r < x+$d_1$, 1<= c <= y
2๋ฒ ์ ๊ฑฐ๊ตฌ) 1 <= r <= x+$d_2$, y < c <= N
3๋ฒ ์ ๊ฑฐ๊ตฌ) x+$d_1$ <= r <= N, 1 <= c < y-$d_1$+$d_2$
4๋ฒ ์ ๊ฑฐ๊ตฌ) x+$d_2$ < r <= N, y-$d_1$+$d_2$ <= c <= N
์ ๊ฑฐ๊ตฌ๋ฅผ ๋๋๋ ๋ฐฉ๋ฒ ์ค, ์ธ๊ตฌ๊ฐ ๊ฐ์ฅ ๋ง์ ์ ๊ฑฐ๊ตฌ - ์ธ๊ตฌ๊ฐ ๊ฐ์ฅ ์ ์ ์ ๊ฑฐ๊ตฌ์ ์ต์๊ฐ์ ์ถ๋ ฅ
๋ฌธ์ ํ์ด
4์ค ํฌ๋ฌธ์ผ๋ก ๋ฒ์๋ด์ ์์์ (x, y, $d_1$, $d_2$)๋ฅผ ์ ํํด ์ต์๊ฐ ํ์ธ!
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_BOJ_17778_๊ฒ๋ฆฌ๋งจ๋๋ง_2 {
static int N;
static int[][] A;
static int totalOfPeople = 0;
static int ans = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer;
N = Integer.parseInt(br.readLine());
A = new int[N][N];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
A[i][j] = Integer.parseInt(stringTokenizer.nextToken());
totalOfPeople += A[i][j];
}
}
for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++) {
for (int d1 = 1; d1 < N; d1++) {
for (int d2 = 1; d2 < N; d2++) {
if(x + d1 + d2 >= N) continue;
if(y - d1 < 0 || y + d2 >= N) continue;
solution(x, y, d1, d2);
}
}
}
}
System.out.println(ans);
}
private static void solution(int x, int y, int d1, int d2) {
boolean[][] borders = new boolean[N][N];
//๊ฒฝ๊ณ์
for (int i = 0; i <= d1; i++) {
borders[x + i][y - i] = true;
borders[x + d2 + i][y + d2 - i] = true;
}
for (int i = 0; i <= d2; i++) {
borders[x + i][y + i] = true;
borders[x + d1 + i][y - d1 + i] = true;
}
int[] sumOfPeople = new int[6];
// 1๊ตฌ์ญ
for (int i = 0; i < x + d1; i++) {
for (int j = 0; j <= y; j++) {
if(borders[i][j]) break;
sumOfPeople[1] += A[i][j];
}
}
// 2๊ตฌ์ญ
for (int i = 0; i <= x + d2; i++) {
for (int j = N - 1; j > y; j--) {
if(borders[i][j]) break;
sumOfPeople[2] += A[i][j];
}
}
// 3๊ตฌ์ญ
for (int i = x + d1; i < N; i++) {
for (int j = 0; j < y - d1 + d2; j++) {
if(borders[i][j]) break;
sumOfPeople[3] += A[i][j];
}
}
// 4๊ตฌ์ญ
for (int i = x + d2 + 1; i < N; i++) {
for (int j = N - 1; j >= y - d1 + d2; j--) {
if (borders[i][j]) break;
sumOfPeople[4] += A[i][j];
}
}
// 5๊ตฌ์ญ
sumOfPeople[5] = totalOfPeople;
for (int i = 1; i < 5; i++) {
sumOfPeople[5] -= sumOfPeople[i];
}
Arrays.sort(sumOfPeople);
ans = sumOfPeople[5] - sumOfPeople[1] < ans ? sumOfPeople[5] - sumOfPeople[1] : ans;
}
}
728x90
๋ฐ์ํ
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ BOJ / ๋ฐฑ์ค 20058 ] ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด์คํฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.05 |
---|---|
[ BOJ / ๋ฐฑ์ค 19238 ] ์คํํธ ํ์ ( ์๋ฐ / JAVA ) (0) | 2022.02.04 |
[ ๋ฐฑ์ค / BOJ 17140 ] ์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ์ฐ์ฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.02 |
[ ๋ฐฑ์ค / BOJ 17135 ] ์บ์ฌ ๋ํ์ค ( ์๋ฐ / JAVA ) (0) | 2022.01.31 |
[ ๋ฐฑ์ค / BOJ 16236 ] ์๊ธฐ ์์ด ( ์๋ฐ/JAVA ) (0) | 2022.01.30 |