Skip to content

Commit b591ef0

Browse files
feat: 알고리즘 학습
1 parent 1f088a5 commit b591ef0

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.algorithm2025.backjoon3.day002;
2+
3+
import java.util.Arrays;
4+
5+
public class Example20250621_Q1051 { //1051. Height Checker
6+
7+
public int heightChecker(int[] heights) {
8+
9+
int[] expectedHeights = Arrays.copyOf(heights, heights.length);
10+
Arrays.sort(expectedHeights);
11+
12+
int mismatchCount = 0;
13+
14+
for (int i = 0; i < heights.length; i++) {
15+
if (heights[i] != expectedHeights[i]) {
16+
mismatchCount++;
17+
}
18+
}
19+
20+
return mismatchCount;
21+
}
22+
23+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.algorithm2025.backjoon3.day002;
2+
3+
import java.util.*;
4+
5+
public class Example20250621_Q84021 { //코딩테스트 연습 깊이/너비 우선 탐색(DFS/BFS) 퍼즐 조각 채우기 복습
6+
7+
int[] dx = {0, 0, 1, -1};
8+
int[] dy = {1, -1, 0, 0};
9+
10+
public int Example20250621_Q84021(int[][] game_board, int[][] table) {
11+
int len = game_board.length;
12+
List<List<Point>> emptySpaces = extractBlocks(game_board, 0, true);
13+
List<List<Point>> tableBlocks = extractBlocks(table, 1, false);
14+
15+
boolean[] used = new boolean[emptySpaces.size()];
16+
int answer = 0;
17+
18+
for (List<Point> block : tableBlocks) {
19+
normalize(block);
20+
for (int i = 0; i < emptySpaces.size(); i++) {
21+
if (used[i] || block.size() != emptySpaces.get(i).size()) continue;
22+
if (canFit(block, emptySpaces.get(i))) {
23+
used[i] = true;
24+
answer += block.size();
25+
break;
26+
}
27+
}
28+
}
29+
30+
return answer;
31+
}
32+
33+
private List<List<Point>> extractBlocks(int[][] board, int target, boolean invert) {
34+
int len = board.length;
35+
boolean[][] visited = new boolean[len][len];
36+
List<List<Point>> blocks = new ArrayList<>();
37+
38+
for (int i = 0; i < len; i++) {
39+
for (int j = 0; j < len; j++) {
40+
int value = invert ? 1 - board[i][j] : board[i][j];
41+
if (value == target && !visited[i][j]) {
42+
blocks.add(bfs(i, j, board, visited, target, invert));
43+
}
44+
}
45+
}
46+
return blocks;
47+
}
48+
49+
private List<Point> bfs(int x, int y, int[][] board, boolean[][] visited, int target, boolean invert) {
50+
Queue<Point> q = new LinkedList<>();
51+
List<Point> shape = new ArrayList<>();
52+
53+
q.add(new Point(x, y));
54+
visited[x][y] = true;
55+
shape.add(new Point(0, 0));
56+
57+
while (!q.isEmpty()) {
58+
Point p = q.poll();
59+
60+
for (int d = 0; d < 4; d++) {
61+
int nx = p.x + dx[d];
62+
int ny = p.y + dy[d];
63+
64+
if (nx >= 0 && ny >= 0 && nx < board.length && ny < board.length && !visited[nx][ny]) {
65+
int value = invert ? 1 - board[nx][ny] : board[nx][ny];
66+
if (value == target) {
67+
visited[nx][ny] = true;
68+
q.add(new Point(nx, ny));
69+
shape.add(new Point(nx - x, ny - y));
70+
}
71+
}
72+
}
73+
}
74+
return shape;
75+
}
76+
77+
private boolean canFit(List<Point> block, List<Point> space) {
78+
for (int i = 0; i < 4; i++) {
79+
normalize(block);
80+
if (match(block, space)) return true;
81+
rotate(block);
82+
}
83+
return false;
84+
}
85+
86+
private void rotate(List<Point> shape) {
87+
for (Point p : shape) {
88+
int temp = p.x;
89+
p.x = p.y;
90+
p.y = -temp;
91+
}
92+
}
93+
94+
private void normalize(List<Point> shape) {
95+
int minX = Integer.MAX_VALUE;
96+
int minY = Integer.MAX_VALUE;
97+
for (Point p : shape) {
98+
minX = Math.min(minX, p.x);
99+
minY = Math.min(minY, p.y);
100+
}
101+
for (Point p : shape) {
102+
p.x -= minX;
103+
p.y -= minY;
104+
}
105+
Collections.sort(shape);
106+
}
107+
108+
private boolean match(List<Point> a, List<Point> b) {
109+
for (int i = 0; i < a.size(); i++) {
110+
if (a.get(i).x != b.get(i).x || a.get(i).y != b.get(i).y) return false;
111+
}
112+
return true;
113+
}
114+
115+
static class Point implements Comparable<Point> {
116+
int x, y;
117+
Point(int x, int y) {
118+
this.x = x;
119+
this.y = y;
120+
}
121+
122+
public int compareTo(Point o) {
123+
return x != o.x ? Integer.compare(x, o.x) : Integer.compare(y, o.y);
124+
}
125+
}
126+
127+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.algorithm2025.backjoon3.day002;
2+
3+
public class Example20250621_Q92345 { //코딩테스트 연습 2022 KAKAO BLIND RECRUITMENT 사라지는 발판
4+
int[] dx = {0, 0, -1, 1};
5+
int[] dy = {-1, 1, 0, 0};
6+
int n, m;
7+
8+
boolean OOB(int x, int y) {
9+
return x < 0 || x >= n || y < 0 || y >= m;
10+
}
11+
12+
int[][] vis = new int[5][5];
13+
int[][] block = new int[5][5];
14+
int solve(int curx, int cury, int opx, int opy) {
15+
16+
if (vis[curx][cury] == 1) return 0;
17+
18+
19+
int minWinTurns = Integer.MAX_VALUE;
20+
21+
int maxLoseTurns = 0;
22+
23+
boolean canMove = false;
24+
25+
26+
for (int dir = 0; dir < 4; dir++) {
27+
int nx = curx + dx[dir];
28+
int ny = cury + dy[dir];
29+
30+
31+
if (OOB(nx, ny) || vis[nx][ny] == 1 || block[nx][ny] == 0) continue;
32+
33+
canMove = true;
34+
vis[curx][cury] = 1;
35+
36+
37+
int turns = solve(opx, opy, nx, ny) + 1;
38+
39+
vis[curx][cury] = 0;
40+
41+
42+
if (turns % 2 == 0) {
43+
44+
maxLoseTurns = Math.max(maxLoseTurns, turns);
45+
}
46+
47+
else {
48+
49+
minWinTurns = Math.min(minWinTurns, turns);
50+
}
51+
}
52+
53+
54+
if (!canMove) {
55+
return 0;
56+
}
57+
58+
59+
if (minWinTurns != Integer.MAX_VALUE) {
60+
return minWinTurns;
61+
}
62+
63+
else {
64+
return maxLoseTurns;
65+
}
66+
}
67+
68+
public int solution(int[][] board, int[] aloc, int[] bloc) {
69+
n = board.length;
70+
m = board[0].length;
71+
for (int i = 0; i < n; i++) {
72+
for (int j = 0; j < m; j++) {
73+
block[i][j] = board[i][j];
74+
}
75+
}
76+
return solve(aloc[0], aloc[1], bloc[0], bloc[1]);
77+
}
78+
}

0 commit comments

Comments
 (0)