-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
67-9kyo-hwang #228
base: main
Are you sure you want to change the base?
67-9kyo-hwang #228
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
첫 λ²μ§Έ νΈλΌμ΄
import java.util.*;
class Solution {
private static int N;
private static int[] dx = {0, 0, -1, 1};
private static int[] dy = {1, -1, 0, 0};
private static Map<Integer, Integer> blocks;
private static boolean[][] visited;
private static int answer;
private static int[][] table;
private static int[][] game_board;
public int solution(int[][] game_board, int[][] table) {
this.table = table;
this.game_board = game_board;
N = table.length;
answer = 0;
blocks = new HashMap<>();
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && table[i][j] == 1) {
count(i, j);
}
}
}
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && game_board[i][j] == 0) {
bfs(i, j);
}
}
}
return answer;
}
private static void bfs(int gx, int gy) {
visited[gx][gy] = true;
Deque<int[]> dq = new LinkedList<>();
dq.addLast(new int[]{gx, gy});
int count = 0;
while (!dq.isEmpty()) {
count++;
int[] now = dq.removeFirst();
int x = now[0];
int y = now[1];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isOut(nx, ny)) continue;
if (visited[nx][ny]) continue;
if (game_board[nx][ny] != 0) continue;
visited[nx][ny] = true;
dq.addLast(new int[]{nx, ny});
}
}
if (blocks.containsKey(count) && blocks.get(count) > 0) {
answer += count;
blocks.put(count, blocks.get(count) - 1);
}
}
private static void count(int gx, int gy) {
visited[gx][gy] = true;
Deque<int[]> dq = new LinkedList<>();
dq.addLast(new int[]{gx, gy});
int count = 0;
while (!dq.isEmpty()) {
count++;
int[] now = dq.removeFirst();
int x = now[0];
int y = now[1];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isOut(nx, ny)) continue;
if (visited[nx][ny]) continue;
if (table[nx][ny] != 1) continue;
visited[nx][ny] = true;
dq.addLast(new int[]{nx, ny});
}
}
blocks.put(count, blocks.getOrDefault(count, 0) + 1);
}
private static boolean isOut(int x, int y) {
return x < 0 || x >= N || y < 0 || y >= N;
}
}
μ.. λ¬Έμ λ₯Ό μλͺ» μ½μ΄μ 20λΆ λ λ¦¬κ³ λ€μ νμλ€μ..γ
γ
νμ€ννλλ° ν°λ¨Έλ¦¬ λκ³ ,, μ΄κ² λ λ§λμ§ μκ°νλλ° ν°λ¨Έλ¦¬ λ λ¬μ΅λλ€..
board ν¬κΈ°κ° 50μΌλ‘ μμμ Set μμ±νκ³ νμ©ν΄μ νμμ΅λλΉ
++μ΄κ² μ level3μ£ ...
λ λ²μ§Έ νΈλΌμ΄
import java.util.*;
class Solution {
private static final int[] dx = {-1, 1, 0, 0};
private static final int[] dy = {0, 0, -1, 1};
private static int N;
public int solution(int[][] game_board, int[][] table) {
N = game_board.length;
List<List<int[]>> emptySpaces = findBlocks(game_board, 0);
List<List<int[]>> puzzlePieces = findBlocks(table, 1);
boolean[] used = new boolean[puzzlePieces.size()];
int answer = 0;
for (List<int[]> space : emptySpaces) {
for (int i = 0; i < puzzlePieces.size(); i++) {
if (used[i]) continue;
List<int[]> piece = puzzlePieces.get(i);
if (canFit(space, piece)) {
used[i] = true;
answer += piece.size();
break;
}
}
}
return answer;
}
private List<List<int[]>> findBlocks(int[][] board, int target) {
boolean[][] visited = new boolean[N][N];
List<List<int[]>> blocks = new ArrayList<>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if(board[i][j] != target) continue;
if(visited[i][j]) continue;
List<int[]> block = new ArrayList<>();
dfs(board, i, j, visited, block, target);
normalize(block);
blocks.add(block);
}
}
return blocks;
}
private void dfs(int[][] board, int x, int y, boolean[][] visited, List<int[]> block, int target) {
if (x < 0 || x >= N || y < 0 || y >= N ||
visited[x][y] || board[x][y] != target) {
return;
}
visited[x][y] = true;
block.add(new int[]{x, y});
for (int i = 0; i < 4; i++) {
dfs(board, x + dx[i], y + dy[i], visited, block, target);
}
}
private void normalize(List<int[]> block) {
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
for (int[] point : block) {
minX = Math.min(minX, point[0]);
minY = Math.min(minY, point[1]);
}
for (int[] point : block) {
point[0] -= minX;
point[1] -= minY;
}
}
private boolean canFit(List<int[]> space, List<int[]> piece) {
if (space.size() != piece.size()) return false;
for (int rot = 0; rot < 4; rot++) {
if (isSameShape(space, piece)) return true;
rotate(piece);
}
return false;
}
private boolean isSameShape(List<int[]> shape1, List<int[]> shape2) {
Set<String> set1 = new HashSet<>();
Set<String> set2 = new HashSet<>();
for (int[] point : shape1) set1.add(point[0] + "," + point[1]);
for (int[] point : shape2) set2.add(point[0] + "," + point[1]);
return set1.equals(set2);
}
private void rotate(List<int[]> piece) {
for (int[] point : piece) {
int temp = point[0];
point[0] = point[1];
point[1] = -temp;
}
normalize(piece);
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¬Έμ 보μλ§μ 머리 μνμ€λ λ¬Έμ ... tableμ μλ blockλ§ λ½μλ΄κ³ , boardμ λ£λ건... ν¬κΈ°νμ΅λλ€π₯² κ·Έλ₯ λ λ€ λ€ λ리면 λλκ±°μκ΅°μ...
PR μ°Έκ³ ν΄μ μ€μ€μ₯ ν΄κ²°νκ³ κ°λλ€!
μ λ κ·Έλ₯ set μ μ°κ³ μ₯μ νμ΅λλ€
μμ₯μ°½
def solution(game_board, table):
puzzles, emptys = [], []
offset = [(-1, 0), (0, 1), (1, 0), (0, -1)]
tx, ty = len(table), len(table[0])
gx, gy = len(game_board), len(game_board[0])
def dfs_table(x, y):
puzzle = [(x, y)]
for dx, dy in offset:
nx, ny = x + dx, y + dy
if not 0 <= nx < tx or not 0 <= ny < ty:
continue
if table[nx][ny] == 0 or table[nx][ny] == 2:
continue
table[nx][ny] = 2
puzzle += dfs_table(nx, ny)
return puzzle
def dfs_board(x, y):
empty = [(x, y)]
for dx, dy in offset:
nx, ny = x + dx, y + dy
if not 0 <= nx < gx or not 0 <= ny < gy:
continue
if game_board[nx][ny] == 1 or game_board[nx][ny] == 2:
continue
game_board[nx][ny] = 2
empty += dfs_board(nx, ny)
return empty
def stadnard(space):
dx = min([i[0] for i in space])
dy = min([i[1] for i in space])
h = max([i[0] for i in space]) - dx + 1
w = max([i[1] for i in space]) - dy + 1
grid = [[0]*w for _ in range(h)]
for x, y in space:
grid[x-dx][y-dy] = 1
return grid
grid = [list(e) for e in zip(*grid[::-1])]
num = 0
for i in range(tx):
for j in range(ty):
if table[i][j] == 1:
table[i][j] = 2
num += 1
puzzles.append(stadnard(dfs_table(i, j)))
for i in range(gx):
for j in range(gy):
if game_board[i][j] == 0:
game_board[i][j] = 2
emptys.append(stadnard(dfs_board(i, j)))
result = 0
is_use = [False]*len(puzzles)
for e in emptys:
is_empty = True
for idx, p in enumerate(puzzles):
if is_use[idx]:
continue
cnt = 4
while cnt:
if e == p:
result += sum([sum(i) for i in e])
is_use[idx] = True
is_empty = False
break
else:
p = [list(e) for e in zip(*p[::-1])]
cnt -= 1
if not is_empty:
break
return result
π λ¬Έμ λ§ν¬
νΌμ¦ μ‘°κ° μ±μ°κΈ°
ν μ΄λΈμ λμΈ λΈλ‘μ κ²μ 보λμ λΉ κ³΅κ°μ μ¬λ €λμμΌ νλ€. μ΄ λ, λ€μ κ·μΉμ λ°λΌμΌ νλ€.
μμ κ°μ΄ 3, 4, 5λ² λΈλμ 격μ μΉΈμ λμΌλ©΄ κ·μΉμ μ΄κΈλ λΆκ°λ₯νλ€.
κ·μΉμ λ§κ² μ΅λν λ§μ λΈλμ μ±μ λ£μΌλ©΄ μμ κ°μΌλ©°, μ΄ 14μΉΈμ μ±μΈ μ μλ€.
κ·μΉμ λ§κ² μ΅λν λ§μ νΌμ¦ μ‘°κ°μ μ±μ λ£μ κ²½μ°, μ΄ λͺ μΉΈμ μ±μΈ μ μλμ§ return νλλ‘ ν¨μλ₯Ό μμ±νλΌ.
βοΈ μμλ μκ°
λͺ¨λ¦. λ§€μ° μ€λ κ±Έλ¦Ό
β¨ μλ μ½λ
λ¬Έμ μμ νΌμ¦ μ‘°κ°μ μ΅λ 6κ°κΉμ§λ§ μ£Όμ΄μ§λ€κ³ νμΌλ―λ‘, 보λμ ν μ΄λΈμμ νΌμ¦ μ‘°κ° μ 보λ₯Ό μ μ₯ν λ€ μ μ κ²μ¬νλ λ°©μμ μ¬μ©νλ€.
μ°μ νΌμ¦ μ‘°κ° μ 보λ₯Ό μ»μ΄λ΄λ κ²μ DFS/BFS λ°©μμΌλ‘ κ°λ₯νλ€. 2μ°¨μ λ°°μ΄μμ κ·Έλν μ°κ²° μμ μμλ΄λ κ²μ²λΌ ꡬν΄λΌ μ μλ€.
λ€λ§
game_board
μtable
μμ λͺ¨λ μΆμΆν΄λ΄μΌ νλλ°, κ²μ 보λλ λΉ μΉΈ / ν μ΄λΈμ μ±μμ§ μΉΈμ΄λ―λ‘ μ΄λ₯Ό ꡬλΆν΄μ£Όλ νλκ·ΈIsBoard
λ₯Ό ν΅ν΄ ꡬλΆνλ€.μ΄ ν¨μλ₯Ό μ΄μ©ν΄ λ€μκ³Ό κ°μ΄ game_boardμ λν λΈλλ€κ³Ό tableμ λν λΈλλ€ μ 보λ₯Ό μ»λλ€.
μ΄μ μ΄λ κ² μ»μ μ‘°κ°λ€μ κ°μ§κ³ μ μ μ‘°μ¬λ₯Ό μμνλ€. λͺ¨λ 보λ λΈλλ€μ λν΄, ν μ΄λΈ λΈλμ νλμ© νμ μν€λ©° νμΈνλ€.
μ΄λ¬ν λ‘μ§μΌλ‘ μ§νλλλ°, λ¬Έμ λ "λΈλλΌλ¦¬ μ΄λ»κ² λΉκ΅ν κ²μΈκ°"μ΄λ€. λλ μ¬κΈ°μ μ»μ΄λΈ λΈλλ€μ 2μ°¨μ λ°°μ΄λ‘ κ°κ³΅ μ²λ¦¬νλ€.
μλ₯Ό λ€μ΄ μμ κ·Έλ¦Όμμ μ£Όμ΄μ§ 2λ² λΈλμ κ²½μ° μ΄λ° μμΌλ‘ λ³ννλ€.
μ΄ κ³Όμ μ λ€κ³ μλ μ’νλ€μ μ΅λ, μ΅μ x/y μ’νκ°μ μ΄μ©ν΄ ꡬνν μ μλ€.
λ§μ§λ§μΌλ‘ λ¨μ 건 λΈλμ "νμ " κΈ°λ₯μ΄λ€.
μ΄λ λ€μκ³Ό κ°μ μ’ν λ³νμΌλ‘ κ°λ₯νλ€: [x, y] -> [y, N - 1 - x](N: 2μ°¨μ νλ ¬μ ν ν¬κΈ°)
μ€λΉκ° μλ£λμμΌλ, λͺ¨λ λΈλλΌλ¦¬ νμ μ νλ©΄μ λΉκ΅νλ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
νλ‘κ·Έλλ¨Έμ€ κ·Έλν μν μΉ΄ν κ³ λ¦¬μ μλ κ±΄λ° ν λ²μ―€ νμ΄λ΄μΌμ§... νκ³ λ λλ€κ° νμ΄λ³΄κ² λλλ°
ν... λ무 νλλ€. μ€κ°μ λΈλ‘κ·Έ μ°Έκ³ λ₯Ό λͺ λ²μ΄λ νλμ§ λͺ¨λ₯΄κ² λ€. μκ°λ μμ°½ λ λ €λ¨Ήμλ€.