Submission details
Task:Hypyt
Sender:MatoCSES
Submission time:2025-11-06 15:33:37 +0200
Language:Rust (2021)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#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
#4ACCEPTED0.00 s1, 2, 3, 4, 5details
#5ACCEPTED0.00 s1, 2, 3, 4, 5details
#6--2, 5details
#7--2, 5details
#8ACCEPTED0.81 s2, 5details
#9--3, 4, 5details
#10ACCEPTED0.68 s3, 4, 5details
#11ACCEPTED0.49 s3, 4, 5details
#12--4, 5details
#13--4, 5details
#14--4, 5details
#15--5details
#16--5details
#17--5details
#18--5details
#19ACCEPTED0.63 s5details
#20--5details
#21ACCEPTED0.37 s5details
#22ACCEPTED0.00 s1, 2, 3, 4, 5details
#23ACCEPTED0.00 s1, 2, 3, 4, 5details
#24ACCEPTED0.39 s5details
#25ACCEPTED0.39 s5details
#26--5details
#27ACCEPTED0.36 s5details

Code

use std::io::{
    BufReader,
    BufRead,
    stdin,
    Stdin
};
use std::collections::{
    HashMap, 
    HashSet,
    VecDeque
};

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Node {
    Cell((usize, usize)),
    Row(usize),
    Column(usize)
}

fn main() {
    let mut reader: BufReader<Stdin> = BufReader::new(stdin());
    
    let mut input: String = String::new();
    reader.read_line(&mut input).unwrap();
    let input: Vec<usize> = input.trim().split(" ").map(|s: &str| s.parse::<usize>().unwrap()).collect();

    let mut graph: HashMap<Node, Vec<Node>> = HashMap::new();
    parse_graph(&input, &mut reader, &mut graph);

    let mut queries: HashMap<Node, HashSet<Node>> = HashMap::new();
    let mut queries_order: Vec<(Node, Node)> = Vec::new();
    for _ in 0..input[2] {
        let mut query: String = String::new();
        reader.read_line(&mut query).unwrap();
        let query: Vec<usize> = query.trim().split(" ").map(|s: &str| s.parse::<usize>().unwrap()).collect();

        let start: Node = Node::Cell((query[0], query[1]));
        let goal: Node = Node::Cell((query[2], query[3]));
        queries.entry(start).or_insert_with(HashSet::new).insert(goal);
        queries_order.push((start, goal));
    }

    let mut distance_table: HashMap<Node, HashMap<Node, usize>> = HashMap::new();
    get_distance_table(&mut distance_table, &graph, &queries);

    for (start, goal) in queries_order {
        if let Some(dist) = distance_table.get(&start).unwrap().get(&goal) {
            println!("{}", dist/2);
        } else {
            println!("-1")
        }
    }
}

fn parse_graph(input: &Vec<usize>, reader: &mut BufReader<Stdin>, graph: &mut HashMap<Node, Vec<Node>>) {
    for r in 0..input[0] {
        let mut row: String = String::new();
        reader.read_line(&mut row).unwrap();
        
        for (c, cell) in row.chars().enumerate() {
            if cell == '.' {
                graph.insert(Node::Cell((r + 1, c + 1)), Vec::from([Node::Row(r + 1), Node::Column(c + 1)]));
                graph.entry(Node::Row(r + 1)).or_insert_with(Vec::new).push(Node::Cell((r + 1, c + 1)));
                graph.entry(Node::Column(c + 1)).or_insert_with(Vec::new).push(Node::Cell((r + 1, c + 1)));
            }
        }
    }
}

fn get_distance_table(table: &mut HashMap<Node, HashMap<Node, usize>>, graph: &HashMap<Node, Vec<Node>>, queries: &HashMap<Node, HashSet<Node>>) {
    for (&root, goals) in queries {
        table.insert(root, HashMap::new());
        bfs(root, goals, table.get_mut(&root).unwrap(), graph);
    }
}

fn bfs(start: Node, goals: &HashSet<Node>, dist: &mut HashMap<Node, usize>, graph: &HashMap<Node, Vec<Node>>) {
    dist.insert(start, 0);
    let mut queue: VecDeque<Node> = VecDeque::from([start]);
    let goals_count: usize = goals.len();
    let mut goals_visited: usize = 0;

    while let Some(current) = queue.pop_front() {
        for neighbour in graph.get(&current).unwrap() {
            if !dist.contains_key(neighbour) {
                dist.insert(*neighbour, dist.get(&current).unwrap() + 1);
                queue.push_back(*neighbour);
                if goals.contains(neighbour) {
                    goals_visited += 1;
                }
            }
        }
        if goals_visited == goals_count {
            break;
        }
    }
}

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

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

correct output
2
3
3
3
3
...

user output
2
3
3
3
3
...

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

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

correct output
2
1
3
2
2
...

user output
2
1
3
2
2
...

Test 11

Group: 3, 4, 5

Verdict: ACCEPTED

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

correct output
3
3
3
3
3
...

user output
3
3
3
3
3
...

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

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

correct output
104
422
145
93
65
...

user output
104
422
145
93
65
...

Test 20

Group: 5

Verdict:

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

correct output
57
155
38
65
98
...

user output
(empty)

Test 21

Group: 5

Verdict: ACCEPTED

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

correct output
498
498
498
498
498
...

user output
498
498
498
498
498
...

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