From 1933f3f9338b0d51bc39540dbfd4d07a13e866c5 Mon Sep 17 00:00:00 2001 From: Gabriel Peralta Date: Sat, 3 Dec 2022 14:30:40 -0500 Subject: [PATCH 1/2] Add program file --- program.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 program.py diff --git a/program.py b/program.py new file mode 100644 index 0000000..7419491 --- /dev/null +++ b/program.py @@ -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)) \ No newline at end of file From 7a080d305c41f894915060ec2692b9b9a9f5e69d Mon Sep 17 00:00:00 2001 From: Gabriel Peralta Date: Sat, 3 Dec 2022 14:34:35 -0500 Subject: [PATCH 2/2] Rename program.py --- program.py => solution.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename program.py => solution.py (100%) diff --git a/program.py b/solution.py similarity index 100% rename from program.py rename to solution.py