-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossword_Puzzle.py
More file actions
78 lines (63 loc) · 1.96 KB
/
crossword_Puzzle.py
File metadata and controls
78 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/python3
import sys
def printBoard(board):
for row in board:
print(''.join(row))
def possibleDirections(board,word):
length=len(word)
for i in range(10):
for j in range(10):
properSlotH = True
properSlotV = True
for k in range(length):
#Horizontal direction, axis marked as 0:
if j<10-length+1:
if board[i][j+k] not in ['-',word[k]]:
properSlotH = False
#Vertival direction, axis marked as 1:
if i<10-length+1:
if board[i+k][j] not in ['-',word[k]]:
properSlotV = False
if properSlotH and j<10-length+1:
yield (i,j,0)
if properSlotV and i<10-length+1:
yield (i,j,1)
def move(board,word,startLocation):
i,j,axis=startLocation
length=len(word)
if axis == 0:
for k in range(length):
board[i][j+k]=word[k]
else:
for k in range(length):
board[i+k][j]=word[k]
def rollback(board,word,startLocation):
i,j,axis=startLocation
length=len(word)
if axis == 0:
for k in range(length):
board[i][j+k]='-'
else:
for k in range(length):
board[i+k][j]='-'
def solve(board,words):
global solved
if len(words) == 0:
if not solved:
printBoard(board)
solved=True
return
word=words.pop()
for direction in possibleDirections(board,word):
move(board,word,direction)
solve(board,words)
rollback(board,word,direction)
words.append(word)
if __name__ == '__main__':
board = []
for _ in range(10):
board_item = list(input())
board.append(board_item)
words = input().split(";")
solved=False
solve(board,words)