forked from marcosfede/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfashion_show.py
105 lines (85 loc) · 2.8 KB
/
fashion_show.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
def map_board(func, board):
return [[func(el) for el in row] for row in board]
def clone_board(board):
return map_board(lambda x: x, board)
def print_board(board):
print()
for row in board:
print(' '.join(row))
def solve_rooks(board):
n = len(board)
board = clone_board(board)
av_rows = set(range(n))
av_cols = set(range(n))
for ir in range(n):
for ic in range(n):
if board[ir][ic] == 'x':
av_cols.remove(ic)
av_rows.remove(ir)
for _ in range(len(av_rows)):
r = av_rows.pop()
c = av_cols.pop()
board[r][c] = 'x'
return board
def solve_bishops(board):
n = len(board)
board = clone_board(board)
for i in range(n):
board[0][i] = '+'
if n > 1:
for i2 in range(1, n - 1):
board[-1][i2] = '+'
return board
def merge_solutions(rook_solution, bishop_solution):
n = len(rook_solution)
solution = []
for ir in range(n):
row = []
for ic in range(n):
if rook_solution[ir][ic] == '.':
row.append(bishop_solution[ir][ic])
elif bishop_solution[ir][ic] == '+':
row.append('o')
else:
row.append('x')
solution.append(row)
return solution
def solve(board):
rook_board = map_board(lambda piece: 'x' if piece in 'xo' else '.', board)
bishop_board = map_board(lambda piece: '+' if piece in '+o' else '.', board)
rook_solution = solve_rooks(rook_board)
bishop_solution = solve_bishops(bishop_board)
solution = merge_solutions(rook_solution, bishop_solution)
score = score_board(solution)
insertions = diff_board(board, solution)
return score, insertions
def score_board(board):
scores = {'.': 0, '+': 1, 'x': 1, 'o': 2}
score = 0
for row in board:
for el in row:
score += scores[el]
return score
def diff_board(original, new):
n = len(original)
insertions = []
for rowidx in range(n):
for colidx in range(n):
if original[rowidx][colidx] != new[rowidx][colidx]:
insertions.append((new[rowidx][colidx], rowidx + 1, colidx + 1))
return insertions
def read_input():
t = int(input())
for case in range(1, t + 1):
n, m = map(int, input().split(" "))
board = [['.'] * n for _ in range(n)]
for k in range(m):
piece, row, col = input().split(" ")
board[int(row) - 1][int(col) - 1] = piece
score, insertions = solve(board)
print(f'CASE #{case}: {score} {len(insertions)}')
for insertion in insertions:
print(f'{insertion[0]} {str(insertion[1])} {str(insertion[2])}')
# read_input()
for row in solve([['.', '.', '.'], ['+', '+', '+'], ['.', '.', '.']]):
print(row)