-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2105_디저트카페.py
101 lines (81 loc) · 2.82 KB
/
2105_디저트카페.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
test_num = int(input())
for t in range(test_num):
N = int(input())
max_result = 0
board = [0] * N
for i in range(N):
board[i] = list(map(int, input().split()))
def dessert_tour(k,i,j,arr,move1,move2,temp_result):
global max_result
if k == 4:
# print('k == 4')
if temp_result > max_result:
max_result = temp_result
return
if k == 0:
# print('k == 0')
cnt = 0
temp_i = i
temp_j = j
now_arr = arr[:]
for l in range(N-j-1):
cnt += 1
temp_i += 1
temp_j += 1
if not(0 <= temp_i < N and 0 <= temp_j < N):
return
if board[temp_i][temp_j] in now_arr:
return
now_arr += [board[temp_i][temp_j]]
dessert_tour(k+1, temp_i, temp_j, now_arr, cnt, move2, temp_result + cnt)
elif k == 1:
# print('k == 1')
cnt = 0
temp_i = i
temp_j = j
now_arr = arr[:]
for l in range(N-i-1):
cnt += 1
temp_i += 1
temp_j -= 1
if not(0 <= temp_i < N and 0 <= temp_j < N):
return
if board[temp_i][temp_j] in now_arr:
return
now_arr += [board[temp_i][temp_j]]
dessert_tour(k+1, temp_i, temp_j, now_arr, move1, cnt, temp_result + cnt)
elif k == 2:
# print('k == 2')
temp_i = i
temp_j = j
now_arr = arr[:]
for l in range(move1):
temp_i -= 1
temp_j -= 1
if not(0 <= temp_i < N and 0 <= temp_j < N):
return
if board[temp_i][temp_j] in now_arr:
return
now_arr += [board[temp_i][temp_j]]
dessert_tour(k+1, temp_i, temp_j, now_arr, move1, move2, temp_result + move1)
elif k == 3:
# print('k == 3')
temp_i = i
temp_j = j
now_arr = arr[:]
for l in range(move2-1):
temp_i -= 1
temp_j += 1
if not(0 <= temp_i < N and 0 <= temp_j < N):
return
if board[temp_i][temp_j] in now_arr:
return
now_arr += [board[temp_i][temp_j]]
dessert_tour(k+1, temp_i, temp_j, now_arr, move1, move2, temp_result + move2-1)
for i in range(N-2):
for j in range(1, N-1):
dessert_tour(0,i,j,[board[i][j]],-1,-1,1)
if max_result == 0:
max_result = -1
print('#' + str(t+1) + ' ', end='')
print(max_result)