Skip to content

Commit 3d7d1de

Browse files
committed
33week
1 parent 42aab5c commit 3d7d1de

File tree

4 files changed

+485
-0
lines changed

4 files changed

+485
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.example._33week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class GeriMandering2 {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static int[][] map;
12+
private static boolean[][] boarder;
13+
private static int answer = Integer.MAX_VALUE;
14+
15+
public static void main(String[] args) throws IOException {
16+
int N = Integer.parseInt(br.readLine());
17+
map = new int[N + 1][N + 1];
18+
boarder = new boolean[N + 1][N + 1];
19+
int total = 0;
20+
21+
for (int row = 1; row <= N; row++) {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
for (int col = 1; col <= N; col++) {
24+
map[row][col] = Integer.parseInt(st.nextToken());
25+
total += map[row][col];
26+
}
27+
}
28+
29+
for (int x = 1; x <= N; x++) {
30+
for (int y = 1; y <= N; y++) {
31+
32+
for (int d1 = 1; d1 < N; d1++) {
33+
for (int d2 = 1; d2 < N; d2++) {
34+
if (x + d1 + d2 > N) {
35+
continue;
36+
}
37+
38+
if (!(1 <= y - d1 && y + d2 <= N)) {
39+
continue;
40+
}
41+
42+
boarder = new boolean[N + 1][N + 1];
43+
int[] regions = new int[5];
44+
45+
for (int i = 0; i <= d1; i++) {
46+
boarder[x + i][y - i] = true;
47+
boarder[x + d2 + i][y + d2 - i] = true;
48+
}
49+
50+
for (int i = 0; i <= d2; i++) {
51+
boarder[x + i][y + i] = true;
52+
boarder[x + d1 + i][y - d1 + i] = true;
53+
}
54+
55+
56+
// 1구역
57+
for (int row = 1; row < x + d1; row++) {
58+
for (int col = 1; col <= N; col++) {
59+
if (boarder[row][col]) {
60+
break;
61+
}
62+
63+
regions[0] += map[row][col];
64+
}
65+
}
66+
67+
// 2구역
68+
for (int row = 1; row < x + d2; row++) {
69+
for (int col = N; col > y; col--) {
70+
if (boarder[row][col]) {
71+
break;
72+
}
73+
74+
regions[1] += map[row][col];
75+
}
76+
}
77+
78+
// 3구역
79+
for (int row = x + d1; row < N; row++) {
80+
for (int col = 1; col < y - d1 + d2; col++) {
81+
if (boarder[row][col]) {
82+
break;
83+
}
84+
85+
regions[2] += map[row][col];
86+
}
87+
}
88+
89+
// 4구역
90+
for (int row = x + d2 + 1; row <= N; row++) {
91+
for (int col = N; col >= y - d1 + d2; col--) {
92+
if (boarder[row][col]) {
93+
break;
94+
}
95+
96+
regions[3] += map[row][col];
97+
}
98+
}
99+
100+
regions[4] = total;
101+
for (int i = 0; i < 4; i++) {
102+
regions[4] -= regions[i];
103+
}
104+
105+
int max = Integer.MIN_VALUE;
106+
int min = Integer.MAX_VALUE;
107+
for (int i = 0; i < regions.length; i++) {
108+
max = Math.max(regions[i], max);
109+
min = Math.min(regions[i], min);
110+
}
111+
112+
answer = Math.min(max - min, answer);
113+
}
114+
}
115+
}
116+
}
117+
118+
System.out.println(answer);
119+
}
120+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package org.example._33week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.StringTokenizer;
10+
11+
public class NewGame2 {
12+
13+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
private static final int[] dr = {0, 0, 0, -1, 1};
16+
private static final int[] dc = {0, 1, -1, 0, 0};
17+
18+
private static final int WHITE = 0;
19+
private static final int RED = 1;
20+
private static final int BLUE = 2;
21+
22+
private static int mapSize;
23+
24+
public static void main(String[] args) throws IOException {
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
mapSize = Integer.parseInt(st.nextToken()) + 1;
27+
int tokenCount = Integer.parseInt(st.nextToken());
28+
29+
List<Token>[][] tokenMap = new List[mapSize][mapSize];
30+
int[][] map = new int[mapSize][mapSize];
31+
for (int row = 1; row < mapSize; row++) {
32+
st = new StringTokenizer(br.readLine());
33+
for (int col = 1; col < mapSize; col++) {
34+
map[row][col] = Integer.parseInt(st.nextToken());
35+
tokenMap[row][col] = new ArrayList<>();
36+
}
37+
}
38+
39+
List<Token> tokens = new ArrayList<>();
40+
for (int i = 0; i < tokenCount; i++) {
41+
st = new StringTokenizer(br.readLine());
42+
43+
int row = Integer.parseInt(st.nextToken());
44+
int col = Integer.parseInt(st.nextToken());
45+
int dir = Integer.parseInt(st.nextToken());
46+
Token token = new Token(row, col, dir);
47+
tokens.add(token);
48+
tokenMap[row][col].add(token);
49+
}
50+
51+
// Game Start
52+
for (int depth = 1; depth <= 1000; depth++) {
53+
for (Token token : tokens) {
54+
int currentTokenRow = token.row;
55+
int currentTokenCol = token.col;
56+
int nextRow = currentTokenRow + dr[token.dir];
57+
int nextCol = currentTokenCol + dc[token.dir];
58+
59+
if (!isInMap(nextRow, nextCol) || map[nextRow][nextCol] == BLUE) {
60+
token.reverseDir();
61+
nextRow = currentTokenRow + dr[token.dir];
62+
nextCol = currentTokenCol + dc[token.dir];
63+
64+
if (!isInMap(nextRow, nextCol) || map[nextRow][nextCol] == BLUE) {
65+
continue;
66+
}
67+
}
68+
69+
if (map[nextRow][nextCol] == WHITE) {
70+
// 1 step : tokenMap에서 몇번째 인덱스인지를 찾는다.
71+
int currentTokenIndex = token.index;
72+
int currentMapSize = tokenMap[currentTokenRow][currentTokenCol].size();
73+
74+
// 2 step : 다 이동시킨다.
75+
for (int idx = currentTokenIndex; idx < currentMapSize; idx++) {
76+
Token shouldMoveToken = tokenMap[currentTokenRow][currentTokenCol].get(idx);
77+
int nextIndex = tokenMap[nextRow][nextCol].size();
78+
tokenMap[nextRow][nextCol].add(shouldMoveToken);
79+
shouldMoveToken.row = nextRow;
80+
shouldMoveToken.col = nextCol;
81+
shouldMoveToken.index = nextIndex;
82+
}
83+
84+
// 3 step : 기존 데이터 삭제한다.
85+
for (int i = currentMapSize - 1; i >= currentTokenIndex; i--) {
86+
tokenMap[currentTokenRow][currentTokenCol].remove(i);
87+
}
88+
89+
// 4 step : 한 cell에 token이 4개 이상이면 게임 종료.
90+
if (tokenMap[nextRow][nextCol].size() >= 4) {
91+
System.out.println(depth);
92+
return;
93+
}
94+
95+
continue;
96+
}
97+
98+
if (map[nextRow][nextCol] == RED) {
99+
// 1 step : tokenMap에서 몇번째 인덱스인지를 찾는다.
100+
int currentTokenIndex = token.index;
101+
int currentMapSize = tokenMap[currentTokenRow][currentTokenCol].size();
102+
103+
// 2 step : 다 이동시킨다.
104+
for (int idx = currentMapSize - 1; idx >= currentTokenIndex; idx--) {
105+
Token shouldMoveToken = tokenMap[currentTokenRow][currentTokenCol].get(idx);
106+
int nextIndex = tokenMap[nextRow][nextCol].size();
107+
tokenMap[nextRow][nextCol].add(shouldMoveToken);
108+
shouldMoveToken.row = nextRow;
109+
shouldMoveToken.col = nextCol;
110+
shouldMoveToken.index = nextIndex;
111+
}
112+
113+
// 3 step : 기존 데이터 삭제한다.
114+
for (int i = currentMapSize - 1; i >= currentTokenIndex; i--) {
115+
tokenMap[currentTokenRow][currentTokenCol].remove(i);
116+
}
117+
118+
// 4 step : 한 cell에 token이 4개 이상이면 게임 종료.
119+
if (tokenMap[nextRow][nextCol].size() >= 4) {
120+
System.out.println(depth);
121+
return;
122+
}
123+
}
124+
}
125+
}
126+
127+
System.out.println(-1);
128+
}
129+
130+
private static boolean isInMap(int nextRow, int nextCol) {
131+
return !(nextRow >= mapSize || nextRow <= 0 || nextCol >= mapSize || nextCol <= 0);
132+
}
133+
134+
private static class Token {
135+
int row;
136+
int col;
137+
int dir;
138+
int index;
139+
140+
public Token(int row, int col, int dir) {
141+
this.row = row;
142+
this.col = col;
143+
this.dir = dir;
144+
this.index = 0;
145+
}
146+
147+
public void reverseDir() {
148+
switch (dir) {
149+
case 1:
150+
dir = 2;
151+
break;
152+
case 2:
153+
dir = 1;
154+
break;
155+
case 3:
156+
dir = 4;
157+
break;
158+
case 4:
159+
dir = 3;
160+
break;
161+
}
162+
}
163+
}
164+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.example._33week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class TwoDimensionalArraysAndOperations {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
private static int targetRow;
13+
private static int targetCol;
14+
private static int targetValue;
15+
16+
public static void main(String[] args) throws IOException {
17+
int[][] A = new int[100][100];
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
targetRow = Integer.parseInt(st.nextToken()) - 1;
20+
targetCol = Integer.parseInt(st.nextToken()) - 1;
21+
targetValue = Integer.parseInt(st.nextToken());
22+
23+
// A 초기화 끝.
24+
for (int i = 0; i < 3; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
for (int j = 0; j < 3; j++) {
27+
A[i][j] = Integer.parseInt(st.nextToken());
28+
}
29+
}
30+
31+
operation(A);
32+
}
33+
34+
private static void operation(int[][] a) {
35+
int maxRowSize = 3;
36+
int maxColSize = 3;
37+
38+
for (int depth = 0; depth <= 100; depth++) {
39+
if (a[targetRow][targetCol] == targetValue) {
40+
System.out.println(depth);
41+
return;
42+
}
43+
44+
int[][] newA = new int[100][100];
45+
if (maxRowSize >= maxColSize) {
46+
for (int row = 0; row < maxRowSize; row++) {
47+
Map<Integer, Integer> frequencyMap = new HashMap<>();
48+
for (int col = 0; col < maxColSize; col++) {
49+
if (a[row][col] == 0) {
50+
continue;
51+
}
52+
53+
Integer currentCount = frequencyMap.getOrDefault(a[row][col], 0);
54+
frequencyMap.put(a[row][col], currentCount + 1);
55+
}
56+
57+
58+
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(entryComparator());
59+
queue.addAll(frequencyMap.entrySet());
60+
61+
for (int idx = 0; idx < 50 && !queue.isEmpty(); idx += 2) {
62+
Map.Entry<Integer, Integer> poll = queue.poll();
63+
newA[row][idx] = poll.getKey();
64+
newA[row][idx + 1] = poll.getValue();
65+
maxColSize = Math.max((idx + 1) * 2, maxColSize);
66+
}
67+
}
68+
} else {
69+
for (int col = 0; col < maxRowSize; col++) {
70+
Map<Integer, Integer> frequencyMap = new HashMap<>();
71+
for (int row = 0; row < maxColSize; row++) {
72+
if (a[row][col] == 0) {
73+
continue;
74+
}
75+
76+
Integer currentCount = frequencyMap.getOrDefault(a[row][col], 0);
77+
frequencyMap.put(a[row][col], currentCount + 1);
78+
}
79+
80+
81+
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(entryComparator());
82+
queue.addAll(frequencyMap.entrySet());
83+
84+
for (int idx = 0; idx < 50 && !queue.isEmpty(); idx += 2) {
85+
Map.Entry<Integer, Integer> poll = queue.poll();
86+
newA[idx][col] = poll.getKey();
87+
newA[idx + 1][col] = poll.getValue();
88+
maxRowSize = Math.max((idx + 1) * 2, maxRowSize);
89+
}
90+
}
91+
}
92+
93+
a = newA;
94+
}
95+
96+
System.out.println(-1);
97+
}
98+
99+
private static Comparator<Map.Entry<Integer, Integer>> entryComparator() {
100+
return (e1, e2) -> {
101+
int valueCompare = Integer.compare(e1.getValue(), e2.getValue());
102+
if (valueCompare != 0) {
103+
return valueCompare;
104+
} else {
105+
return Integer.compare(e1.getKey(), e2.getKey());
106+
}
107+
};
108+
}
109+
110+
111+
}

0 commit comments

Comments
 (0)