-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDS.py
161 lines (147 loc) · 4.89 KB
/
IDS.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from array import *
import re
from copy import copy, deepcopy
import numpy as np
exploredNodeCount = 0
createdNodes = 0
class Card:
def __init__(self, color, number):
self.color = color
self.number = number
class Node:
def __init__(self, parent, board, depth, x, y):
self.parent = parent
self.board = board
self.childs = []
self.depth = depth
self.x = x
self.y = y
def getHValue(self):
decreaseRows = 0
oneColorRows = 0
for row in self.board:
if len(row) == 0:
decreaseRows = decreaseRows + 1
oneColorRows = oneColorRows + 1
continue
isDecrease = True
isOneColor = True
rowColor = row[0].color
key = pow(10, 5)
for card in row:
if card.number >= key:
isDecrease = False
if card.color != rowColor:
isOneColor = False
key = card.number
if isDecrease:
decreaseRows = decreaseRows + 1
if isOneColor:
oneColorRows = oneColorRows + 1
return -1 * (decreaseRows + oneColorRows - self.depth)
def getInput(rowCount):
rootBoard = []
for i in range(int(rowCount)):
boardRow = []
j = 0
for card in input().split():
if card == "#":
break
cardInfo = re.compile("([0-9]+)([a-zA-Z]+)").match(card).groups()
boardRow.insert(j, Card(cardInfo[1], int(cardInfo[0])))
j = j + 1
rootBoard.insert(i, boardRow)
print("Let's solve...");
return rootBoard
def isTarget(node):
for row in node.board:
if len(row) == 0:
continue
rowColor = row[0].color
key = pow(10, 5)
for card in row:
if row.index(card) == 0:
continue
if card.color != rowColor or card.number > key:
return False
else:
key = card.number
return True
def makeChilds(node):
childs = []
for rowIndex in range(len(node.board)):
if len(node.board[rowIndex]) == 0:
continue
lastCard = node.board[rowIndex][len(node.board[rowIndex]) - 1]
for targetRowIndex in range(len(node.board)):
if rowIndex == targetRowIndex:
continue
if len(node.board[targetRowIndex]) != 0:
targetLastCard = node.board[targetRowIndex][len(node.board[targetRowIndex]) - 1]
if len(node.board[targetRowIndex]) == 0 or lastCard.number < targetLastCard.number:
childBoard = deepcopy(node.board)
removingCard = childBoard[rowIndex].pop(len(childBoard[rowIndex]) - 1)
childBoard[targetRowIndex].append(removingCard)
childs.append(Node(node, childBoard, node.depth + 1, rowIndex + 1, targetRowIndex + 1))
return childs
def printNode(node):
print("-------------------")
for row in node.board:
string = ""
for card in row:
string = string + str(card.number) + card.color + " "
if string == "":
print("#")
else:
print(string)
print("-------------------")
def dlsSearch(root, limit):
if isTarget(root):
return root
elif limit == 0:
return None
else:
root.childs = makeChilds(root)
global createdNodes
createdNodes += len(root.childs)
for child in root.childs:
global exploredNodeCount
exploredNodeCount = exploredNodeCount + 1
node = dlsSearch(child, limit - 1)
if node is not None:
return node
return None
def idsSearch(root):
for depth in range(10):
node = dlsSearch(root, depth)
if node is not None:
return node
return None
def showResults(target, exploredNodes):
if target is None:
print("We reach to no target in " + str(exploredNodes))
return
print("Target is :")
printNode(target)
print("Target depth is " + str(target.depth))
solution = []
currentNode = target
while True:
if currentNode.parent is None:
break;
solution.append(currentNode)
currentNode = currentNode.parent
solution.reverse()
for node in solution:
print("Move from " + str(node.x) + "th row to " + str(node.y) + "th row")
print("Explored nodes are " + str(exploredNodes))
print("Created nodes are " + str(createdNodes))
if __name__ == "__main__":
inputString = input("Please enter your input:\n")
rowCount = inputString.split()[0]
colorCount = inputString.split()[1]
maxNum = inputString.split()[2]
rootBoard = getInput(rowCount)
root = Node(None, rootBoard, 0, None, None)
target = idsSearch(root)
showResults(target, exploredNodeCount)