Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-09-29] wooyeol #271 #295

Merged
merged 1 commit into from
Oct 6, 2023
Merged

[2023-09-29] wooyeol #271 #295

merged 1 commit into from
Oct 6, 2023

Conversation

Woo-Yeol
Copy link
Member

PR Summary

문자열 지옥에 빠진 호석
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. 등록된 해시 테이블의 값에서 입력받은 문자열의 값을 반환

@Woo-Yeol Woo-Yeol added the BOJ label Sep 30, 2023
@Woo-Yeol Woo-Yeol self-assigned this Sep 30, 2023
@Woo-Yeol Woo-Yeol linked an issue Sep 30, 2023 that may be closed by this pull request
@Woo-Yeol
Copy link
Member Author

늦어서 죄송합니다 여러분들 ㅠㅠㅠㅠ

Copy link
Contributor

@zsmalla zsmalla left a comment

Choose a reason for hiding this comment

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

풀이 고생하셨습니다 우열님. BFS로 깔끔하게 로직을 작성하셨네요. 모든 알파벳에 대한 경우의 수를 구해놓고 효율적으로 답을 도출할 수 있게 잘 풀이하신 것 같습니다.

answer_dict[text] += 1

# 문자열의 길이가 5 이상이라면 더 이상 검색하지 않는다.
if len(text) >= 5:
Copy link
Contributor

Choose a reason for hiding this comment

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

큐에 삽입할 때(52번 라인) 이 조건문을 추가해주면 연산 횟수도 줄고 조금 더 깔끔해지지 않을까 싶습니다!

N, M, K = map(int, input().split())

# N by M 의 격자
table = []
Copy link
Contributor

Choose a reason for hiding this comment

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

별 다른 추가 연산이 없기 때문에 57 ~ 59번 라인을 table = [input().rstrip() for _ in range(N)] 으로 가능! 파이써닉하게 가봅시다

answer_dict = defaultdict(int)
for row_i, row in enumerate(table):
for col_i, col in enumerate(row):
bfs(row_i, col_i)
Copy link
Contributor

Choose a reason for hiding this comment

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

우열님 이부분은 range(len()) 을 사용하셔도 괜찮을 것 같습니다!!
제 로컬 IDE 에서 돌려보니 약 1.75 배정도 속도차이가 나네요!

Copy link
Contributor

Choose a reason for hiding this comment

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

도현님이 코멘트 남겨주신 것처럼, 위에서 table의 크기인 N과 M 주어지기 때문에

for row_i in range(N):
    for col_i in range(M):
        bfs(row_i, col_i)

로 쓰시면 더 깔끔하고 빠를것 같아요~!!!

answer_dict = defaultdict(int)
for row_i, row in enumerate(table):
for col_i, col in enumerate(row):
bfs(row_i, col_i)
Copy link
Contributor

Choose a reason for hiding this comment

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

도현님이 코멘트 남겨주신 것처럼, 위에서 table의 크기인 N과 M 주어지기 때문에

for row_i in range(N):
    for col_i in range(M):
        bfs(row_i, col_i)

로 쓰시면 더 깔끔하고 빠를것 같아요~!!!

# 상, 하, 좌, 우, 대각선 왼쪽 위, 대각선 오른쪽 위, 대각선 왼쪽 아래, 대각선 오른쪽 아래
directions = ((-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1))

def bfs(x, y):
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 DFS로 풀이했는데, 우열님께서 BFS로 깔끔하게 풀이해주신것 같아요~!! 고생하셨습니다 :)

@Woo-Yeol Woo-Yeol merged commit 0bfcffa into main Oct 6, 2023
@zsmalla zsmalla deleted the wooyeol-#271 branch March 18, 2024 10:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BOJ] 문자열 지옥에 빠진 호석
4 participants