Submission details
Task:Hypyt
Sender:JuusoH
Submission time:2025-11-09 14:45:36 +0200
Language:Rust (2021)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3, 4, 5details
#2ACCEPTED0.00 s1, 2, 3, 4, 5details
#3ACCEPTED0.00 s1, 2, 3, 4, 5details
#40.00 s1, 2, 3, 4, 5details
#5ACCEPTED0.00 s1, 2, 3, 4, 5details
#60.07 s2, 5details
#70.05 s2, 5details
#80.03 s2, 5details
#90.06 s3, 4, 5details
#100.06 s3, 4, 5details
#110.06 s3, 4, 5details
#120.06 s4, 5details
#130.06 s4, 5details
#140.06 s4, 5details
#150.13 s5details
#160.11 s5details
#170.09 s5details
#180.08 s5details
#19ACCEPTED0.07 s5details
#20ACCEPTED0.07 s5details
#21ACCEPTED0.06 s5details
#22ACCEPTED0.00 s1, 2, 3, 4, 5details
#23ACCEPTED0.00 s1, 2, 3, 4, 5details
#24ACCEPTED0.06 s5details
#25ACCEPTED0.06 s5details
#26ACCEPTED0.15 s5details
#27ACCEPTED0.05 s5details

Compiler report

warning: unused import: `std::time::*`
 --> input/code.rs:3:5
  |
3 | use std::time::*;
  |     ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `l`
  --> input/code.rs:26:13
   |
