-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc_2008_S3.py
38 lines (35 loc) · 1.13 KB
/
ccc_2008_S3.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
cases = int(input())
def move(y, x):
global interCount
c = city[y][x]
if c == "-" or c == "+":
if x > 0 and city[y][x-1] != "*":
if interCount[y][x] + 1 < interCount[y][x-1]:
interCount[y][x-1] = interCount[y][x] + 1
move(y, x-1)
if x < width-1 and city[y][x+1] != "*":
if interCount[y][x] + 1 < interCount[y][x+1]:
interCount[y][x+1] = interCount[y][x] + 1
move(y, x+1)
if c == "|" or c == "+":
if y > 0 and city[y-1][x] != "*":
if interCount[y][x] + 1 < interCount[y-1][x]:
interCount[y-1][x] = interCount[y][x] + 1
move(y-1, x)
if y < height-1 and city[y+1][x] != "*":
if interCount[y][x] + 1 < interCount[y+1][x]:
interCount[y+1][x] = interCount[y][x] + 1
move(y+1, x)
for case in range(cases):
height = int(input())
width = int(input())
city = []
for i in range(height):
city.append(input())
interCount = [[99999 for i in range(width)] for j in range(height)]
interCount[0][0] = 1
move(0, 0)
if interCount[-1][-1] == 99999:
print(-1)
else:
print(interCount[-1][-1])