Skip to content
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

[이지민] - A와 B 2, 알고스팟, 회장뽑기, 입국심사 #189

Merged
merged 4 commits into from
Oct 8, 2023
Merged
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
31 changes: 31 additions & 0 deletions src/main/kotlin/jimin/47week/A와 B 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
A와 B 문제를 풀고 아이디어를 얻음
(그냥 완탐시 시간초과)
S -> T 이니까 반대로 T를 S로 만들도록 함
뒤가 'A'면 그냥 맨 끝을 자르고
앞이 'B'이면 앞을 하나 자르고 뒤집는다!
'''

import sys
sys.setrecursionlimit(10**6)
a = list(sys.stdin.readline().rstrip())
b = list(sys.stdin.readline().rstrip())
result = 0


def set_recursion(b):
global result, a
if len(a) == len(b):
if a == b:
result = 1
return

if b[-1] == 'A':
set_recursion(b[:-1])

if b[0] == 'B':
set_recursion(b[:0:-1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와 부럽다 파이썬...



set_recursion(b)
print(result)
32 changes: 32 additions & 0 deletions src/main/kotlin/jimin/47week/알고스팟.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
0-1 너비 우선 탐색 이용!
'''

import sys
from collections import deque
m, n = map(int, sys.stdin.readline().split())
miros = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(n)]

queue = deque([[0, 0, 0]])
visited = [[0 for _ in range(m)] for _ in range(n)]
visited[0][0] = 1

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
result = -1
while queue:
nx, ny, num = queue.popleft()
if nx == n - 1 and ny == m - 1:
result = num
break

for i in range(4):
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m and not visited[nx + dx[i]][ny + dy[i]]:
new = miros[nx + dx[i]][ny + dy[i]]
if new == 0:
queue.appendleft([nx + dx[i], ny + dy[i], num])
else:
queue.append([nx + dx[i], ny + dy[i], num + 1])
visited[nx + dx[i]][ny + dy[i]] = 1

print(result)
20 changes: 20 additions & 0 deletions src/main/kotlin/jimin/47week/입국심사.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sys
def solution(n, times):
answer = sys.maxsize

front = 1
last = 1_000_000_000 * 1_000_000_000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬은 이런게 부럽네요...


while front <= last:
mid = (front + last) // 2
num = 0
for t in times:
num += mid // t
if num < n:
front = mid + 1
else:
last = mid - 1
answer = mid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answer 를 여기서 최신화 시켜서 리턴 시키는 게 더 잘 읽히네요!



return answer
41 changes: 41 additions & 0 deletions src/main/kotlin/jimin/47week/회장뽑기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''
bfs를 활용해서 문제를 해결했다
'''

import sys
from collections import deque

n = int(sys.stdin.readline())
friends = [[] for _ in range(n + 1)]
info = [0 for _ in range(n + 1)]
while True:
a, b = map(int, sys.stdin.readline().split())
if a == -1 and b == -1:
break
friends[a].append(b)
friends[b].append(a)

def bfs(target):
global n, info
queue = deque([[target, 0]])
visited = [0 for _ in range(n + 1)]

while queue:
now, num = queue.popleft()
for i in friends[now]:
if i != target and not visited[i]:
queue.append([i, num + 1])
visited[i] = num + 1

return num
Comment on lines +18 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엄청 깔끔하게 로직 잘 짜신 것 같아요..!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 점수를 이렇게 한 번에 구하신 게 좋네요!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num이 리턴되니 로직이 훨씬 깔끔한 것 같아요 멋집니당



for i in range(1, n + 1):
info[i] = bfs(i)

mini = min(info[1:])
print(mini, info.count(mini))

for i in range(1, n + 1):
if info[i] == mini:
print(i, end=" ")