|
| 1 | +""" |
| 2 | +[문제풀이] |
| 3 | +# Inputs |
| 4 | +- board: m*n grid of characters |
| 5 | +- word: str |
| 6 | +# Outputs |
| 7 | +- word 가 grid 안에 있으면 true, 아니면 false |
| 8 | +# Constraints |
| 9 | +- 탐색: 상하좌우 |
| 10 | +- m == board.length |
| 11 | +- n = board[i].length |
| 12 | +- 1 <= m, n <= 6 |
| 13 | +- 1 <= word.length <= 15 |
| 14 | +
|
| 15 | +- board, word => 무조건 대소문자로 구성 |
| 16 | +# Ideas |
| 17 | +1. 퍼지면서 탐색 -> bfs? |
| 18 | +q = cur.append(i, j, "") |
| 19 | +q.pop() 해서 |
| 20 | +if 현재 w 가 word랑 같다면 그 즉시 bfs stop하고 return True |
| 21 | +만약 bfs 끝까지 했는데도 안나오면 return False |
| 22 | +
|
| 23 | +bfs: q에 다음 원소 넣을 때 현재 값에 이어붙인문자가 w랑 어떤식으로 비교해야 맞는 조건절이 되는걸까? |
| 24 | +=> cur_w의 len 값을 i로 사용하려했지만 w가 더 짧으면 idxError 뜰까봐 보류 |
| 25 | +=> 메모리 초과 오류 뜸.. |
| 26 | +=> 아..같은 자리를 방문하면 안됨!! 즉, 원복이 필요함! |
| 27 | +-> 백트래킹으로 풀이 change |
| 28 | +
|
| 29 | +2. dfs로도 꼭 풀어보기! |
| 30 | +백트래킹 복습 용으로 굿 |
| 31 | +
|
| 32 | +board안의 숫자는 오직 대소문자 -> isalpha() 로 체킹 가능 |
| 33 | +=> 방문하면 0으로 바꾸기 |
| 34 | +
|
| 35 | +풀기는 풀었지만..7090ms : Beats 7.34% |
| 36 | +
|
| 37 | +Follow up : Could you use search pruning to make your solution faster with a larger board |
| 38 | +가지치기 기법 어떻게 도입할까?? |
| 39 | +=> cur_idx 하나 둬서, AB.. ABC -> 진행 도중, 하나라도 word랑 다르면 바로 그 경로 컷 |
| 40 | +=> 그래도 4587ms.. Beats 41.00% |
| 41 | +=> 먼가 더 정석 성능 개선이 필요 |
| 42 | +
|
| 43 | +해설 참고 |
| 44 | +=> word_idx를 이용, any문을 이용한 리팩토링 |
| 45 | +=> 해설 풀이 : 4385ms |
| 46 | +
|
| 47 | +[회고] |
| 48 | +idx를 이용한 가지치기 방법 잘 알아두자 |
| 49 | +""" |
| 50 | + |
| 51 | +# from collections import deque |
| 52 | +# class Solution: |
| 53 | +# def exist(board, word): |
| 54 | +# |
| 55 | +# q = deque([]) |
| 56 | +# n, m = len(board), len(board[0]) |
| 57 | +# |
| 58 | +# q.append((0, 0, board[0][0])) |
| 59 | +# |
| 60 | +# dy = [-1, 0, 1, 0] |
| 61 | +# dx = [0, 1, 0, -1] |
| 62 | +# |
| 63 | +# while q: |
| 64 | +# cur_y, cur_x, cur_w = q.popleft() |
| 65 | +# |
| 66 | +# if cur_w == word: |
| 67 | +# return True |
| 68 | +# |
| 69 | +# for i in range(3): |
| 70 | +# ny = cur_y + dy[i] |
| 71 | +# nx = cur_x + dx[i] |
| 72 | +# |
| 73 | +# if 0 <= ny < n and 0 <= nx < m: |
| 74 | +# q.append((ny, nx, cur_w + board[ny][nx])) |
| 75 | +# |
| 76 | +# return False |
| 77 | +# |
| 78 | +# exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCDE") |
| 79 | + |
| 80 | + |
| 81 | +class Solution: |
| 82 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 83 | + |
| 84 | + n, m = len(board), len(board[0]) |
| 85 | + |
| 86 | + dy = [-1, 0, 1, 0] |
| 87 | + dx = [0, 1, 0, -1] |
| 88 | + |
| 89 | + v = [[False for _ in range(m)] for _ in range(n)] |
| 90 | + |
| 91 | + def dfs(y, x, w): |
| 92 | + #print('y, x : ', y, x) |
| 93 | + #print('w: ', w) |
| 94 | + if w == word: |
| 95 | + return True |
| 96 | + |
| 97 | + if len(w) >= len(word): |
| 98 | + return False |
| 99 | + |
| 100 | + for k in range(4): |
| 101 | + ny = y + dy[k] |
| 102 | + nx = x + dx[k] |
| 103 | + |
| 104 | + if 0 <= ny < n and 0 <= nx < m and not v[ny][nx]: |
| 105 | + v[ny][nx] = True |
| 106 | + if dfs(ny, nx, w + board[ny][nx]): |
| 107 | + return True |
| 108 | + v[ny][nx] = False |
| 109 | + |
| 110 | + for i in range(n): |
| 111 | + for j in range(m): |
| 112 | + if not v[i][j] and board[i][j] == word[0]: |
| 113 | + v[i][j] = True |
| 114 | + if dfs(i, j, board[i][j]): |
| 115 | + return True |
| 116 | + v[i][j] = False |
| 117 | + |
| 118 | + return False |
| 119 | + |
| 120 | +# 해설 |
| 121 | +class Solution: |
| 122 | + def exist(self, board: List[List[str]], word: str) -> bool: |
| 123 | + |
| 124 | + n, m = len(board), len(board[0]) |
| 125 | + |
| 126 | + dy = [-1, 0, 1, 0] |
| 127 | + dx = [0, 1, 0, -1] |
| 128 | + |
| 129 | + def dfs(y, x, idx): |
| 130 | + if idx == len(word): |
| 131 | + return True |
| 132 | + |
| 133 | + if not (0 <= y < n and 0 <= x < m): |
| 134 | + return False |
| 135 | + |
| 136 | + if board[y][x] != word[idx]: |
| 137 | + return False |
| 138 | + |
| 139 | + temp = board[y][x] |
| 140 | + board[y][x] = "" |
| 141 | + |
| 142 | + for i in range(4): |
| 143 | + if dfs(y + dy[i], x + dx[i], idx + 1): |
| 144 | + return True |
| 145 | + |
| 146 | + board[y][x] = temp |
| 147 | + return False |
| 148 | + |
| 149 | + return any(dfs(i, j, 0) for i in range(n) for j in range(m)) |
| 150 | + |
0 commit comments