From f8c3dba76cd76d4a16c82de096ceb268acc17c48 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Sat, 13 Apr 2024 22:22:32 +0900 Subject: [PATCH 1/5] =?UTF-8?q?solve:=ED=8F=AC=ED=83=91=20=EB=B6=80?= =?UTF-8?q?=EC=88=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1 \353\266\200\354\210\230\352\270\260.py" | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 "src/main/kotlin/heejik/58week/\355\217\254\355\203\221 \353\266\200\354\210\230\352\270\260.py" diff --git "a/src/main/kotlin/heejik/58week/\355\217\254\355\203\221 \353\266\200\354\210\230\352\270\260.py" "b/src/main/kotlin/heejik/58week/\355\217\254\355\203\221 \353\266\200\354\210\230\352\270\260.py" new file mode 100644 index 00000000..20ca7c0a --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\355\217\254\355\203\221 \353\266\200\354\210\230\352\270\260.py" @@ -0,0 +1,194 @@ +from collections import deque + +n, m, k = 0, 0, 0 +# 터렛들 집합 +board_turret = [] +# 터렛이 최근에 쏜 시점 집합 +board_turret_recent_attack = [] +# 공격 당한 터렛들 집합 Boolean 으로 표시 +board_turret_attacked = [] + +round = 1 + +visited = [] + + +def start_game(): + global round + + while round <= k: + attack_pos = pick_attack() + attacked_pos = pick_attacked() + # print("attack_pos:", attack_pos, "attacked_pos:", attacked_pos) + # print("before_attack", board_turret) + attack(attack_pos=attack_pos, attacked_pos=attacked_pos) + # print("after_attack", board_turret) + # 부서지지 않은 포탑이 1개라면 즉시 게임 종료. + zero_cnt = 0 + for x in range(n): + for y in range(m): + if board_turret[x][y] != 0: + zero_cnt += 1 + if zero_cnt == 1: + break + + repair_turret(attack_pos=attack_pos) + # print("after_repair", board_turret) + # print("attack_round", board_turret_recent_attack) + + round += 1 + # print("last: ", board_turret) + print(max(sum(board_turret, []))) + + +def pick_attack(): + # 공격력이 가장 낮은 포탑 찾기 + min_power = min(filter(lambda x: x != 0, sum(board_turret, []))) + pos_min_power = [] + + for x in range(n): + for y in range(m): + if board_turret[x][y] == min_power: + pos_min_power.append([x, y]) + if len(pos_min_power) == 1: return pos_min_power[0] + + # 최근에 공격한 포탑 찾기 + pos_min_power_recent_attack = [] + pos_min_power_recent_round = max(board_turret_recent_attack[x][y] for x, y in pos_min_power) + for x, y in pos_min_power: + if board_turret_recent_attack[x][y] == pos_min_power_recent_round: + pos_min_power_recent_attack.append([x, y]) + + if len(pos_min_power_recent_attack) == 1: return pos_min_power_recent_attack[0] + # 행과 열의 합 + # print(max(x + y for x, y in pos_min_power_recent_attack)) + max_sum_x_y = max(x + y for x, y in pos_min_power_recent_attack) + # 열이 가장 큰 + filtered_attack = filter(lambda x: x[0] + x[1] == max_sum_x_y, + [[x, y] for x, y in pos_min_power_recent_attack]) + return max(filtered_attack, key=lambda x: x[1]) + + +def pick_attacked(): + # 공격력이 높은 낮은 포탑 찾기 + max_power = max(sum(board_turret, [])) + pos_max_power = [] + + for x in range(n): + for y in range(m): + if board_turret[x][y] == max_power: + pos_max_power.append([x, y]) + if len(pos_max_power) == 1: return pos_max_power[0] + + # 공격한 지 오래된 포탑 찾기 + pos_max_power_old_attack = [] + pos_max_power_old_round = min(board_turret_recent_attack[x][y] for x, y in pos_max_power) + for x, y in pos_max_power: + if board_turret_recent_attack[x][y] == pos_max_power_old_round: + pos_max_power_old_attack.append([x, y]) + + if len(pos_max_power_old_attack) == 1: return pos_max_power_old_attack[0] + # 행과 열의 합 가장 작은 + min_sum_x_y = min(x + y for x, y in pos_max_power_old_attack) + # 열이 가장 작은 + filtered_attack = filter(lambda x: x[0] + x[1] == min_sum_x_y, + [[x, y] for x, y in pos_max_power_old_attack]) + + return min(filtered_attack, key=lambda x: x[1]) + + +def attack(attack_pos, attacked_pos): + # print("wwwwwwwwwwww", board_turret) + # print("dddddddddddd", attack_pos, attacked_pos) + board_turret[attack_pos[0]][attack_pos[1]] += n + m + board_turret_recent_attack[attack_pos[0]][attack_pos[1]] = round + # 어택당한 포탑 초기화 + for x in range(n): + for y in range(m): + board_turret_attacked[x][y] = False + + if not attack_laser(attack_pos, attacked_pos): + attack_cannon(attack_pos, attacked_pos) + + +def attack_laser(attack_pos, attacked_pos): + dx = [0, 1, 0, -1] + dy = [1, 0, -1, 0] + + for x in range(n): + for y in range(m): + visited[x][y] = False + + queue = deque() + queue.append([attack_pos, [attack_pos]]) + # print(board_turret) + power = board_turret[attack_pos[0]][attack_pos[1]] + + while queue: + [x, y], poses = queue.popleft() + + # print(poses) + # print("poses", poses) + if [x, y] == attacked_pos: + poses.remove(attack_pos) + poses.remove(attacked_pos) + # 피해 입히기 + for attacked_x, attacked_y in poses: + board_turret_attacked[attacked_x][attacked_y] = True + board_turret[attacked_x][attacked_y] = max(0, board_turret[attacked_x][attacked_y] - (power // 2)) + board_turret_attacked[x][y] = True + board_turret[x][y] = max(0, board_turret[x][y] - power) + return True + for i in range(4): + nx = (x + dx[i]) % n + ny = (y + dy[i]) % m + if visited[nx][ny]: continue + if board_turret[nx][ny] == 0: continue + if [nx, ny] in poses: continue + + queue.append([[nx, ny], poses + [[nx, ny]]]) + visited[nx][ny] = True + return False + + +def attack_cannon(attack_pos, attacked_pos): + dx = [0, 1, 0, -1, 1, 1, -1, -1] + dy = [1, 0, -1, 0, 1, -1, 1, -1] + + power = board_turret[attack_pos[0]][attack_pos[1]] + x = attacked_pos[0] + y = attacked_pos[1] + # print("cannon: ", power) + board_turret_attacked[x][y] = True + board_turret[x][y] = max(0, board_turret[x][y] - power) + for i in range(8): + nx = (x + dx[i]) % n + ny = (y + dy[i]) % m + # print("in cannon:", nx, ny) + if board_turret[nx][ny] == 0: continue + if nx == x and ny == y: continue + if nx == attack_pos[0] and ny == attack_pos[1]: continue + board_turret_attacked[nx][ny] = True + board_turret[nx][ny] = max(0, board_turret[nx][ny] - (power // 2)) + + +def repair_turret(attack_pos): + # for row in board_turret_attacked: + # print(row) + for x in range(n): + for y in range(m): + if [x, y] == attack_pos: continue + if board_turret_attacked[x][y]: continue + if board_turret[x][y] == 0: continue + board_turret[x][y] += 1 + + +if __name__ == '__main__': + n, m, k = map(int, input().split()) + for _ in range(n): + board_turret.append(list(map(int, input().split()))) + board_turret_recent_attack.append([0 for _ in range(m)]) + board_turret_attacked.append([False for _ in range(m)]) + + visited = [[False for _ in range(m)] for _ in range(n)] + start_game() From 283a50844d4f1b4720a04ff145420a2c8df158f6 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:37:27 +0900 Subject: [PATCH 2/5] solve:58week --- ...0\354\240\201 \355\203\220\354\202\254.py" | 111 ++++++++++++ ...4\355\230\270\354\235\230 \352\260\222.kt" | 51 ++++++ ...1\352\270\260 \353\206\200\354\235\264.py" | 142 +++++++++++++++ ...30\353\254\264\353\260\225\353\251\270.py" | 169 ++++++++++++++++++ ...5\354\235\264 \354\210\253\354\236\220.py" | 40 +++++ ... \354\225\244 \353\215\230\354\240\204.kt" | 30 ++++ ...04\353\241\234\354\240\235\355\212\270.py" | 32 ++++ .../\354\210\240\353\236\230\354\236\241.py" | 150 ++++++++++++++++ .../\354\213\270\354\233\200\353\225\205.py" | 116 ++++++++++++ ...1\354\271\230\352\270\260 \352\277\215.py" | 54 ++++++ .../\354\230\210\354\210\240\354\204\261.py" | 128 +++++++++++++ ...4 \352\265\264\353\246\254\352\270\260.py" | 109 +++++++++++ 12 files changed, 1132 insertions(+) create mode 100644 "src/main/kotlin/heejik/58week/\352\263\240\353\214\200 \353\254\270\353\252\205 \354\234\240\354\240\201 \355\203\220\354\202\254.py" create mode 100644 "src/main/kotlin/heejik/58week/\352\264\204\355\230\270\354\235\230 \352\260\222.kt" create mode 100644 "src/main/kotlin/heejik/58week/\352\274\254\353\246\254\354\236\241\352\270\260 \353\206\200\354\235\264.py" create mode 100644 "src/main/kotlin/heejik/58week/\353\202\230\353\254\264\353\260\225\353\251\270.py" create mode 100644 "src/main/kotlin/heejik/58week/\353\213\254\355\214\275\354\235\264 \354\210\253\354\236\220.py" create mode 100644 "src/main/kotlin/heejik/58week/\353\223\234\353\236\230\352\263\244 \354\225\244 \353\215\230\354\240\204.kt" create mode 100644 "src/main/kotlin/heejik/58week/\353\260\261\353\247\214 \354\236\245\354\236\220 \355\224\204\353\241\234\354\240\235\355\212\270.py" create mode 100644 "src/main/kotlin/heejik/58week/\354\210\240\353\236\230\354\236\241.py" create mode 100644 "src/main/kotlin/heejik/58week/\354\213\270\354\233\200\353\225\205.py" create mode 100644 "src/main/kotlin/heejik/58week/\354\226\221\354\271\230\352\270\260 \352\277\215.py" create mode 100644 "src/main/kotlin/heejik/58week/\354\230\210\354\210\240\354\204\261.py" create mode 100644 "src/main/kotlin/heejik/58week/\354\240\225\354\234\241\353\251\264\354\262\264 \355\225\234\353\262\210 \353\215\224 \352\265\264\353\246\254\352\270\260.py" diff --git "a/src/main/kotlin/heejik/58week/\352\263\240\353\214\200 \353\254\270\353\252\205 \354\234\240\354\240\201 \355\203\220\354\202\254.py" "b/src/main/kotlin/heejik/58week/\352\263\240\353\214\200 \353\254\270\353\252\205 \354\234\240\354\240\201 \355\203\220\354\202\254.py" new file mode 100644 index 00000000..2e1d2098 --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\352\263\240\353\214\200 \353\254\270\353\252\205 \354\234\240\354\240\201 \355\203\220\354\202\254.py" @@ -0,0 +1,111 @@ +from collections import deque +import heapq + +total_score = 0 + + +def solve(): + global total_score + + for _ in range(k): + total_score = 0 + b_x, b_y, b_degree = get_best_pos() + if b_x == -1: break + n_board = rotate(b_x, b_y, b_degree) + for i in range(5): + for j in range(5): + board[i][j] = n_board[i][j] + scored_pos = get_score(board) + total_score += len(scored_pos) + fill_piece(scored_pos) + # for row in board: + # print(row) + # print("-----------------------") + while True: + scored_pos = get_score(board) + if len(scored_pos) < 3: break + total_score += len(scored_pos) + fill_piece(scored_pos) + print(total_score, end=" ") + + +def get_best_pos(): + pos_list = [] + for degree in [90, 180, 270]: + for x in range(1, 4): + for y in range(1, 4): + n_board = rotate(x, y, degree) + score = len(get_score(n_board)) + heapq.heappush(pos_list, [-score, degree, y, x]) + + minus_score, degree, y, x = heapq.heappop(pos_list) + if -minus_score == 0: return -1, -1, -1 + return x, y, degree + + +def rotate(x, y, degree): + n_board = [[0] * 5 for _ in range(5)] + tmp_board = [[0] * 3 for _ in range(3)] + for i in range(5): + for j in range(5): + n_board[i][j] = board[i][j] + + x_offset = x - 1 + y_offset = y - 1 + for _ in range(degree // 90): + for i in range(3): + for j in range(3): + tmp_board[i][j] = n_board[3 - 1 - j + x_offset][i + y_offset] + + for i in range(3): + for j in range(3): + n_board[i + x_offset][j + y_offset] = tmp_board[i][j] + + return n_board + + +dx = [1, -1, 0, 0] +dy = [0, 0, 1, -1] + + +def get_score(n_board): + scored_pos = [] + visited = [[False] * 5 for _ in range(5)] + for i in range(5): + for j in range(5): + if visited[i][j]: continue + queue = deque() + queue.append([i, j]) + visited[i][j] = True + tmp_store = [[i, j]] + + while queue: + x, y = queue.popleft() + for d in range(4): + nx = x + dx[d] + ny = y + dy[d] + if nx not in range(5) or ny not in range(5): continue + if visited[nx][ny]: continue + if n_board[nx][ny] != n_board[x][y]: continue + visited[nx][ny] = True + queue.append([nx, ny]) + tmp_store.append([nx, ny]) + + if len(tmp_store) < 3: continue + for t_x, t_y in tmp_store: + heapq.heappush(scored_pos, [t_y, -t_x]) + + return scored_pos + + +def fill_piece(deleted_pos): + while deleted_pos: + y, x = heapq.heappop(deleted_pos) + board[-x][y] = piece.pop(0) + + +if __name__ == '__main__': + k, m = map(int, input().split()) + board = [list(map(int, input().split())) for _ in range(5)] + piece = list(map(int, input().split())) + solve() diff --git "a/src/main/kotlin/heejik/58week/\352\264\204\355\230\270\354\235\230 \352\260\222.kt" "b/src/main/kotlin/heejik/58week/\352\264\204\355\230\270\354\235\230 \352\260\222.kt" new file mode 100644 index 00000000..f0bfe1b2 --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\352\264\204\355\230\270\354\235\230 \352\260\222.kt" @@ -0,0 +1,51 @@ +class `괄호의 값`() { + fun solve() { + val input = readln() + val stack = ArrayDeque() + var totalScore = 0 + var score = 1 + for (i in input.indices) { + when (input[i]) { + '(' -> { + score *= 2 + stack.add(input[i]) + } + + '[' -> { + score *= 3 + stack.add(input[i]) + } + + ')' -> { + if (stack.lastOrNull() != '(') { + totalScore = 0 + break + } + if (input[i - 1] == '(') { + totalScore += score + } + score /= 2 + stack.removeLast() + } + + ']' -> { + if (stack.lastOrNull() != '[') { + totalScore = 0 + break + } + if (input[i - 1] == '[') { + totalScore += score + } + score /= 3 + stack.removeLast() + } + } + } + if (stack.isNotEmpty()) totalScore = 0 + print(totalScore) + } +} + +fun main() { + `괄호의 값`().solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/heejik/58week/\352\274\254\353\246\254\354\236\241\352\270\260 \353\206\200\354\235\264.py" "b/src/main/kotlin/heejik/58week/\352\274\254\353\246\254\354\236\241\352\270\260 \353\206\200\354\235\264.py" new file mode 100644 index 00000000..b56072ae --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\352\274\254\353\246\254\354\236\241\352\270\260 \353\206\200\354\235\264.py" @@ -0,0 +1,142 @@ +from collections import deque + +score = 0 +teams = [] +round = 0 +dx = [0, -1, 0, 1] +dy = [1, 0, -1, 0] + + +def in_range(x, y): + return x in range(n) and y in range(n) + + +def get_player(): + for x in range(n): + for y in range(n): + if board[x][y] == 1: + find_my_team(x, y) + + +def find_my_team(x, y): + tmp_team = [] + queue = deque() + queue.append([x, y, 1]) + board[x][y] = 4 + tmp_team.append([x, y]) + while queue: + x, y, val = queue.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if in_range(nx, ny) and board[nx][ny] % 4 != 0 and board[nx][ny] - val <= 1: + queue.append([nx, ny, board[nx][ny]]) + tmp_team.append([nx, ny]) + board[nx][ny] = 4 + + teams.append(tmp_team) + + +def start_game(): + global round + + get_player() + while round < k: + play() + round += 1 + print(score) + + +def play(): + for team in teams: + move(team) + + shot() + + +def move(team): + h_x, h_y = team[0] + s_x, s_y = team[1] + cant_dir = -1 + go_dir = -1 + + # 갈 수 없는 방향 + for i in range(4): + if [dx[i], dy[i]] == [s_x - h_x, s_y - h_y]: + cant_dir = i + break + + # 이동 방향 결정 + for i in range(4): + if i == cant_dir: continue + nx = h_x + dx[i] + ny = h_y + dy[i] + if in_range(nx, ny) and board[nx][ny] == 4: + go_dir = i + + # 이전 친구들은 앞에 애들의 위치에 간다 맨 앞은 제외하고 + for idx in range(len(team) - 1, 0, -1): + team[idx] = team[idx - 1] + + team[0] = [team[0][0] + dx[go_dir], team[0][1] + dy[go_dir]] + + +def shot(): + global score + + ball_direction = (round // n) % 4 + line = round % n + + # 오른쪽 발사, line 은 오름차순 0, 1, 2, ... + if ball_direction == 0: + for y in range(n): + b_x = line + b_y = y + for team in teams: + for idx, [x, y] in enumerate(team): + if x == b_x and y == b_y: + score += (idx + 1) ** 2 + team.reverse() + return + + # 위쪽 발사, line 은 오름차순 0, 1, 2, ... + elif ball_direction == 1: + for x in range(n - 1, -1, -1): + b_x = x + b_y = line + for team in teams: + for idx, [x, y] in enumerate(team): + if x == b_x and y == b_y: + score += (idx + 1) ** 2 + team.reverse() + return + + # `왼쪽 발사, line 은 내림차순 6, 5, 4, ... + elif ball_direction == 2: + for y in range(n - 1, -1, -1): + b_x = n - 1 - line + b_y = y + for team in teams: + for idx, [x, y] in enumerate(team): + if x == b_x and y == b_y: + score += (idx + 1) ** 2 + team.reverse() + return + + # 아래 발사, line 은 내림차순 6, 5, 4, ... + else: + for x in range(n): + b_x = x + b_y = n - 1 - line + for team in teams: + for idx, [x, y] in enumerate(team): + if x == b_x and y == b_y: + score += (idx + 1) ** 2 + team.reverse() + return + + +if __name__ == '__main__': + n, m, k = map(int, input().split()) + board = [list(map(int, input().split())) for _ in range(n)] + start_game() diff --git "a/src/main/kotlin/heejik/58week/\353\202\230\353\254\264\353\260\225\353\251\270.py" "b/src/main/kotlin/heejik/58week/\353\202\230\353\254\264\353\260\225\353\251\270.py" new file mode 100644 index 00000000..6e7000ef --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\353\202\230\353\254\264\353\260\225\353\251\270.py" @@ -0,0 +1,169 @@ +# 총 박멸된 나무들 +killed_tree = 0 +# 박멸된 친구들을 가늠할 기준치 +kill_offset = 10_000_000 + +# 상하좌우 +dx_udlr = [1, -1, 0, 0] +dy_udlr = [0, 0, -1, 1] + +# 대각선 +dx_diagonal = [1, 1, -1, -1] +dy_diagonal = [1, -1, 1, -1] + + +def is_tree(x, y): + return 0 < board[x][y] < kill_offset + + +def is_wall(x, y): + return board[x][y] == -1 + + +def is_empty(x, y): + return board[x][y] == 0 + + +def in_range(x, y): + return x in range(n) and y in range(n) + + +# 박멸시간 줄이기 +def reduce_kill_time(): + for x in range(n): + for y in range(n): + if board[x][y] > kill_offset: + board[x][y] -= 1 + if board[x][y] == kill_offset: + board[x][y] = 0 + + +def grow(): + for x in range(n): + for y in range(n): + if is_tree(x, y): + board[x][y] += get_count_side_tree(x, y) + + +def get_count_side_tree(x, y): + count = 0 + for i in range(4): + nx = x + dx_udlr[i] + ny = y + dy_udlr[i] + if in_range(nx, ny) and is_tree(nx, ny): + count += 1 + + return count + + +# 번식 +def breed(): + new_board = [[0] * n for _ in range(n)] + for x in range(n): + for y in range(n): + new_board[x][y] = board[x][y] + + for x in range(n): + for y in range(n): + if not is_tree(x, y): continue + count = 0 + store_side_empty = [] + for i in range(4): + nx = x + dx_udlr[i] + ny = y + dy_udlr[i] + if in_range(nx, ny) and is_empty(nx, ny): + count += 1 + store_side_empty.append([nx, ny]) + if count == 0: continue + breed_count = board[x][y] // count + for e_x, e_y in store_side_empty: + new_board[e_x][e_y] += breed_count + + for x in range(n): + for y in range(n): + board[x][y] = new_board[x][y] + + +def kill(): + global killed_tree + + k_x = -1 + k_y = -1 + k_cnt = 0 + # 박멸할 위치 찾기 + for x in range(n): + for y in range(n): + if not is_tree(x, y): continue + cnt = get_count_kill_tree(x, y) + if cnt > k_cnt: + k_x = x + k_y = y + k_cnt = cnt + + if k_cnt == 0: return + # 박멸. + killed_tree += board[k_x][k_y] + board[k_x][k_y] = kill_offset + c + for i in range(4): + nx = k_x + ny = k_y + for _ in range(k): + nx += dx_diagonal[i] + ny += dy_diagonal[i] + if not in_range(nx, ny): break + # 벽이라면 그냥 끝낸다. + if is_wall(nx, ny): break + # 비어있다면 거기까지 제초제를 뿌리고 끝낸다. + if is_empty(nx, ny): + board[nx][ny] = kill_offset + c + break + # 이미 제초제가 있는 칸이라면 제초제 시간을 초기화 시킨다. + if board[nx][ny] > kill_offset: + board[nx][ny] = kill_offset + c + break + # 나무에 제초제를 뿌리는 경우 + killed_tree += board[nx][ny] + board[nx][ny] = kill_offset + c + + +def get_count_kill_tree(x, y): + count_kill_tree = board[x][y] + for i in range(4): + nx = x + ny = y + for _ in range(k): + nx += dx_diagonal[i] + ny += dy_diagonal[i] + if not in_range(nx, ny): break + if is_wall(nx, ny): break + if is_empty(nx, ny): break + if board[nx][ny] > kill_offset: break + count_kill_tree += board[nx][ny] + + return count_kill_tree + + +if __name__ == '__main__': + n, m, k, c = map(int, input().split()) + c = c + 1 + board = [list(map(int, input().split())) for _ in range(n)] + + for _ in range(m): + grow() + # for row in board: + # print(row) + # print("==================") + breed() + # for row in board: + # print(row) + # print("==================") + kill() + # for row in board: + # print(row) + # print("==================") + reduce_kill_time() + # for row in board: + # print(row) + # print("==================") + + print(killed_tree) diff --git "a/src/main/kotlin/heejik/58week/\353\213\254\355\214\275\354\235\264 \354\210\253\354\236\220.py" "b/src/main/kotlin/heejik/58week/\353\213\254\355\214\275\354\235\264 \354\210\253\354\236\220.py" new file mode 100644 index 00000000..76c56987 --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\353\213\254\355\214\275\354\235\264 \354\210\253\354\236\220.py" @@ -0,0 +1,40 @@ +# import sys +# +# sys.stdin = open("input.txt", "r") + +dx = [0, 1, 0, -1] +dy = [1, 0, -1, 0] + +test_case = 1 + + +def solve(): + global test_case + + n = int(input()) + number = 1 + count = n + board = [[0] * n for _ in range(n)] + direction = 0 + x, y = 0, -1 + + while number <= n ** 2: + for _ in range(count): + x += dx[direction] + y += dy[direction] + board[x][y] = number + number += 1 + direction = (direction + 1) % 4 + if direction % 2 != 0: + count -= 1 + + print("#" + str(test_case)) + for row in board: + print(*row) + test_case += 1 + + +if __name__ == '__main__': + t = int(input()) + for _ in range(t): + solve() diff --git "a/src/main/kotlin/heejik/58week/\353\223\234\353\236\230\352\263\244 \354\225\244 \353\215\230\354\240\204.kt" "b/src/main/kotlin/heejik/58week/\353\223\234\353\236\230\352\263\244 \354\225\244 \353\215\230\354\240\204.kt" new file mode 100644 index 00000000..522bb1d9 --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\353\223\234\353\236\230\352\263\244 \354\225\244 \353\215\230\354\240\204.kt" @@ -0,0 +1,30 @@ +package heejik.`58week` + +import kotlin.math.min + +class `드래곤 앤 던전` { + + fun solve() { + var hp = 0L + var answer = hp + + var (n, atk) = readln().split(' ').map { it.toLong() } + repeat(n.toInt()) { + val (t, a, h) = readln().split(' ').map { it.toLong() } + if (t.toInt() == 1) { + val damagedCnt = if (h % atk != 0L) (h / atk) else ((h / atk) - 1) + hp -= (damagedCnt * a) + answer = min(answer, hp) + } else { + atk += a + hp = min(0, hp + h) + } + } + print(-answer + 1) + } +} + + +fun main() { + `드래곤 앤 던전`().solve() +} \ No newline at end of file diff --git "a/src/main/kotlin/heejik/58week/\353\260\261\353\247\214 \354\236\245\354\236\220 \355\224\204\353\241\234\354\240\235\355\212\270.py" "b/src/main/kotlin/heejik/58week/\353\260\261\353\247\214 \354\236\245\354\236\220 \355\224\204\353\241\234\354\240\235\355\212\270.py" new file mode 100644 index 00000000..01ac361a --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\353\260\261\353\247\214 \354\236\245\354\236\220 \355\224\204\353\241\234\354\240\235\355\212\270.py" @@ -0,0 +1,32 @@ +round = 1 + + +def solve(_prices): + money = 0 + + all_day = len(_prices) - 1 + day = 0 + + prices = _prices + while day < all_day: + max_price = max(prices) + max_index = prices.index(max_price) + # 산 가격 합 + buy = sum(prices[:max_index]) + # 팔기 + money += (max_price * max_index) - buy + # 날짜는 모두 판 다음 날 + day += max_index + 1 + # 가격도 다음 배열로 + prices = prices[max_index + 1:] + + print("#" + str(round), money) + + +if __name__ == '__main__': + t = int(input()) + for _ in range(t): + n = int(input()) + input_prices = list(map(int, input().split())) + solve(input_prices) + round += 1 diff --git "a/src/main/kotlin/heejik/58week/\354\210\240\353\236\230\354\236\241.py" "b/src/main/kotlin/heejik/58week/\354\210\240\353\236\230\354\236\241.py" new file mode 100644 index 00000000..11efd20a --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\354\210\240\353\236\230\354\236\241.py" @@ -0,0 +1,150 @@ +dx = [-1, 0, 1, 0] +dy = [0, 1, 0, -1] + +dx_reverse = [1, 0, -1, 0] +dy_reverse = [0, 1, 0, -1] + +# True -> 바깥쪽(초기값), False -> 안쪽 +catcher_outside = True +catcher_dir = 0 + +n, m, h, k = map(int, input().split()) +runners = [] +for _ in range(m): + # d:1 좌우, 오른쪽 d:2: 상하, 아래쪽 + x, y, d = map(int, input().split()) + runners.append([x - 1, y - 1, d]) + +tree_board = [[False] * n for _ in range(n)] + +for _ in range(h): + i, j = map(lambda x: int(x) - 1, input().split()) + tree_board[i][j] = True +catcher = [n // 2, n // 2] +move_catcher_offset = [] +move_catcher_offset_reverse = [] + +round = 1 +point = 0 + + +def in_range(x, y): + return x in range(n) and y in range(n) + + +def get_catcher_dxy(): + catcher_dx = dx[catcher_dir] if catcher_outside else dx_reverse[catcher_dir] + catcher_dy = dy[catcher_dir] if catcher_outside else dy_reverse[catcher_dir] + + return catcher_dx, catcher_dy + + +def make_catcher_move(): + change_offset = 2 + timer = 1 + count = 0 + i = 1 + while i < (n ** 2): + count += 1 + if count == change_offset: + timer += 1 + count = 0 + move_catcher_offset.append(i) + i += timer + + +def make_catcher_move_reverse(): + for value in move_catcher_offset: + move_catcher_offset_reverse.append((n ** 2) - 1 - value) + move_catcher_offset_reverse.reverse() + print(move_catcher_offset) + print(move_catcher_offset_reverse) + + +def move_catcher(): + global catcher_outside, catcher_dir + + # 이동하기d + catcher_dx, catcher_dy = get_catcher_dxy() + catcher[0] += catcher_dx + catcher[1] += catcher_dy + + # 안쪽 바깥쪽 바뀐 지 체크 + is_change_direction = round != 0 and round % ((n ** 2) - 1) == 0 # 24, 48, ... + if is_change_direction: + catcher_outside = not catcher_outside + + # 이동 후 방향 바꾸기 + _round = round % ((n ** 2) - 1) + + if catcher_outside: + if _round in move_catcher_offset: + catcher_dir = (catcher_dir + 1) % 4 + else: + if _round in move_catcher_offset_reverse: + catcher_dir = (catcher_dir + 1) % 4 + + +def get_distance(x1, y1, x2, y2): + return abs(x1 - x2) + abs(y1 - y2) + + +def move_runners(): + for idx, (r_x, r_y, d) in enumerate(runners): + c_x, c_y = catcher + dist = get_distance(r_x, r_y, c_x, c_y) + + if dist > 3: continue + nx = r_x + dx[d] + ny = r_y + dy[d] + if not in_range(nx, ny): + nd = (d + 2) % 4 + nx = r_x + dx[nd] + ny = r_y + dy[nd] + if catcher == [nx, ny]: + runners[idx] = [r_x, r_y, nd] + continue + runners[idx] = [nx, ny, nd] + else: + if catcher == [nx, ny]: continue + runners[idx] = [nx, ny, d] + + +def catch_me_if_you_can(): + global point + + n_cx, n_cy = catcher + remove_runner = [] + + if not tree_board[n_cx][n_cy]: + for idx, (r_x, r_y, d) in enumerate(runners): + if n_cx == r_x and n_cy == r_y: + remove_runner.append([r_x, r_y, d]) + + for i in range(2): + catcher_dx, catcher_dy = get_catcher_dxy() + n_cx += catcher_dx + n_cy += catcher_dy + if not in_range(n_cx, n_cy): + break + if not tree_board[n_cx][n_cy]: + for idx, (r_x, r_y, d) in enumerate(runners): + if n_cx == r_x and n_cy == r_y: + remove_runner.append([r_x, r_y, d]) + + for val in remove_runner: + runners.remove(val) + point += round + + +if __name__ == '__main__': + make_catcher_move() + make_catcher_move_reverse() + + while round <= k: + move_runners() + move_catcher() + catch_me_if_you_can() + round += 1 + + print(point) diff --git "a/src/main/kotlin/heejik/58week/\354\213\270\354\233\200\353\225\205.py" "b/src/main/kotlin/heejik/58week/\354\213\270\354\233\200\353\225\205.py" new file mode 100644 index 00000000..c50fc45a --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\354\213\270\354\233\200\353\225\205.py" @@ -0,0 +1,116 @@ +dx = [-1, 0, 1, 0] +dy = [0, 1, 0, -1] + +n, m, k = 0, 0, 0 +board = [] +pos = [] +direction = [] +man_power = [] +gun_power = [] +point = [] + + +def get_gun(pid): + # 내려 놓는다. + x, y = pos[pid] + if gun_power[pid] != 0: + board[x][y].append(gun_power[pid]) + gun_power[pid] = 0 + # 가장 쎈 총을 줍는다. + if len(board[x][y]) != 0: + best_gun = max(board[x][y]) + gun_power[pid] = best_gun + board[x][y].remove(best_gun) + + +def move(pid): + x, y = pos[pid] + dir = direction[pid] + if x + dx[dir] not in range(n) or y + dy[dir] not in range(n): + direction[pid] = (dir + 2) % 4 + ndir = direction[pid] + nx = x + dx[ndir] + ny = y + dy[ndir] + pos[pid] = [nx, ny] + + is_fight = False + + for pid2, (t_x, t_y) in enumerate(pos): + if pid2 == pid: continue + if t_x == nx and t_y == ny: + # 이동했는데 사람이 있다면 싸움 + fight(pid1=pid, pid2=pid2) + is_fight = True + break + + # 싸우지 않았는데 총이 있으면 획득 + if not is_fight: + get_gun(pid=pid) + + +def fight(pid1, pid2): + # print("pid1:", pid1, "pid2:", pid2) + # print("before fight pos:", pos) + + pid1_power = man_power[pid1] + gun_power[pid1] + pid2_power = man_power[pid2] + gun_power[pid2] + + if pid1_power > pid2_power: + win = pid1 + lose = pid2 + elif pid2_power > pid1_power: + win = pid2 + lose = pid1 + else: + win = pid1 if man_power[pid1] > man_power[pid2] else pid2 + lose = pid2 if man_power[pid1] > man_power[pid2] else pid1 + + point[win] += abs(pid1_power - pid2_power) + + lose_x, lose_y = pos[lose] + # 진 놈 총 내려놓기 + if gun_power[lose] != 0: + board[lose_x][lose_y].append(gun_power[lose]) + gun_power[lose] = 0 + # 진 놈 튀기 + while True: + lose_dir = direction[lose] + nx = lose_x + dx[lose_dir] + ny = lose_y + dy[lose_dir] + # print("lose_dir:", lose_dir) + if nx not in range(n) or ny not in range(n) or [nx, ny] in pos: + direction[lose] = (direction[lose] + 1) % 4 + continue + pos[lose] = [nx, ny] + break + # 진 놈이 이동해서 총 먹기 + get_gun(pid=lose) + + # 이긴 놈은 총 먹기 + get_gun(pid=win) + # print("after fight pos:", pos) + + +def start_game(): + for _ in range(k): + for id in range(m): + move(pid=id) + # print("pos:", pos) + + print(*point) + + +if __name__ == '__main__': + n, m, k = map(int, input().split()) + for _ in range(n): + board.append(list(map(lambda x: [int(x)], input().split()))) + + for i in range(m): + _x, _y, _d, _s = map(int, input().split()) + pos.append([_x - 1, _y - 1]) + direction.append(_d) + man_power.append(_s) + gun_power.append(0) + point.append(0) + + start_game() diff --git "a/src/main/kotlin/heejik/58week/\354\226\221\354\271\230\352\270\260 \352\277\215.py" "b/src/main/kotlin/heejik/58week/\354\226\221\354\271\230\352\270\260 \352\277\215.py" new file mode 100644 index 00000000..c97f38db --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\354\226\221\354\271\230\352\270\260 \352\277\215.py" @@ -0,0 +1,54 @@ +from collections import deque + +n, m = 0, 0 +board = [] +dx = [1, -1, 0, 0] +dy = [0, 0, 1, -1] +wolf_cnt = 0 +sheep_cnt = 0 + + +def solve(): + for i in range(n): + for j in range(m): + if board[i][j] != '#': + bfs(i, j) + + print(sheep_cnt, wolf_cnt) + + +def bfs(sx, sy): + global wolf_cnt, sheep_cnt + + tmp_wolf_cnt = 0 + tmp_sheep_cnt = 0 + if board[sx][sy] == 'v': tmp_wolf_cnt += 1 + if board[sx][sy] == 'k': tmp_sheep_cnt += 1 + board[sx][sy] = '#' + + queue = deque() + queue.append([sx, sy]) + + while queue: + x, y = queue.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if nx not in range(n) or ny not in range(m): continue + if board[nx][ny] == '#': continue + if board[nx][ny] == 'v': tmp_wolf_cnt += 1 + if board[nx][ny] == 'k': tmp_sheep_cnt += 1 + board[nx][ny] = '#' + queue.append([nx, ny]) + + if tmp_wolf_cnt >= tmp_sheep_cnt: + wolf_cnt += tmp_wolf_cnt + else: + sheep_cnt += tmp_sheep_cnt + + +if __name__ == '__main__': + n, m = map(int, input().split()) + for _ in range(n): + board.append(list(input())) + solve() diff --git "a/src/main/kotlin/heejik/58week/\354\230\210\354\210\240\354\204\261.py" "b/src/main/kotlin/heejik/58week/\354\230\210\354\210\240\354\204\261.py" new file mode 100644 index 00000000..edb6a37d --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\354\230\210\354\210\240\354\204\261.py" @@ -0,0 +1,128 @@ +from collections import deque + +n = 0 +total_score = 0 +board = [] + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + +group_count = [0] * ((29 * 29) + 1) + + +# 그룹을 만든다 +# 각 그룹 내부 칸의 숫자를 센다 +# 해당 그룹으로 상하좌우를 비교한다. + +def solve(): + get_score() + + for _ in range(3): + rotate() + get_score() + + print(total_score) + + +def rotate(): + middle = n // 2 + + # 세로 뽑아내기 + vertical = [board[x][middle] for x in range(n)] + # 가로 뽑아내기 + horizontal = board[middle] + + horizontal = reverse_line(horizontal) + + # 가로로 넣기 + board[middle] = vertical + # 세로로 넣기 + for x in range(n): + board[x][middle] = horizontal[x] + + for x in range(0, n, middle + 1): + for y in range(0, n, middle + 1): + rotate_rect(x, y, x + middle - 1, y + middle - 1) + + +# 1: 왼쪽 위, 2: 오른쪽 아래 +def rotate_rect(x1, y1, x2, y2): + rect_size = n // 2 + slice_rect = [[0] * rect_size for _ in range(rect_size)] + + # 잘라진 사각형 + for x in range(x1, x2 + 1): + for y in range(y1, y2 + 1): + slice_rect[x - x1][y - y1] = board[x][y] + + rotated_rect = [[0] * rect_size for _ in range(rect_size)] + # 회전 + for x in range(rect_size): + for y in range(rect_size): + rotated_rect[y][rect_size - x - 1] = slice_rect[x][y] + + # 기존 보드에 합치기 + for x in range(x1, x2 + 1): + for y in range(y1, y2 + 1): + board[x][y] = rotated_rect[x - x1][y - y1] + + +def reverse_line(line): + return line[::-1] + + +def get_score(): + score = 0 + global total_score, group_count + + group_count = list(map(lambda x: 0, group_count)) + make_group() + + for x in range(n): + for y in range(n): + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if nx not in range(n) or ny not in range(n): continue + if board[x][y] == board[nx][ny]: continue + score += (group_count[group[x][y]] + group_count[group[nx][ny]]) * board[x][y] * board[nx][ny] + total_score += score // 2 + + +def make_group(): + visited = [[False] * n for _ in range(n)] + queue = deque() + group_number = 0 + + for i in range(n): + for j in range(n): + if visited[i][j]: continue + group_number += 1 + queue.append([i, j]) + visited[i][j] = True + group[i][j] = group_number + group_count[group_number] += 1 + + while queue: + x, y = queue.popleft() + for d in range(4): + nx = x + dx[d] + ny = y + dy[d] + if nx not in range(n) or ny not in range(n): + continue + if visited[nx][ny]: + continue + if board[x][y] == board[nx][ny]: + queue.append([nx, ny]) + visited[nx][ny] = True + group[nx][ny] = group_number + group_count[group_number] += 1 + + +if __name__ == '__main__': + n = int(input()) + for _ in range(n): + board.append(list(map(int, input().split()))) + + group = [[0] * n for _ in range(n)] + solve() diff --git "a/src/main/kotlin/heejik/58week/\354\240\225\354\234\241\353\251\264\354\262\264 \355\225\234\353\262\210 \353\215\224 \352\265\264\353\246\254\352\270\260.py" "b/src/main/kotlin/heejik/58week/\354\240\225\354\234\241\353\251\264\354\262\264 \355\225\234\353\262\210 \353\215\224 \352\265\264\353\246\254\352\270\260.py" new file mode 100644 index 00000000..e2e8a7aa --- /dev/null +++ "b/src/main/kotlin/heejik/58week/\354\240\225\354\234\241\353\251\264\354\262\264 \355\225\234\353\262\210 \353\215\224 \352\265\264\353\246\254\352\270\260.py" @@ -0,0 +1,109 @@ +from collections import deque + +score = 0 + +# 위, 앞, 옆 +dice_number = [1, 2, 3] +dice_direction = 0 +dice_pos = [0, 0] + +dx = [0, 1, 0, -1] +dy = [1, 0, -1, 0] + + +def in_range(x, y): + return x in range(n) and y in range(n) + + +def change_dice_number(direction): + global dice_number + up, front, side = dice_number + # 오른쪽 + if direction == 0: + n_up, n_front, n_side = 7 - side, front, up + # 왼쪽 + elif direction == 2: + n_up, n_front, n_side = side, front, 7 - up + # 위쪽 + elif direction == 3: + n_up, n_front, n_side = front, 7 - up, side + # 아래쪽 + else: + n_up, n_front, n_side = 7 - front, up, side + + dice_number = [n_up, n_front, n_side] + + +def move_dice(): + global dice_direction, dice_pos + d_x, d_y = dice_pos + nx = d_x + dx[dice_direction] + ny = d_y + dy[dice_direction] + + # 밖을 벗어나는 경우 + if not in_range(nx, ny): + dice_direction = (dice_direction + 2) % 4 + nx = d_x + dx[dice_direction] + ny = d_y + dy[dice_direction] + + change_dice_number(dice_direction) + dice_pos = [nx, ny] + + +def rotate_dice(): + global dice_direction + # 아래 번호 + down_number = abs(7 - dice_number[0]) + + d_x, d_y = dice_pos + board_number = board[d_x][d_y] + # 더 크면 90도 시계 회전 + if down_number > board_number: + dice_direction = (dice_direction + 1) % 4 + # 더 작으면 90도 반시계 회전 + elif down_number < board_number: + dice_direction = (dice_direction - 1) % 4 + # 같으면 그대로 + else: + pass + + +# 현재 다이스의 위치의 보드 숫자에서 bfs 로 상하좌우로 같은 숫자면 점수 합 +def get_score(): + global score + d_x, d_y = dice_pos + board_number = board[d_x][d_y] + + score += board_number + visited = [[False] * n for _ in range(n)] + visited[d_x][d_y] = True + queue = deque() + queue.append([d_x, d_y]) + + while queue: + x, y = queue.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if not in_range(nx, ny): continue + if board[nx][ny] != board_number: continue + if visited[nx][ny]: continue + queue.append([nx, ny]) + visited[nx][ny] = True + score += board_number + + +def roll_dice(): + move_dice() + get_score() + rotate_dice() + + +if __name__ == '__main__': + n, m = map(int, input().split()) + board = [list(map(int, input().split())) for _ in range(n)] + + for _ in range(m): + roll_dice() + + print(score) From ed7e24ee75d2b2b154592774e0507a5ce193f096 Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Sat, 20 Apr 2024 22:39:25 +0900 Subject: [PATCH 3/5] =?UTF-8?q?solve:=20=EB=8F=99=EC=A0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../59week/\353\217\231\354\240\204.kt" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "src/main/kotlin/heejik/59week/\353\217\231\354\240\204.kt" diff --git "a/src/main/kotlin/heejik/59week/\353\217\231\354\240\204.kt" "b/src/main/kotlin/heejik/59week/\353\217\231\354\240\204.kt" new file mode 100644 index 00000000..92a09c4a --- /dev/null +++ "b/src/main/kotlin/heejik/59week/\353\217\231\354\240\204.kt" @@ -0,0 +1,40 @@ +package heejik.`59week` + +class 동전 { + + fun solve() { + val t = readln().toInt() + repeat(t) { + val n = readln().toInt() + val coins = readln().split(' ').map { it.toInt() } + val price = readln().toInt() + val dp = MutableList(n + 1) { MutableList(10001) { 0 } } + + for (i in 0 until n) { + dp[i][0] = 1 + } + + for (i in 0 until n) { + for (j in 1..price) { + if (i == 0) { + if (j - coins[0] >= 0) { + dp[0][j] = dp[0][j - coins[0]] + } + } else { + if (j - coins[i] >= 0) { + dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i]] + } else { + dp[i][j] = dp[i - 1][j] + } + } + } + } + println(dp[n-1][price]) + } + } +} + + +fun main() { + 동전().solve() +} \ No newline at end of file From 1e4b44f34351835fb00924418ff8d3f453c113c0 Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Sat, 20 Apr 2024 22:39:32 +0900 Subject: [PATCH 4/5] =?UTF-8?q?solve:=20=EB=A9=80=EC=A9=A1=ED=95=9C=20?= =?UTF-8?q?=EC=82=AC=EA=B0=81=ED=98=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\202\254\352\262\251\355\230\225.kt" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "src/main/kotlin/heejik/59week/\353\251\200\354\251\241\355\225\234 \354\202\254\352\262\251\355\230\225.kt" diff --git "a/src/main/kotlin/heejik/59week/\353\251\200\354\251\241\355\225\234 \354\202\254\352\262\251\355\230\225.kt" "b/src/main/kotlin/heejik/59week/\353\251\200\354\251\241\355\225\234 \354\202\254\352\262\251\355\230\225.kt" new file mode 100644 index 00000000..cad79782 --- /dev/null +++ "b/src/main/kotlin/heejik/59week/\353\251\200\354\251\241\355\225\234 \354\202\254\352\262\251\355\230\225.kt" @@ -0,0 +1,26 @@ +package heejik.`59week` + +class Solution { + fun solution(w: Int, h: Int): Long { + val gcd = getGcd(w, h) + + val smallW: Long = (w.toLong() / gcd) + val smallH: Long = (h.toLong() / gcd) + + val liveRectCnt: Long = (smallH - 1) * (smallW - 1) + val removeRectCnt: Long = (smallH * smallW) - liveRectCnt + + return (w.toLong() * h.toLong()) - (removeRectCnt * gcd) + } + + private fun getGcd(a: Int, b: Int): Int { + if (b == 0) return a + return getGcd(b, a % b) + } +} + +fun main() { + Solution().solution(100000000, 99999999).also { + println(it) + } +} \ No newline at end of file From fc8215d02b639ba5d53acefefc441ae9e0560005 Mon Sep 17 00:00:00 2001 From: jhg3410 Date: Sun, 21 Apr 2024 13:55:52 +0900 Subject: [PATCH 5/5] =?UTF-8?q?solve:=20=ED=98=B8=ED=85=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../59week/\355\230\270\355\205\224.kt" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "src/main/kotlin/heejik/59week/\355\230\270\355\205\224.kt" diff --git "a/src/main/kotlin/heejik/59week/\355\230\270\355\205\224.kt" "b/src/main/kotlin/heejik/59week/\355\230\270\355\205\224.kt" new file mode 100644 index 00000000..c0e066d8 --- /dev/null +++ "b/src/main/kotlin/heejik/59week/\355\230\270\355\205\224.kt" @@ -0,0 +1,40 @@ +package heejik.`59week` + +import kotlin.math.max + +class 호텔 { + + fun solve() { + val (c, n) = readln().split(' ').map { it.toInt() } + val dp = List(n) { MutableList(size = 100001) { 0 } } + val prices = mutableListOf() + val customers = mutableListOf() + repeat(n) { idx -> + val (price, customer) = readln().split(' ').map { it.toInt() } + dp[idx][price] = customer + prices.add(price) + customers.add(customer) + } + for (i in 0 until n) { + for (j in 1 until 100001) { + if (i == 0) { + if (j - prices[i] >= 0) { + dp[i][j] = dp[i][j - prices[i]] + customers[i] + } + } else { + if (j - prices[i] >= 0) { + dp[i][j] = max(dp[i - 1][j], dp[i][j - prices[i]] + customers[i]) + } else { + dp[i][j] = max(dp[i - 1][j], dp[i][j]) + } + } + } + } + + print(dp[n - 1].indexOfFirst { it >= c }) + } +} + +fun main() { + 호텔().solve() +} \ No newline at end of file