Skip to content

Team A Submission #15

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 2 commits 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
66 changes: 66 additions & 0 deletions solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import sys

class SearchWord:
def __init__(self):
self.moves = [[-1, 0], [1, 0], [0, -1], [0, 1]]

def isWordInBoard(self, board, word):
self.board = board
self.word = word

self.rows = len(board)
self.cols = len(board[0])

print(board)

for i in range(self.rows):
for j in range(self.cols):
found = self.dfs(word, i, j)
if found:
return True

return False

def dfs(self, word, i, j):
if len(word) < 1:
return True

if i < 0 or i >= self.rows or j < 0 or j >= self.cols:
return False

if word[0] != self.board[i][j]:
return False

letter = self.board[i][j]
self.board[i][j] = 0

for (x, y) in self.moves:
found = self.dfs(word[1:], i+x, j+y)

if found:
#Once found, immediately stops the search on other branches
return True

self.board[i][j] = letter

return False

board = []

previous_line = ""
current_line = input()

#Get Input
while True:
previous_line = current_line
current_line = input()

if not current_line:
word = previous_line
break

board.append(previous_line.split(" "))


searchWord = SearchWord()
print(searchWord.isWordInBoard(board, word))