Submission details
Task:Hypyt
Sender:Toitsu
Submission time:2025-10-30 14:26:09 +0200
Language:Python3 (PyPy3)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
#40
#50
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1, 2, 3, 4, 5details
#2ACCEPTED0.09 s1, 2, 3, 4, 5details
#3ACCEPTED0.08 s1, 2, 3, 4, 5details
#4ACCEPTED0.05 s1, 2, 3, 4, 5details
#5ACCEPTED0.05 s1, 2, 3, 4, 5details
#6--2, 5details
#7--2, 5details
#8--2, 5details
#9--3, 4, 5details
#10--3, 4, 5details
#11--3, 4, 5details
#12--4, 5details
#13--4, 5details
#14--4, 5details
#15--5details
#16--5details
#17--5details
#18--5details
#19--5details
#20--5details
#21--5details
#22ACCEPTED0.05 s1, 2, 3, 4, 5details
#23ACCEPTED0.05 s1, 2, 3, 4, 5details
#24--5details
#25--5details
#26--5details
#27--5details

Code

from collections import deque

height, width, question_count = input().split(" ")
height = int(height)
width = int(width)
question_count = int(question_count)

# create the grid from input
grid = []
for _ in range(height):
    grid.append(list(input()))

# read the questions from input
questions = []
for _ in range(question_count):
    data = input().split(" ")
    # convert 1-based Y1 X1 Y2 X2 to 0-based [(X2, Y2), (X2, Y2)]
    questions.append([(int(data[1]) - 1, int(data[0]) - 1), (int(data[3]) - 1, int(data[2]) - 1)])



def get_neighbors(node_x, node_y):
    neighbors = set()

    for x in range(width):
        if grid[node_y][x] == "." and x != node_x:
            neighbors.add((x, node_y))

    for y in range(height):
        if grid[y][node_x] == "." and y != node_y:
            neighbors.add((node_x, y))

    return list(neighbors)

def reconstructPath(start_x, start_y, end_x, end_y, previous):
    path = []

    at = (end_x, end_y)

    while at is not None:
        path.append(at)
        at = previous.get(at)
    
    path.reverse()

    if path[0] == (start_x, start_y):
        return path
    
    return []

def solve(start_x, start_y):
    queue = deque()
    queue.append((start_x, start_y))

    visited_nodes = {}

    for y in range(height):
        for x in range(width):
            visited_nodes[(x, y)] = False
    
    visited_nodes[(start_x, start_y)] = True

    previous = {}

    while queue:
        node_x, node_y = queue.popleft()
        neighbors = get_neighbors(node_x, node_y)

        for next in neighbors:
            if not visited_nodes[next]:
                queue.append(next)
                visited_nodes[next] = True
                previous[next] = (node_x, node_y)
        
    return previous


def bfs(start_x, start_y, end_x, end_y):
    previous = solve(start_x, start_y)

    return reconstructPath(start_x, start_y, end_x, end_y, previous)


# process questions
for question in questions:
    start_x, start_y = question[0]
    end_x, end_y = question[1]
    path = bfs(start_x, start_y, end_x, end_y)

    print(len(path) - 1)

Test details

Test 1 (public)

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
4 6 5
.*.***
*...**
*****.
*..*.*
...

correct output
1
0
3
3
-1

user output
1
0
3
3
-1

Test 2

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
10 10 10
..........
.....*....
........*.
*.*....*..
...

correct output
1
2
1
2
2
...

user output
1
2
1
2
2
...

Test 3

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
10 10 10
*...***.**
*****.*...
**..**.**.
..**.**.*.
...

correct output
1
2
2
1
2
...

user output
1
2
2
1
2
...

Test 4

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
10 10 10
***.*.****
**********
*.********
.*.***.**.
...

correct output
3
4
2
3
4
...

user output
3
4
2
3
4
...

Test 5

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
10 10 1
.****.****
**.**..***
**********
*******..*
...

correct output
7

user output
7

Test 6

Group: 2, 5

Verdict:

input
250 250 250
.*...*.....*******..**...*.......

correct output
2
3
3
2
2
...

user output
(empty)

Test 7

Group: 2, 5

Verdict:

input
250 250 250
...*......**.**.*.*..**..*..**...

correct output
2
2
2
2
3
...

user output
(empty)

Test 8

Group: 2, 5

Verdict:

input
250 250 250
**..**..****.****.*.***.***..*...

correct output
2
3
3
3
3
...

user output
(empty)

Test 9

Group: 3, 4, 5

Verdict:

input
40 40 200000
...*.**.*..*.............*.*.....

correct output
2
2
2
2
2
...

user output
(empty)

Test 10

Group: 3, 4, 5

Verdict:

input
40 40 200000
**.**..*.*.*.******....****.*....

correct output
2
1
3
2
2
...

user output
(empty)

Test 11

Group: 3, 4, 5

Verdict:

input
40 40 200000
.*.*.**.*****.***.*.****.**.**...

correct output
3
3
3
3
3
...

user output
(empty)

Test 12

Group: 4, 5

Verdict:

input
80 80 200000
*....**.***..****...*.....*......

correct output
2
2
2
2
2
...

user output
(empty)

Test 13

Group: 4, 5

Verdict:

input
80 80 200000
.***.*..*.***..*****....**...*...

correct output
3
2
2
3
2
...

user output
(empty)

Test 14

Group: 4, 5

Verdict:

input
80 80 200000
*******.*****.*..*..****...***...

correct output
2
3
1
2
2
...

user output
(empty)

Test 15

Group: 5

Verdict:

input
250 250 200000
*....*..*..*..**..*.........**...

correct output
3
2
2
2
2
...

user output
(empty)

Test 16

Group: 5

Verdict:

input
250 250 200000
..*....*..*......*.**.*.*..***...

correct output
2
2
2
2
2
...

user output
(empty)

Test 17

Group: 5

Verdict:

input
250 250 200000
*..*.*****.*********.****.****...

correct output
3
3
2
2
2
...

user output
(empty)

Test 18

Group: 5

Verdict:

input
250 250 200000
*********.**********.******.**...

correct output
3
3
3
3
3
...

user output
(empty)

Test 19

Group: 5

Verdict:

input
250 250 200000
.*****************************...

correct output
104
422
145
93
65
...

user output
(empty)

Test 20

Group: 5

Verdict:

input
250 250 200000
..****************************...

correct output
57
155
38
65
98
...

user output
(empty)

Test 21

Group: 5

Verdict:

input
250 250 200000
.*****************************...

correct output
498
498
498
498
498
...

user output
(empty)

Test 22

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
10 1 10
*
*
.
*
...

correct output
0
1
1
0
0
...

user output
0
1
1
0
0
...

Test 23

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

input
1 10 10
........*.
1 7 1 10
1 4 1 7
1 5 1 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 24

Group: 5

Verdict:

input
250 1 200000
*
.
*
.
...

correct output
1
1
1
1
1
...

user output
(empty)

Test 25

Group: 5

Verdict:

input
1 250 200000
*.*.*...*.*.**.***..**.*.*..**...

correct output
1
1
1
1
1
...

user output
(empty)

Test 26

Group: 5

Verdict:

input
250 250 200000
.................................

correct output
2
2
2
2
2
...

user output
(empty)

Test 27

Group: 5

Verdict:

input
250 250 200000
******************************...

correct output
0
0
0
0
0
...

user output
(empty)