-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-29] wooyeol #271 #295
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
문자열 지옥에 빠진 호석 | ||
https://www.acmicpc.net/problem/20166 | ||
|
||
풀이시간 | ||
12:42 ~ (문제 풀이 실패) | ||
|
||
문제 조건 | ||
3 <= N,M <= 10 | ||
1 <= K <= 1,000 | ||
1 <= 신이 좋아하는 문자열 <= 5 | ||
|
||
시간 복잡도 : | ||
O(N * M * 8^5 + K) | ||
O(3,277,800) | ||
|
||
접근법 | ||
무슨 알고리즘으로 풀이 할 수 있을까? -> BFS 탐색 | ||
|
||
1. 만들어질 수 있는 모든 문자열을 탐색하여 해시테이블에 등록 | ||
2. 등록된 해시 테이블의 값에서 입력받은 문자열의 값을 반환 | ||
""" | ||
import sys | ||
from collections import deque | ||
from collections import defaultdict | ||
|
||
input = sys.stdin.readline | ||
|
||
# 상, 하, 좌, 우, 대각선 왼쪽 위, 대각선 오른쪽 위, 대각선 왼쪽 아래, 대각선 오른쪽 아래 | ||
directions = ((-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)) | ||
|
||
def bfs(x, y): | ||
queue = deque() | ||
queue.append([x, y, table[x][y]]) | ||
|
||
while queue: | ||
x, y, text = queue.popleft() | ||
|
||
# 지금까지 확인된 문자열의 갯수 증가 | ||
answer_dict[text] += 1 | ||
|
||
# 문자열의 길이가 5 이상이라면 더 이상 검색하지 않는다. | ||
if len(text) >= 5: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 큐에 삽입할 때(52번 라인) 이 조건문을 추가해주면 연산 횟수도 줄고 조금 더 깔끔해지지 않을까 싶습니다! |
||
continue | ||
|
||
# BFS 탐색 | ||
for dx, dy in directions: | ||
# nx,ny의 환형 구조를 반영 | ||
nx, ny = (x + dx) % N, (y + dy) % M | ||
|
||
# 다음 탐색할 노드 추가 | ||
queue.append((nx, ny, text + table[nx][ny])) | ||
|
||
N, M, K = map(int, input().split()) | ||
|
||
# N by M 의 격자 | ||
table = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별 다른 추가 연산이 없기 때문에 57 ~ 59번 라인을 |
||
for _ in range(N): | ||
table.append(input().rstrip()) | ||
|
||
# 격자에서 가능한 모든 문자열 갯수 탐색 | ||
answer_dict = defaultdict(int) | ||
for row_i, row in enumerate(table): | ||
for col_i, col in enumerate(row): | ||
bfs(row_i, col_i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우열님 이부분은 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 도현님이 코멘트 남겨주신 것처럼, 위에서 table의 크기인 N과 M 주어지기 때문에
로 쓰시면 더 깔끔하고 빠를것 같아요~!!! |
||
|
||
# 입력받은 문자열의 갯수 출력 | ||
for _ in range(K): | ||
# 입력 받은 신이 좋아하는 문자열 | ||
target_string = input().rstrip() | ||
print(answer_dict[target_string]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 DFS로 풀이했는데, 우열님께서 BFS로 깔끔하게 풀이해주신것 같아요~!! 고생하셨습니다 :)