Submission details
Task:Hypyt
Sender:madde
Submission time:2025-11-08 21:26:02 +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
#6ACCEPTED0.01 s2, 5details
#70.01 s2, 5details
#80.02 s2, 5details
#90.65 s3, 4, 5details
#100.86 s3, 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.00 s1, 2, 3, 4, 5details
#23ACCEPTED0.00 s1, 2, 3, 4, 5details
#24ACCEPTED0.33 s5details
#25ACCEPTED0.35 s5details
#26--5details
#27ACCEPTED0.32 s5details

Code

use std::io::{self};

const X: usize = 0;
const Y: usize = 1;

fn main() {
    let input = io::stdin();
    let mut input_buf: String = "".to_string();
    let _ = input.read_line(&mut input_buf);
    let line1: Vec<&str> = input_buf.split_whitespace().collect();
    // let start_time = Instant::now();
    let height: u8 = line1[0].parse().unwrap();
    let width: u8 = line1[1].parse().unwrap();
    let query_count: u32 = line1[2].parse().unwrap();
    let mut x_connections: Vec<Vec<u8>> = Vec::with_capacity(height as usize);
    let mut y_connections: Vec<Vec<u8>> = Vec::with_capacity(width as usize);
    y_connections.append(&mut vec![vec![]; width as usize]);
    input_buf = "".to_string();
    for y in 0..height {
        let _ = input.read_line(&mut input_buf);
        x_connections.push(vec![]);
        for x in 0..input_buf.as_bytes().len() {
            if input_buf.as_bytes()[x] == '.' as u8 {
                x_connections[y as usize].push(x as u8);
                y_connections[x].push(y);
            }
        }
        // eprintln!("{y}");
        input_buf = "".to_string();
    }
    // eprintln!("x_connections.len() = {}", x_connections.len());
    // eprintln!("y_connections.len() = {}", y_connections.len());
    // for line in y_connections.iter() {
    //     for node in line.iter() {
    //         eprintln!("\t {node}");
    //     }
    //     eprintln!("")
    // }
    // for line in x_connections.iter() {
    //     for node in line.iter() {
    //         eprintln!("\t {node}");
    //     }
    //     eprintln!("")
    // }
    for _i in 0..query_count {
        let _ = input.read_line(&mut input_buf);
        let line: Vec<&str> = input_buf.split_whitespace().collect();
        println!(
            "{}",
            pathfind(
                &x_connections,
                &y_connections,
                [line[1].parse().unwrap(), line[0].parse().unwrap()],
                [line[3].parse().unwrap(), line[2].parse().unwrap()]
            )
        );
        input_buf = "".to_string();
    }
}

#[derive(Clone)]
struct Node {
    x: u8,
    y: u8,
    f: u16,
    g: u16,
    // parent: usize,
}

