Submission details
Task:Hypyt
Sender:3lv11ra
Submission time:2025-11-07 12:18:31 +0200
Language:Python3 (CPython3)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#10.02 s1, 2, 3, 4, 5details
#20.02 s1, 2, 3, 4, 5details
#30.02 s1, 2, 3, 4, 5details
#40.02 s1, 2, 3, 4, 5details
#50.02 s1, 2, 3, 4, 5details
#6--2, 5details
#70.16 s2, 5details
#80.11 s2, 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
#190.92 s5details
#20--5details
#21--5details
#220.02 s1, 2, 3, 4, 5details
#230.02 s1, 2, 3, 4, 5details
#24--5details
#25--5details
#26--5details
#27ACCEPTED0.49 s5details

Code

from collections import deque
import sys
input = sys.stdin.readline

n, m, q = map(int, input().split())
grid = [input().strip() for _ in range(n)]

# Indeksoidaan ruudut lineaarisesti: id = i*m + j
NM = n * m

# 1) Rivijaksot ja niiden ruutulistat
row_comp = [[-1] * m for _ in range(n)]
row_cells = []  # list of lists
r_id = 0
for i in range(n):
    j = 0
    while j < m:
        if grid[i][j] == '*':
            j += 1
            continue
        cells = []
        while j < m and grid[i][j] == '.':
            row_comp[i][j] = r_id
            cells.append(i * m + j)
            j += 1
        row_cells.append(cells)
        r_id += 1

# 2) Sarakejaksot ja niiden ruutulistat
col_comp = [[-1] * m for _ in range(n)]
col_cells = []
c_id = 0
for j in range(m):
    i = 0
    while i < n:
        if grid[i][j] == '*':
            i += 1
            continue
        cells = []
        while i < n and grid[i][j] == '.':
            col_comp[i][j] = c_id
            cells.append(i * m + j)
            i += 1
        col_cells.append(cells)
        c_id += 1

# apurakenteet: map ruutu -> (row_id, col_id)
cell_row = [-1] * NM
cell_col = [-1] * NM
for i in range(n):
    for j in range(m):
        if grid[i][j] == '.':
            idx = i * m + j
            cell_row[idx] = row_comp[i][j]
            cell_col[idx] = col_comp[i][j]

# visited -taulut timestamp-menetelmällä
vis_cell = [0] * NM
vis_row = [0] * r_id
vis_col = [0] * c_id
dist = [-1] * NM
cur_stamp = 1

out_lines = []
for _ in range(q):
    y1, x1, y2, x2 = map(int, input().split())
    y1 -= 1; x1 -= 1; y2 -= 1; x2 -= 1
    s_idx = y1 * m + x1
    t_idx = y2 * m + x2

    # jos jompikumpi on hirviö (tehtävä sanoo, etteivät ole), suoraan -1
    if grid[y1][x1] == '*' or grid[y2][x2] == '*':
        out_lines.append("-1")
        continue

    if s_idx == t_idx:
        out_lines.append("0")
        continue

    # BFS, mutta jokainen rivijakso/sarakejakso käsitellään vähintään kerran
    qd = deque()
    stamp = cur_stamp
    cur_stamp += 1
    # alustus start
    vis_cell[s_idx] = stamp
    dist[s_idx] = 0
    qd.append(s_idx)

    found = -1
    while qd:
        u = qd.popleft()
        d = dist[u]
        # laajennus rivijaksoon
        r = cell_row[u]
        if r != -1 and vis_row[r] != stamp:
            vis_row[r] = stamp
            for v in row_cells[r]:
                if vis_cell[v] != stamp:
                    vis_cell[v] = stamp
                    dist[v] = d + 1
                    if v == t_idx:
                        found = dist[v]
                        qd.clear()
                        break
                    qd.append(v)
            if found != -1:
                break
        # laajennus sarakejaksoon
        c = cell_col[u]
        if c != -1 and vis_col[c] != stamp:
            vis_col[c] = stamp
            for v in col_cells[c]:
                if vis_cell[v] != stamp:
                    vis_cell[v] = stamp
                    dist[v] = d + 1
                    if v == t_idx:
                        found = dist[v]
                        qd.clear()
                        break
                    qd.append(v)
            if found != -1:
                break

    if found == -1:
        out_lines.append("-1")
    else:
        out_lines.append(str(found))

# tulosta kaikki
print("\n".join(out_lines))

Test details

Test 1 (public)

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
1
0
3
3
-1

user output
-1
0
-1
-1
-1

Feedback: Incorrect character on line 1 col 1: expected "1", got "-1"

Test 2

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
1
2
1
2
2
...

user output
1
4
1
4
2
...

Feedback: Incorrect character on line 2 col 1: expected "2", got "4"

Test 3

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
1
2
2
1
2
...

user output
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "1", got "-1"

Test 4

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
3
4
2
3
4
...

user output
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "3", got "-1"

Test 5

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
7

user output
-1

Feedback: Incorrect character on line 1 col 1: expected "7", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "2", got "-1"

Test 8

Group: 2, 5

Verdict:

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

correct output
2
3
3
3
3
...

user output
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "2", got "-1"

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
-1
-1
-1
-1
-1
...

Feedback: Incorrect character on line 1 col 1: expected "104", got "-1"

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:

input
10 1 10
*
*
.
*
...

correct output
0
1
1
0
0
...

user output
0
-1
-1
0
0
...

Feedback: Incorrect character on line 2 col 1: expected "1", got "-1"

Test 23

Group: 1, 2, 3, 4, 5

Verdict:

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
...

Feedback: Incorrect character on line 1 col 1: expected "1", got "-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: ACCEPTED

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

correct output
0
0
0
0
0
...

user output
0
0
0
0
0
...