Skip to content

initial #10

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys

def find_word(board, word):
def dfs(row, col, seen, togo):
if len(togo) == 0:
return True
if not (0 <= row < len(board)):
return False
if not (0 <= col < len(board[0])):
return False
if (row,col) in seen:
return False

seen.add((row, col))
if board[row][col] == togo[0]:
if dfs(row-1,col,seen,togo[1:]):
return True
if dfs(row+1,col,seen,togo[1:]):
return True
if dfs(row,col-1,seen,togo[1:]):
return True
if dfs(row,col+1,seen,togo[1:]):
return True

for r in range(len(board)):
for c in range(len(board[0])):
if dfs(r,c,set(), word):
return True
return False

# for input, read in each line from stdin and split it by spaces into a list, then append this list to a list of lists that represents our matrix. the final list in this matrix is the target word
matrix = []
test = "S"
for line in sys.stdin:
linestr = line[:len(line)-1]
matrix.append(list(linestr.replace(" ", "")))
if line[1] != ' ':
break

print(find_word(matrix[:-1], "".join(matrix[-1])))