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

[이지민] - 항체 인식, 토마토, 우체국, 문자열 게임 2 #224

Merged
merged 4 commits into from
Mar 16, 2024
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
35 changes: 35 additions & 0 deletions src/main/kotlin/jimin/54week/문자열 게임 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
[문자열 게임 2](https://www.acmicpc.net/problem/20437)
알파벳 배열을 만들어서 문자열 W의 각 문자 위치를 담았다.
이를 이용해서 해당 문자를 K개 포함할 때 최소와 최대를 업데이트 해주었다.

'''


import sys

t = int(sys.stdin.readline())

for i in range(t):
w = sys.stdin.readline()
k = int(sys.stdin.readline())

alphabet = [[] for _ in range(26)]

for i in range(len(w) - 1):
alphabet[ord(w[i]) - ord('a')].append(i)

mini = 10_001
Copy link
Contributor

Choose a reason for hiding this comment

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

지미니미니 ㅎㅎ

maxi = -1

for i in range(26):
if len(alphabet[i]) >= k:
for j in range(0, len(alphabet[i]) - k + 1):
mini = min(mini, alphabet[i][j + k - 1] - alphabet[i][j] + 1)
maxi = max(maxi, alphabet[i][j + k - 1] - alphabet[i][j] + 1)
Comment on lines +25 to +29
Copy link
Member

Choose a reason for hiding this comment

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

와 깔끔하군요,,,


if maxi == -1:
print(-1)
else:
print(mini, maxi)

33 changes: 33 additions & 0 deletions src/main/kotlin/jimin/54week/우체국.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
[우체국](https://www.acmicpc.net/problem/2141)

https://tussle.tistory.com/1050 블로그 참고
왼쪽 오른쪽 사람 수가 균일한 위치를 찾는다.
이를 위해서 입력받을때 전체 사람수 total을 만들고,
마을을 순차탐색하면서 왼쪽과 오른쪽 사람의 차이가 가장 적은 위치를 구한다.

'''

import sys

n = int(sys.stdin.readline())
town = []
total = 0
for i in range(n):
x, a = map(int, sys.stdin.readline().split())
total += a
town.append([x, a])

town.sort(key= lambda x: x[0])

people = 0
mini = 1_000_000_000 * 100_000
result = -1
for i in range(n):
if abs(people - (total - people - town[i][1])) < mini:
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 제 식이 뭔지 이해가 안갔었는데 지민님 설명 듣고 이해가 됐습니다 ㅎㅎ

mini = abs(people - (total - people - town[i][1]))
Comment on lines +27 to +28
Copy link
Member

Choose a reason for hiding this comment

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

저랑 구현을 다르게 하셔서 좋았습니다!

result = town[i][0]

people += town[i][1]
Comment on lines +26 to +31
Copy link
Member

Choose a reason for hiding this comment

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

직접 푸신게,,, 대단하십니다.


print(result)
43 changes: 43 additions & 0 deletions src/main/kotlin/jimin/54week/토마토.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'''
[토마토](https://www.acmicpc.net/problem/7576)

bfs를 돌면서 토마토를 익혔으며, 이때 날짜를 업데이트 해주었다.
'''

import sys
from collections import deque

m, n = map(int, sys.stdin.readline().split())
tomato = []

for i in range(n):
tomato.append(list(map(int, sys.stdin.readline().split())))

queue = deque([])
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

for i in range(n):
for j in range(m):
if tomato[i][j] == 1:
queue.append([i, j])

while queue:
nx, ny = queue.popleft()

for i in range(4):
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m:
if tomato[nx + dx[i]][ny + dy[i]] == 0:
queue.append([nx + dx[i], ny + dy[i]])
tomato[nx + dx[i]][ny + dy[i]] = tomato[nx][ny] + 1
Comment on lines +28 to +32
Copy link
Member

Choose a reason for hiding this comment

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

진짜 깔끔하네요


result = 0
for i in range(n):
for j in range(m):
if tomato[i][j] == 0:
print("-1")
sys.exit()
else:
result = max(result, tomato[i][j])

print(result - 1)
69 changes: 69 additions & 0 deletions src/main/kotlin/jimin/54week/항체 인식.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'''
[항체 인식](https://www.acmicpc.net/problem/22352)

bfs를 만들어서 before에 맞춰서 after를 같이 탐색한다.
이때 after의 수가 일관되지 않으면 -1을 반환하고,
before와 after가 동일하면 0을 반환하고,
before와 after가 다르면 1을 반환한다.

-1은 백신 조건을 위배한 것으로 바로 No를 출력하고,
0과 1은 계속 더해주면서 항체가 아예 변하지 않거나 한번만 변할 경우만 YES를 출력한다.
'''


import sys
from collections import deque


n, m = map(int, sys.stdin.readline().split())
before = []
after = []
visited = [[False for _ in range(m)] for _ in range(n)]

for i in range(n):
before.append(list(map(int, sys.stdin.readline().split())))

for i in range(n):
after.append(list(map(int, sys.stdin.readline().split())))


def bfs(x, y):
queue = deque([[x, y]])
data = before[x][y]
update = after[x][y]
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
possible = 0

if data != update:
possible = 1

while queue:
nx, ny = queue.popleft()
visited[nx][ny] = True
if after[nx][ny] != update:
possible = -1
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

헉..큐에 넣기 전에 before가 탐색중인 값인지도 조건에 추가하면 after가 다를때 바로 NO처리가 가능하군요...예외처리 댑악


for i in range(4):
if 0 <= nx + dx[i] < n and 0 <= ny + dy[i] < m:
if not visited[nx + dx[i]][ny + dy[i]] and before[nx + dx[i]][ny + dy[i]] == data:
queue.append([nx + dx[i], ny + dy[i]])

return possible


result = 0
for i in range(n):
for j in range(m):
if not visited[i][j]:
possible = bfs(i, j)
if possible == -1:
print("NO")
sys.exit()
else:
result += bfs(i, j)

if result <= 1 :
print("YES")
Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

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

resturn 된 possible 의 합으로 처리하는 부분이 좋았습니다

else:
print("NO")