Skip to content

Commit

Permalink
Merge pull request #243 from wellFoundedDevelopers/heejik/58week
Browse files Browse the repository at this point in the history
[장희직] - 양치기 꿍, 포탑 부수기, 토끼와 경주, 술래잡기
  • Loading branch information
jhg3410 authored Apr 21, 2024
2 parents 2dbcc03 + fc8215d commit fb51d07
Show file tree
Hide file tree
Showing 16 changed files with 1,432 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/main/kotlin/heejik/58week/고대 문명 유적 탐사.py
Original file line number Diff line number Diff line change
@@ -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()
51 changes: 51 additions & 0 deletions src/main/kotlin/heejik/58week/괄호의 값.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class `괄호의 값`() {
fun solve() {
val input = readln()
val stack = ArrayDeque<Char>()
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()
}
142 changes: 142 additions & 0 deletions src/main/kotlin/heejik/58week/꼬리잡기 놀이.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit fb51d07

Please sign in to comment.