fn pathfind(
    x_connections: &Vec<Vec<u8>>,
    y_connections: &Vec<Vec<u8>>,
    mut start: [u8; 2],
    mut target: [u8; 2],
) -> i16 {
    start[0] -= 1;
    start[1] -= 1;
    target[0] -= 1;
    target[1] -= 1;
    // eprintln!("Target: {}, {}", target[X], target[Y]);
    // eprintln!("start: {}, {}", start[X], start[Y]);
    if x_connections[start[Y] as usize].len() == 0 && y_connections[start[X] as usize].len() == 0 {
        return -1;
    }
    if x_connections[target[Y] as usize].len() == 0 && y_connections[target[X] as usize].len() == 0
    {
        return -1;
    }
    if target[X] == start[X] && target[Y] == start[Y] {
        return 0;
    }
    let mut valid_node = false; //if the node is not present in its own connections it's invalid
    for node in x_connections[target[Y] as usize].iter() {
        if node == &target[X] {
            // eprintln!("\tFound:{node}");
            valid_node = true;
            break;
        }
    }
    if !valid_node {
        return -1;
    }
    for node in y_connections[target[X] as usize].iter() {
        if node == &target[Y] {
            // eprintln!("Found:{node}");
            valid_node = true;
            break;
        }
    }
    if !valid_node {
        return -1;
    }
    valid_node = false;
    for node in x_connections[start[Y] as usize].iter() {
        if node == &start[X] {
            // eprintln!("Found:{node}");
            valid_node = true;
            break;
        }
    }
    if !valid_node {
        return -1;
    }
    for node in y_connections[start[X] as usize].iter() {
        if node == &start[Y] {
            // eprintln!("Found:{node}");
            valid_node = true;
            break;
        }
    }
    if !valid_node {
        return -1;
    }
    if start[Y] == target[Y] {
        return 1;
    }
    if start[X] == target[X] {
        return 1;
    }
    let mut open: Vec<Node> = vec![Node {
        x: start[X],
        y: start[Y],
        f: 0,
        g: 0,
        // parent: 0,
    }];
    let mut closed: Vec<Node> = vec![];
    while open.len() > 0 {
        let mut smallest_f = open[0].f;
        let mut q = 0;
        for node in 0..open.len() {
            //smallest f
            if open[node].f < smallest_f {
                q = node;
                smallest_f = open[node].f;
            }
        }
        // eprintln!("\t{q}");
        // eprintln!("\t{},{},{},{}", open[q].x, open[q].y, open[q].f, open[q].g,);
        closed.push(open.remove(q));
        q = closed.len() - 1; //new index of q
        let mut successors: Vec<Node> = vec![];
        let parent = &closed[q];
        for x in x_connections[parent.y as usize].iter() {
            successors.push(Node {
                x: *x,
                y: parent.y,
                f: 0,
                g: 0,
                // parent: q,
            });
        }
        for y in y_connections[parent.x as usize].iter() {
            successors.push(Node {
                x: parent.x,
                y: *y,
                f: 0,
                g: 0,
                // parent: q,
            });
        } //adds the suucessors of q to open
        for succesor in successors.iter_mut() {
            if succesor.x == target[X] as u8 || succesor.y == target[Y] as u8 {
                return (parent.g + 2) as i16;
            } else {
                succesor.g = parent.g + 1;
                succesor.f = (target[X] as i16 - succesor.x as i16 + target[Y] as i16
                    - succesor.y as i16)
                    .abs() as u16
                    + succesor.g;
            }
            let mut duplicate = false;
            //check if succesor is a duplicate
            for node in 0..open.len() {
                if succesor.x == open[node].x
                    && succesor.y == open[node].y
                    && succesor.f >= open[node].f
                {
                    duplicate = true;
                    break;
                }
            }
            if duplicate {
                continue;
            }
            for node in 0..closed.len() {
                if succesor.x == closed[node].x
                    && succesor.y == closed[node].y
                    && succesor.f >= closed[node].f
                {
                    duplicate = true;
                    break;
                }
            }
            if duplicate {
                continue;
            }
            open.push(succesor.clone());
        }
    }
    return -1;
}
/*
test:
16 24 10
.*.**.*.****.*.**.*.****
*...**...****...**...***
**********..**********..
*..*.*..*.***..*.*..*.**
.*.**.*.****.*.**.*.****
*...**...****...**...***
**********..**********..
*..*.*..*.***..*.*..*.**
.*.**.*.****.*.**.*.****
*...**...****...**...***
**********..**********..
*..*.*..*.***..*.*..*.**
.*.**.*.****.*.**.*.****
*...**...****...**...***
**********..**********..
*..*.*..*.***..*.*..*.**
1 1 1 3
2 2 2 2
1 1 4 5
4 5 2 4
3 6 2 2
3 11 7 8
3 11 13 7
3 3 8 15
12 15 6 12
10 8 5 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:

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

correct output
3
4
2
3
4
...

user output
3
4
2
5
5
...

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

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: ACCEPTED

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

correct output
2
3
3
2
2
...

user output
2
3
3
2
2
...

Test 7

Group: 2, 5

Verdict:

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

correct output
2
2
2
2
3
...

user output
2
2
2
2
3
...

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

Test 8

Group: 2, 5

Verdict:

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

correct output
2
3
3
3
3
...

user output
2
5
3
4
5
...

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

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

Feedback: Incorrect character on line 33 col 1: 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
...

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

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: ACCEPTED

input
250 1 200000
*
.
*
.
...

correct output
1
1
1
1
1
...

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

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