Created
May 26, 2016 18:29
-
-
Save ankitsinghaniyaz/d3c14f4f586432a26cacab8a7866465a to your computer and use it in GitHub Desktop.
Python code to solve boggle with crossover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Program to find count of words in a given grid | |
Rule : Do not reuse a cell | |
Rule : Do not cross over | |
Rule : can travel to adjecent 8 cells | |
input : 'word' | |
n x m | |
r a y x d | |
p e t r o | |
o r p o z | |
output : 'output is x', where x is no of instances of words | |
""" | |
count = 0 | |
def main(): | |
word = input() | |
temp = input().strip().split() | |
m, n = int(temp[0]), int(temp[2]) # catch col, row | |
grid = [] | |
for i in range(n): | |
temp = input().strip().split() | |
grid.append(temp) | |
# Test code fixed input below | |
# Input Set 1 : TRUE | |
# word = 'report' | |
# n, m = 4, 5 | |
# grid = [['r','a','y','x','d'], | |
# ['p','e','t','r','o'], | |
# ['o','r','p','o','z'], | |
# ['h','r','y','k','v'] | |
# ] | |
# Input Set 2 : TRUE | |
# word = 'cat' | |
# n, m = 5, 3 | |
# grid = [['a','t','y'], | |
# ['c','y','y'], | |
# ['y','a','t'], | |
# ['t','t','y'], | |
# ['h','r','y'], | |
# ] | |
# Input Set 3 : TRUE | |
# word = 'cobra' | |
# n, m = 3, 3 | |
# grid = [['o','b','o'], | |
# ['c','r','a'], | |
# ['b','o','o'], | |
# ] | |
# Debug line | |
# print(word, n, m, grid) | |
# 1. Check of all the letters are there | |
solutions = solve(word, n, m, grid) | |
def solve(word, rows, cols, grid): | |
"""finds number of instances of word in grid | |
and returns the count. | |
""" | |
global count | |
for row in range(rows): | |
for col in range(cols): | |
visited = [[False for col in range(cols)] for row in range(rows)] | |
dfs(word, row, col, grid, visited, 0, []) | |
# temp = set(result) | |
# temp = list(temp) | |
print ('output is', count // 8) | |
def dfs(word, crow, ccol, grid, visited, cindex, lst): | |
# global result | |
global count | |
if cindex == len(word): # A match has been made | |
# print("found:", lst) | |
if crossover(lst) == False: | |
count += 1 | |
# Result found.. what to do? | |
# 1. if | |
return | |
if crow < 0 or ccol < 0 or crow > len(grid) - 1 or ccol > len(grid[0]) - 1: | |
return | |
if visited[crow][ccol] == True: # Already visited, then return | |
return | |
if grid[crow][ccol] != word[cindex]: # If character does not matches here then return | |
return | |
# If character matched | |
# Try for all adjecent word for next cell | |
# print(crow, ccol, cindex) # will have to store this tuple | |
lst.append([crow, ccol, cindex]) | |
# print(grid[crow][ccol]) | |
visited[crow][ccol] = True | |
dfs(word, crow - 1, ccol, grid, visited, cindex + 1, lst) | |
dfs(word, crow - 1, ccol + 1, grid, visited, cindex + 1, lst) | |
dfs(word, crow, ccol + 1, grid, visited, cindex + 1, lst) | |
dfs(word, crow + 1, ccol + 1, grid, visited, cindex + 1, lst) | |
dfs(word, crow + 1, ccol, grid, visited, cindex + 1, lst) | |
dfs(word, crow + 1, ccol - 1, grid, visited, cindex + 1, lst) | |
dfs(word, crow, ccol - 1, grid, visited, cindex + 1, lst) | |
dfs(word, crow - 1, ccol - 1, grid, visited, cindex + 1, lst) | |
visited[crow][ccol] = False | |
lst.pop() | |
def crossover(lst): | |
# print(lst) | |
# STEPS | |
# First move is a diagnol move | |
# second move is a move back to initial row or column | |
# third move is move bak to 2nd cells row and 1st cells column or | |
# 2nd cells column and 1st cells row | |
# then return True | |
if len(lst) < 4: | |
return False | |
for i in range(len(lst) - 3): | |
row = 0 | |
col = 1 | |
first = lst[i] | |
second = lst[i + 1] | |
third = lst[i + 2] | |
fourth = lst[i + 3] | |
# First move | |
if (abs(first[row] - second[row]) == 1 and abs(first[col] - second[col]) == 1): | |
if(third[row] == first[row] or third[col] == first[col]): | |
if(abs(third[row] - fourth[row]) == 1 and abs(third[col] - fourth[col]) == 1): | |
if ((fourth[row] == first[row] and fourth[col] == second[col]) | |
or | |
(fourth[row] == second[row] and fourth[col] == first[col])): | |
return True | |
return False | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment