-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from IJS1016/main
[23.3.4W]Python PS
- Loading branch information
Showing
5 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,111 @@ | ||
# https://www.acmicpc.net/problem/14500 | ||
# 하나만 놔서 하는건 껌이지 | ||
|
||
# 블록 종류 별로 만드는거, 회전, 대칭 구현하기 | ||
|
||
# 우선 shift 시키면서 최대값 찾는거 어떤 방법으로 할건지 생각 | ||
# 구현을 어떻게 할 것인지 | ||
|
||
# 2시까지 정리해서 풀기 | ||
# 기준점을 두고, 거기서 +- 쉬프트해서 도형 위치를 쓰기 | ||
# 예를 들어 [0, 0] [1, 0] [2, 0], [2, 1] | ||
# 회전은 [x, y] -> [y, n-x] 이 식이었나 시켜서, shift 시키면 되지, 회전하면 정렬이 필요함(0, 0)으로 맞추기 위해서 어짜치 shift할거면 n은 나중에 해줘도 되잖아 | ||
# 종류별로 싹 돌면서 max 값 저장해서 print하면 됨 | ||
|
||
tets = [[[0, 0], [0, 1], [0, 2], [0, 3]], [[0, 0], [0, 1], [1, 0], [1, 1]], [[0, 0], [1, 0], [2, 0], [2, 1]], | ||
[[0, 0], [1, 0], [1, 1], [2, 1]], [[0,0], [0, 1], [0, 2], [1, 1]]] | ||
|
||
def rotate_tet(tet) : | ||
result_tet = [tet] | ||
shiftted_rotated_tet = tet | ||
|
||
for _ in range(4) : # rotate 4 times | ||
min_x, min_y = 10, 10 | ||
|
||
rotated_tet= [] | ||
for polio in shiftted_rotated_tet : | ||
y, x = polio | ||
rotated_tet.append([x, -y]) | ||
min_y = x if min_y > x else min_y | ||
min_x = -y if min_x > -y else min_x | ||
|
||
shiftted_rotated_tet = [] | ||
for rt in rotated_tet : | ||
shiftted_rotated_tet.append([rt[0]-min_y, rt[1]-min_x]) | ||
shiftted_rotated_tet.sort(key=lambda x :(x[0], x[1])) | ||
|
||
if shiftted_rotated_tet not in result_tet : | ||
result_tet.append(shiftted_rotated_tet) | ||
return result_tet | ||
|
||
# 반전 하나 라인만 -로 수정 | ||
def flip_n_rotate_tet(tet) : | ||
result_tet = rotate_tet(tet) | ||
|
||
flip_tet = [] | ||
min_x = 10 | ||
|
||
for p in tet : | ||
flip_tet.append([p[0], -p[1]]) | ||
min_x = -p[1] if min_x > -p[1] else min_x | ||
|
||
shifted_flip_tet = [] | ||
for fp in flip_tet : | ||
shifted_flip_tet.append([fp[0], fp[1]-min_x]) | ||
shifted_flip_tet.sort(key=lambda x : (x[0],x[1])) | ||
|
||
if shifted_flip_tet not in result_tet : | ||
result_tet.extend(rotate_tet(shifted_flip_tet)) | ||
|
||
return result_tet | ||
|
||
def caculate_score(y, x) : | ||
max_score = 0 | ||
# print_flag = False | ||
# # if y == 0 and x == 4 : | ||
# # print_flag = True | ||
|
||
for tet in all_tets : | ||
# if print_flag : | ||
# print(tet) | ||
score = 0 | ||
for p in tet : | ||
ny = y + p[0] | ||
nx = x + p[1] | ||
if not ((0 <= ny < N) and (0 <= nx < M)) : | ||
score = 0 | ||
break | ||
score += mmap[ny][nx] | ||
# if print_flag : | ||
# print(score) | ||
# print() | ||
if max_score < score : | ||
max_score = score | ||
|
||
return max_score | ||
|
||
all_tets = [] | ||
for tet in tets: | ||
# print(tet) | ||
flip_rot_tets = flip_n_rotate_tet(tet) | ||
# print("rot") | ||
# for r in flip_rot_tets : | ||
# print(r) | ||
# print() | ||
all_tets.extend(flip_rot_tets) | ||
|
||
# print(len(all_tets)) | ||
|
||
N, M = map(int, input().split(" ")) | ||
mmap = [] | ||
for _ in range(N) : | ||
mmap.append(list(map(int, input().split(" ")))) | ||
|
||
result = 0 | ||
for yi in range(N) : | ||
for xi in range(M) : | ||
score = caculate_score(yi, xi) | ||
if result < score : | ||
result = score | ||
|
||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 문제 제대로 읽기, 숫자 위치는 바뀌면 안되는거... | ||
# for 문 위치 잘 생각하기 | ||
# 순열 -> 재귀 사용, append, pop 함수 argument로 주면 안됨 | ||
# 순열의 경우 완전탐색으로 시간 초과 날 수 있음 | ||
# 백트래킹? 순열? 뭔차인거지? | ||
# deepcopy 대신 [:] 사용하기 | ||
# 10억 1000000000 1포함 10 | ||
N = int(input()) | ||
nums = list(map(int, input().split(" "))) | ||
opers = list(map(int, input().split(" "))) # +, -, *, / | ||
|
||
def change_opers_format(opers) : | ||
op_order = ["+", "-", "*", "/"] | ||
new_opers = [] | ||
for i, op in enumerate(opers) : | ||
new_opers.extend([op_order[i]] * op) | ||
return new_opers | ||
|
||
def get_permutation(nums, tmp, len_nums) : | ||
if len(tmp) == len_nums: | ||
permutations.append(tmp) | ||
return | ||
|
||
for i, n in enumerate(nums) : | ||
nnums = nums[:] | ||
ntmp = tmp[:] | ||
nnums.pop(i) | ||
ntmp.append(n) | ||
get_permutation(nnums, ntmp, len_nums) | ||
|
||
def get_permutation_without_duplication(mylist) : | ||
global permutations | ||
permutations = [] | ||
get_permutation(mylist, [], len(mylist)) | ||
permutations = list(set(map(tuple, permutations))) | ||
return permutations[:] | ||
|
||
def calcuate(x1, x2, op) : | ||
# / 시 int 형인지, 음수 / 잘 되는지 확인 필요 | ||
if op == '+' : return x1 + x2 | ||
elif op == '-' : return x1 - x2 | ||
elif op == '*' : return x1 * x2 | ||
elif op == '/' : return int(x1 / x2) | ||
|
||
def get_result(num, op_perms) : | ||
min_v = 1000000000 | ||
max_v = -1000000000 | ||
for op in op_perms : | ||
tmp = int(num[0]) | ||
for idx in range(1, N) : | ||
tmp = calcuate(tmp, int(num[idx]), op[idx-1]) | ||
|
||
if min_v > tmp : | ||
min_v = tmp | ||
if max_v < tmp : | ||
# print(f"{dbg_string} = {tmp}") | ||
max_v = tmp | ||
# print(min_v, max_v) | ||
return min_v, max_v | ||
|
||
opers = change_opers_format(opers) | ||
op_perms = get_permutation_without_duplication(opers) | ||
min_v, max_v = get_result(nums, op_perms) | ||
print(max_v) | ||
print(min_v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 다시 풀어보기 | ||
def dfs(depth, index) : | ||
global min_diff | ||
if depth == N//2 : | ||
power1, power2 = 0, 0 | ||
for i in range(N) : | ||
for j in range(N) : | ||
if visited[i] and visited[j] : | ||
power1 += power_map[i][j] | ||
elif not visited[i] and not visited[j] : | ||
power2 += power_map[i][j] | ||
diff_power = abs(power1 - power2) | ||
if min_diff > diff_power : | ||
min_diff = diff_power | ||
else : | ||
for i in range(index, N) : | ||
if not visited[i] : | ||
visited[i] = True | ||
dfs(depth+1, i+1) | ||
visited[i] = False | ||
|
||
N = int(input()) | ||
power_map = [] | ||
for _ in range(N) : | ||
power_map.append(list(map(int, input().split(" ")))) | ||
|
||
min_diff = 1e9 | ||
persons = [x for x in range(N)] | ||
visited = [False for _ in range(N)] | ||
|
||
dfs(0, 0) | ||
print(min_diff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# list transpose : list transpose [list(x) for x in zip(*mmap)] | ||
# zip은 같은 index 요소들을 묶기 때문에, tuple unpacking 후 인덱스 별로 묶어줌 | ||
# Edge 케이스 잘 생각하기.. | ||
# range(N)에서 N는 포함되지 않는 것 주의 | ||
# 문제 완벽히 이해가 필요, 예시 기반으로 먼저 머리로 생각해서 원하는 답이 나오는지 확인 후 문제풀기 | ||
# for문 범위 주의하기 | ||
|
||
# 재귀 - 56ms ###################################################################### | ||
# 문제 이해를 잘못해서, 높은 곳에도 경사로를 둘 수 있다고 생각하여 재귀로 작성 | ||
# 재귀로 작성하지 않아도 풀이 가능 | ||
# def check_possible_road(vector, idx, put_ramp) : | ||
# global vector_result | ||
# if idx == N : | ||
# vector_result = True | ||
# return | ||
# diff = vector[idx] - vector[idx-1] | ||
# # print(idx, put_ramp, diff) | ||
# if abs(diff) > 1 : | ||
# return | ||
# elif diff == 1 : # 뒤에 있는 값이 1 크면, 앞에 경사로 | ||
# if idx-L >= 0 : | ||
# for i in range(idx-L, idx) : | ||
# if i in put_ramp : | ||
# return | ||
# # print(f"FRONT {[idx-L, idx-1]}") | ||
# check_possible_road(vector, idx+1, put_ramp+[x for x in range(idx-L, idx)]) | ||
|
||
# elif diff == -1 : # 뒤에 있는 값이 1 작으면, 뒤에 경사로 | ||
# if idx+L-1 < N : | ||
# be = vector[idx] | ||
# for i in range(idx+1, idx+L) : | ||
# if be != vector[i] : | ||
# return | ||
# be = vector[i] | ||
# # print(f"BACK {[idx, idx+L-1]}") | ||
# check_possible_road(vector, idx+L, put_ramp+[x for x in range(idx, idx+L)]) | ||
# else : | ||
# check_possible_road(vector, idx+1, put_ramp) | ||
# N, L = map(int, input().split(" ")) | ||
# mmap = [] | ||
# for _ in range(N) : | ||
# mmap.append(list(map(int, input().split(" "))) | ||
# ) | ||
# result = 0 | ||
# for m in [mmap, zip(*mmap)] : | ||
# for vector in m : | ||
# vector_result = False | ||
# check_possible_road(vector, 1, []) | ||
# result += vector_result | ||
# # if vector_result : | ||
# # print(vector) | ||
# print(result) | ||
###################################################################### | ||
|
||
# 재귀 X - 48ms ###################################################################### | ||
# 재귀 아닐 떄 종료 조건 잘 넣어주기 | ||
def check_possible_road(vector) : | ||
visited = [False] * len(vector) | ||
for i in range(1, len(vector)) : | ||
# print(i) | ||
# print(visited) | ||
diff = vector[i] - vector[i-1] | ||
if abs(diff) > 1 : | ||
return False | ||
if diff == 1 : # 뒤에 있는 값이 1 크면, 앞에 경사로 | ||
if i-L >= 0 : | ||
for j in range(i-L, i) : | ||
if visited[j] : | ||
return False | ||
visited[j] = True | ||
else : | ||
return False | ||
if diff == -1 : # 뒤에 있는 값이 1 작으면, 뒤에 경사로 | ||
if i+L-1 < N : | ||
be = vector[i] | ||
for j in range(i+1, i+L) : | ||
if be != vector[j] : | ||
return False | ||
be = vector[j] | ||
for j in range(i, i+L) : | ||
visited[j] = True | ||
else : | ||
return False | ||
return True | ||
# FOR DBG ############################################ | ||
# N, L = 6, 2 | ||
# mmap = [[3, 3, 3, 3, 3, 3], | ||
# [2, 3, 3, 3, 3, 3], | ||
# [2, 2, 2, 3, 2, 3], | ||
# [1, 1, 1, 2, 2, 2], | ||
# [1, 1, 1, 3, 3, 1], | ||
# [1, 1, 2, 3, 3, 2]] | ||
# N, L = 6, 1 | ||
# mmap = [[3, 2, 2, 1, 2, 3]] | ||
# result = 0 | ||
# for row in mmap : | ||
# print(row) | ||
# vector_result = False | ||
# check_possible_road(row, 1, row[0], []) | ||
# print(vector_result) | ||
####################################################### | ||
|
||
N, L = map(int, input().split(" ")) | ||
mmap = [] | ||
for _ in range(N) : | ||
mmap.append(list(map(int, input().split(" "))) | ||
) | ||
result = 0 | ||
for m in [mmap, zip(*mmap)] : | ||
for vector in m : | ||
# print() | ||
# if check_possible_road(vector) : | ||
# print("SUCCESS") | ||
# print(vector) | ||
result += check_possible_road(vector) | ||
# if check_possible_road(vector) : | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
def dfs(depth, idx) : | ||
global n | ||
if depth == 6 : | ||
for i in range(n) : | ||
if visited[i] : | ||
print(nums[i], end=" ") | ||
print() | ||
else : | ||
for i in range(idx, n) : | ||
visited[i] = True | ||
dfs(depth+1, i+1) | ||
visited[i] = False | ||
|
||
# DGB | ||
# nums = [1, 2, 3, 4, 5, 6, 7] | ||
# nums.sort() | ||
# n = len(nums) | ||
# visited = [False for _ in range(n)] | ||
# dfs(0, 0) | ||
|
||
n = 1 | ||
while n != 0 : | ||
nums = list(map(int, input().split(" "))) | ||
n, nums = nums[0], nums[1:] | ||
visited = [False for _ in range(n)] | ||
dfs(0, 0) | ||
print() |