26 |         for l in 0..m {
   |             ^ help: if this is intentional, prefix it with an underscore: `_l`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `l`
   --> input/code.rs:101:13
    |
101 |         for l in 0..node_count {
    |             ^ help: if this is intentional, prefix it with an underscore: `_l`

warning: type alias `Pos` is never used
 --> input/code.rs:4:6
  |
4 | type Pos = (usize, usize);
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `one_jump_gap` is never used
   --> input/code.rs:181:4
    |
181 | fn one_jump_gap(a: &Pos, b: &Pos) -> bool {
    |    ^^^^^^^^^^^^

warning: function `two_jump_gap` is never used
   --> input/code.rs:185:4
    |
185 | fn two_jump_ga...

Code

use std::collections::VecDeque;
use std::io::{self};
use std::time::*;
type Pos = (usize, usize);

fn main() {
    //test_perf();
    //from_file();
    //return;
    let mut input = String::new();
    let stdin = io::stdin();
    _ = stdin.read_line(&mut input);
    let mut first_line = input.split_whitespace();
    let n: usize = first_line.next().unwrap().parse().unwrap();
    let m: usize = first_line.next().unwrap().parse().unwrap();
    let q: usize = first_line.next().unwrap().parse().unwrap();

    let mut board: Vec<Vec<bool>> = vec![];

    for i in 0..n {
        board.push(vec![]);
        input.clear();
        _ = stdin.read_line(&mut input);
        let mut line = input.chars();

        for l in 0..m {
            board[i].push(line.next().unwrap() == '*');
        }
    }

    //let mut tests: Vec<(usize, usize, usize, usize)> = vec![];

    let distance_pairs = precomputations(&board, n, m);

    let mut results = vec![];

    for _ in 0..q {
        input.clear();
        _ = stdin.read_line(&mut input);
        let mut line = input.split_whitespace();
        let y1: usize = line.next().unwrap().parse().unwrap();
        let x1: usize = line.next().unwrap().parse().unwrap();
        let y2: usize = line.next().unwrap().parse().unwrap();
        let x2: usize = line.next().unwrap().parse().unwrap();
        //tests.push((y1 - 1, x1 - 1, y2 - 1, x2 - 1));

        results.push(process_test(
            &board,
            n,
            &distance_pairs,
            (y1 - 1, x1 - 1, y2 - 1, x2 - 1),
        ));
    }
    for r in results {
        print!("{r} ");
    }
    // test_once(
    //     &tests, &board, &row_safe, &col_safe, &mut cells, n, m, is_sparse,
    // );
}

fn precomputations(board: &Vec<Vec<bool>>, n: usize, m: usize) -> Vec<Vec<i32>> {
    let mut cols_in_row: Vec<Vec<usize>> = vec![];
    cols_in_row.reserve(n);
    let mut rows_in_col: Vec<Vec<usize>> = vec![];
    rows_in_col.reserve(m);
    for _ in 0..m {
        rows_in_col.push(vec![]);
    }
    for i in 0..n {
        let row = &board[i];
        cols_in_row.push(vec![]);
        for l in 0..m {
            let cell = row[l];
            if !cell {
                cols_in_row[i].push(l);
                rows_in_col[l].push(i);
            }
        }
    }
    let node_count = n + m;
    let mut connections: Vec<Vec<usize>> = vec![];
    for _ in 0..node_count {
        connections.push(vec![]);
    }

    for i in 0..n {
        for &l in &cols_in_row[i] {
            let row_index = i;
            let col_index = n + l;

            connections[row_index].push(col_index);
            connections[col_index].push(row_index);
        }
    }

    let mut distance_pairs: Vec<Vec<i32>> = vec![];

    for i in 0..node_count {
        distance_pairs.push(vec![]);
        for l in 0..node_count {
            distance_pairs[i].push(-1);
        }
    }

    let mut deque: VecDeque<usize> = VecDeque::new();

    for s in 0..node_count {
        if connections[s].is_empty() {
            distance_pairs[s][s] = 0;
            continue;
        }

        let dist_row = &mut distance_pairs[s];
        dist_row[s] = 0;
        deque.clear();
        deque.push_back(s);

        while deque.len() > 0 {
            let u = deque.pop_back().unwrap();
            let nu = dist_row[u] + 1;
            for &v in &connections[u] {
                if dist_row[v] == -1 {
                    dist_row[v] = nu;
                    deque.push_back(v);
                }
            }
        }
    }
    distance_pairs
}

const START_MIN: i32 = 67i32.pow(5);

fn process_test(
    board: &Vec<Vec<bool>>,
    n: usize,
    distance_pairs: &Vec<Vec<i32>>,
    test: (usize, usize, usize, usize),
) -> i32 {
    let a = (test.0, test.1);
    let b = (test.2, test.3);

    if board[a.0][a.1] || board[b.0][b.1] {
        return -1;
    }
    if a == b {
        return 0;
    }
    /*if one_jump_gap(&a, &b) {
        return 1;}
    if two_jump_gap(board, &a, &b) {
        return 2;
    }*/

    let start_row = a.0;
    let end_row = b.0;
    let start_col = n + a.1;
    let end_col = n + b.1;

    let mut smallest_distance = START_MIN;

    let mut res_distances = vec![];
    res_distances.push(distance_pairs[start_row][end_row]);
    res_distances.push(distance_pairs[start_row][end_col]);
    res_distances.push(distance_pairs[start_col][end_row]);
    res_distances.push(distance_pairs[start_col][end_col]);

    let mut found = false;

    for dist in res_distances {
        if dist != -1 && dist < smallest_distance {
            smallest_distance = dist;
            found = true;
        }
    }

    if !found { -1 } else { smallest_distance + 1 }
}

fn one_jump_gap(a: &Pos, b: &Pos) -> bool {
    a.0 == b.0 || a.1 == b.1
}

fn two_jump_gap(board: &Vec<Vec<bool>>, a: &Pos, b: &Pos) -> bool {
    !board[a.0][b.1] || !board[b.0][a.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 2 2 2 1 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 2 1 3 2 2 

Test 4

Group: 1, 2, 3, 4, 5

Verdict:

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

correct output
3
4
2
3
4
...

user output
3 4 2 3 6 2 -1 3 1 2 

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

Test 5

Group: 1, 2, 3, 4, 5

Verdict: ACCEPTED

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

correct output
7

user output

Test 6

Group: 2, 5

Verdict:

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

correct output
2
3
3
2
2
...

user output
2 3 3 2 2 2 2 2 2 2 2 2 2 2 2 ...

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

Test 7

Group: 2, 5

Verdict:

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

correct output
2
2
2
2
3
...

user output
2 2 2 2 3 3 5 2 2 3 2 3 3 2 2 ...

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

Test 8

Group: 2, 5

Verdict:

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

correct output
2
3
3
3
3
...

user output
2 10 5 6 5 2 6 8 8 3 2 11 5 4 ...

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

Test 9

Group: 3, 4, 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

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

Test 10

Group: 3, 4, 5

Verdict:

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

correct output
2
1
3
2
2
...

user output
2 1 3 2 2 2 3 3 3 2 3 2 3 2 2 ...

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

Test 11

Group: 3, 4, 5

Verdict:

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

correct output
3
3
3
3
3
...

user output
7 7 4 5 11 5 3 5 2 4 2 5 3 5 2...

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

Test 12

Group: 4, 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

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

Test 13

Group: 4, 5

Verdict:

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

correct output
3
2
2
3
2
...

user output
3 2 2 3 2 2 3 2 2 3 3 2 2 3 2 ...

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

Test 14

Group: 4, 5

Verdict:

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

correct output
2
3
1
2
2
...

user output
2 5 1 2 2 2 2 3 3 2 2 2 2 2 2 ...

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

Test 15

Group: 5

Verdict:

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

correct output
3
2
2
2
2
...

user output
3 2 2 2 2 2 3 2 2 2 2 2 2 2 2 ...

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

Test 16

Group: 5

Verdict:

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

correct output
2
2
2
2
2
...

user output
2 2 2 2 2 2 2 2 2 2 2 5 2 2 2 ...

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

Test 17

Group: 5

Verdict:

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

correct output
3
3
2
2
2
...

user output
10 3 2 2 2 3 5 3 2 8 3 3 2 12 ...

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

Test 18

Group: 5

Verdict:

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

correct output
3
3
3
3
3
...

user output
5 6 13 17 17 6 8 2 7 10 6 7 16...

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

Test 19

Group: 5

Verdict: ACCEPTED

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

correct output
104
422
145
93
65
...

user output
104 422 145 93 65 69 35 27 86 ...

Test 20

Group: 5

Verdict: ACCEPTED

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

correct output
57
155
38
65
98
...

user output
57 155 38 65 98 122 73 25 54 1...

Test 21

Group: 5

Verdict: ACCEPTED

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

correct output
498
498
498
498
498
...

user output
498 498 498 498 498 498 498 49...

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

Test 24

Group: 5

Verdict: ACCEPTED

input
250 1 200000
*
.
*
.
...

correct output
1
1
1
1
1
...

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

Test 25

Group: 5

Verdict: ACCEPTED

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

correct output
1
1
1
1
1
...

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

Test 26

Group: 5

Verdict: ACCEPTED

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

correct output
2
2
2
2
2
...

user output
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

Test 27

Group: 5

Verdict: ACCEPTED

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

correct output
0
0
0
0
0
...

user output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...