-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
81 lines (70 loc) · 2.28 KB
/
solver.py
File metadata and controls
81 lines (70 loc) · 2.28 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
79
80
81
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Description: it contains a solver using the risk function.
# Author: Yongzhen Ren
from utilities import *
import copy
def calculate_risk(playfield):
risk_list = []
# Rows.
for row in range(playfield.size):
flag = playfield.layout[row][0]
num = 1
for column in range(1, playfield.size):
if playfield.layout[row][column] == flag:
num += 1
else:
flag = playfield.layout[row][column]
risk_list.append(num)
num = 1
risk_list.append(num)
# Columns.
for column in range(playfield.size):
flag = playfield.layout[0][column]
num = 1
for row in range(1, playfield.size):
if playfield.layout[row][column] == flag:
num += 1
else:
flag = playfield.layout[row][column]
risk_list.append(num)
num = 1
risk_list.append(num)
risk = len(risk_list)
return risk
def greedy_place(next_queue, playfield):
risk_dict_all_orders = dict() # 3!
for order in next_queue.generate_all_place_orders():
new_playfield = finish_one_order(order, playfield)
if new_playfield is not None:
risk_dict_all_orders[calculate_risk(new_playfield)] = new_playfield
k = min(risk_dict_all_orders, key = int)
return risk_dict_all_orders[k]
def finish_one_order(order, playfield):
new_playfield = copy.deepcopy(playfield)
for piece in order:
risk_dict_one_piece = dict()
for row in range(playfield.size):
for column in range(playfield.size):
if new_playfield.if_placeable(row, column, piece):
# At least one position is placeable for the current piece.
dummy_playfield = copy.deepcopy(new_playfield) # Deep copy.
dummy_playfield.place_one_piece(row, column, piece)
risk_dict_one_piece[calculate_risk(dummy_playfield)] = (row, column)
if bool(risk_dict_one_piece) == False: # No item is in the dictionary.
return # Go to the next order.
else:
k = min(risk_dict_one_piece, key = int)
row, column = risk_dict_one_piece[k]
new_playfield.place_one_piece(row, column, piece)
return new_playfield
if __name__ == "__main__":
next_queue = NextQueue()
next_queue.print()
playfield = Playfield()
risk = calculate_risk(playfield)
print(risk)
playfield = greedy_place(next_queue, playfield)
risk = calculate_risk(playfield)
playfield.print()
print("The risk of the current playfield is {0}".format(risk))