Skip to content

solve(압축, 프렌즈4블록): rayeon_230905 #188

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions rayeon/week27/압축.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package week27;

import java.util.*;

class 압축 {
public int[] solution(String msg) {
Map<String, Integer> dictionary = new HashMap<>();
for (int i = 0; i < 27; i++) {
dictionary.put(String.valueOf((char)('A' + i)), (i+1));
}

List<Integer> list = new ArrayList<>();
int index = 0;
int sum = 0;
StringBuilder sb = new StringBuilder();
while (index < msg.length()) {
sb.append(msg.charAt(index));

if (dictionary.containsKey(sb.toString())) {
sum = dictionary.get(sb.toString());
} else {
list.add(sum);
dictionary.put(sb.toString(), dictionary.size());

sum = dictionary.get(String.valueOf(msg.charAt(index)));
sb = new StringBuilder(String.valueOf(msg.charAt(index)));
}

index++;
}
list.add(sum);

int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}

return answer;
}
}
104 changes: 104 additions & 0 deletions rayeon/week27/프렌즈4블록.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package week27;

import java.util.*;

class 프렌즈4블록 {
static int M,N;
static char[][] board;
static boolean[][] bombBoard;

static int[][] direction = {{1, 0}, {0, 1}, {1, 1}};

static void checkBomb(int row, int col) {
char target = board[row][col];

for (int[] d : direction) {
if (board[row + d[0]][col + d[1]] != target) {
return;
}
}

bombBoard[row][col] = true;
for (int[] d : direction) {
bombBoard[row + d[0]][col + d[1]] = true;
}
}

static int bomb() {
int count = 0;

for (int row = 0; row < M; row++) {
for (int col = 0; col < N; col++) {
if (bombBoard[row][col]) {
count++;
board[row][col] = ' ';
bombBoard[row][col] = false;
}
}
}

return count;
}

static void check() {
for (int row = 0; row < M - 1; row++) {
for (int col = 0; col < N - 1; col++) {
if (board[row][col] == ' ') {
continue;
}

checkBomb(row, col);
}
}
}

static void down() {
for (int col = 0; col < N; col++) {
Queue<int[]> emptyNodes = new LinkedList<>();

for (int row = M - 1; row >= 0; row--) {
if (board[row][col] == ' ') {
emptyNodes.offer(new int[]{row, col});

continue;
}

if (emptyNodes.isEmpty()) {
continue;
}

int[] node = emptyNodes.poll();
board[node[0]][node[1]] = board[row][col];
board[row][col] = ' ';
emptyNodes.offer(new int[]{row, col});
}
}
}

public int solution(int m, int n, String[] input) {
int answer = 0;

M = m;
N = n;
board = new char[M][N];
bombBoard = new boolean[M][N];

for (int row = 0; row < M; row++) {
board[row] = input[row].toCharArray();
}

while (true) {
check();

int count = bomb();
if (count == 0) {
break;
}

answer += count;
down();
}

return answer;
}
}