-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha417.py
More file actions
40 lines (39 loc) · 1.54 KB
/
a417.py
File metadata and controls
40 lines (39 loc) · 1.54 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
T = int(input())
for _ in range(T):
N, m = map(int, input().split())
Map = [[0 for _ in range(N)] for _ in range(N)]
delta_x = (m==1)*1
delta_y = (m==2)*1
x,y = 0,0
for i in range(1, N*N+1):
Map[y][x] = i
if m == 1:
if (delta_x == 1 and delta_y == 0) and (x == N-1 or Map[y][x+1] != 0):
delta_x = 0
delta_y = 1
elif (delta_x == 0 and delta_y == 1) and (y == N-1 or Map[y+1][x] != 0):
delta_x = -1
delta_y = 0
elif (delta_x == -1 and delta_y == 0) and (x == 0 or Map[y][x-1] != 0):
delta_x = 0
delta_y = -1
elif (delta_x == 0 and delta_y == -1) and (y == 0 or Map[y-1][x] != 0):
delta_x = 1
delta_y = 0
else:
if (delta_x == 0 and delta_y == 1) and (y == N-1 or Map[y+1][x] != 0):
delta_x = 1
delta_y = 0
elif (delta_x == 1 and delta_y == 0) and (x == N-1 or Map[y][x+1] != 0):
delta_x = 0
delta_y = -1
elif (delta_x == 0 and delta_y == -1) and (y == 0 or Map[y-1][x] != 0):
delta_x = -1
delta_y = 0
elif (delta_x == -1 and delta_y == 0) and (x == 0 or Map[y][x-1] != 0):
delta_x = 0
delta_y = 1
x += delta_x
y += delta_y
Map = ["".join([f"{j: 5d}" for j in i]) for i in Map]
print("\n".join(Map))