CSES - Datatähti 2021 alku - Results
Submission details
Task:Ratsun reitit
Sender:jkan
Submission time:2020-10-04 22:16:58 +0300
Language:Rust
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#20.01 s1, 2, 3details
#30.01 s1, 2, 3details
#40.01 s1, 2, 3details
#50.01 s1, 2, 3details
#60.01 s1, 2, 3details
#70.01 s1, 2, 3details
#80.01 s2, 3details
#90.01 s2, 3details
#100.01 s2, 3details
#110.04 s3details
#120.09 s3details
#130.09 s3details

Code

use std::io::{BufRead, BufReader};

fn pretty_print_matrix(m: Vec<Vec<Option<i32>>>) -> String {
    m.iter()
        .map(|v|{
            v.iter()
                .map(|i|{
                    match i {
                        None => "X".to_string(),
                        Some(i) => i.to_string(),
                    }
                })
            .fold("".to_string(), |acc, el| {
                match acc.len() {
                    0 => el,
                    _ => format!("{} {}", acc, el)
                }
            })
        })
    .fold("".to_string(), |acc,el| { 
        match acc.len() {
            0 => el,
            _ => format!("{}{}{}", acc, '\n', el)
        }
    })
}

#[derive(PartialEq, Copy, Clone, Debug)]
struct Move(i32, i32);
impl Eq for Move {}

fn chained_comp(b: i32,v: i32,a: i32) -> bool {
    b < v && v < a
}

fn recurse_knight_moves(
        board: &mut Vec<Vec<Option<i32>>>,
        pos: Move,
        depth: i32,
        note: &mut Vec<Move>) {
    if note.contains(&pos) {
        return ();
    }
    let mut moves = vec![
        Move(-2,-1),
        Move(-1,-2),
        Move(1,-2),
        Move(2, -1),
        Move(2,1),
        Move(1,2),
        Move(-1,2),
        Move(-2,1),
    ];
    let bsize = board.len() as i32;
    moves = moves.iter()
        .map(|t|{Move(t.0+pos.0, t.1+pos.1)}) 
        .filter(|t|{chained_comp(-1,t.0,bsize) && chained_comp(-1,t.1,bsize)})
        // // .filter(|t|{!note.contains(t)})
        .collect::<Vec<Move>>();
    let mut done = Vec::new();
    for t in moves {
        let v = board[t.0 as usize][t.1 as usize];
        if v.unwrap_or(9999) > depth {
            board[t.0 as usize][t.1 as usize] = Some(depth);
            done.push(t)
        }
    }
    // println!("{}\n",pretty_print_matrix(board.clone()));
    note.push(pos);
    for t in done {
        recurse_knight_moves(board, t, depth+1, note); 
    }        
}

fn main() {
    let mut input = BufReader::new(std::io::stdin());
    let mut line = "".to_string();
    input.read_line(&mut line).unwrap();
    line.pop();
    let n: i32 = line.parse().unwrap();
    let mut board = Vec::new();
    for _ in 0..n {
        let mut tmp = Vec::new();
        for _ in 0..n {
            tmp.push(None)
        }
        board.push(tmp)
    }
    board[0][0] = Some(0);
    let mut note = Vec::new();

    recurse_knight_moves(&mut board,Move(0,0),1,&mut note);
    println!("{}",pretty_print_matrix(board));
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
4

correct output
0 3 2 5 
3 4 1 2 
2 1 4 3 
5 2 3 2 

user output
0 3 2 5
3 4 1 2
2 1 4 3
5 2 3 2

Test 2

Group: 1, 2, 3

Verdict:

input
5

correct output
0 3 2 3 2 
3 4 1 2 3 
2 1 4 3 2 
3 2 3 2 3 
2 3 2 3 4 

user output
0 3 2 5 2
3 4 1 2 3
2 1 6 3 2
3 2 3 2 3
2 3 2 5 4

Test 3

Group: 1, 2, 3

Verdict:

input
6

correct output
0 3 2 3 2 3 
3 4 1 2 3 4 
2 1 4 3 2 3 
3 2 3 2 3 4 
2 3 2 3 4 3 
...

user output
0 3 2 7 2 3
3 4 1 2 3 6
2 1 6 3 2 3
3 2 3 2 3 6
2 3 2 5 4 3
...

Test 4

Group: 1, 2, 3

Verdict:

input
7

correct output
0 3 2 3 2 3 4 
3 4 1 2 3 4 3 
2 1 4 3 2 3 4 
3 2 3 2 3 4 3 
2 3 2 3 4 3 4 
...

user output
0 3 2 7 2 3 20
3 4 1 2 3 6 11
2 1 6 3 2 3 6
3 2 3 2 3 8 11
2 3 2 7 4 3 6
...

Test 5

Group: 1, 2, 3

Verdict:

input
8

correct output
0 3 2 3 2 3 4 5 
3 4 1 2 3 4 3 4 
2 1 4 3 2 3 4 5 
3 2 3 2 3 4 3 4 
2 3 2 3 4 3 4 5 
...

user output
0 3 2 9 2 3 8 9
3 4 1 2 3 8 9 8
2 1 10 3 2 3 16 7
3 2 3 2 3 6 7 8
2 3 2 9 4 3 6 9
...

Test 6

Group: 1, 2, 3

Verdict:

input
9

correct output
0 3 2 3 2 3 4 5 4 
3 4 1 2 3 4 3 4 5 
2 1 4 3 2 3 4 5 4 
3 2 3 2 3 4 3 4 5 
2 3 2 3 4 3 4 5 4 
...

user output
0 3 2 9 2 3 10 19 20
3 4 1 2 3 10 17 20 19
2 1 8 3 2 3 10 17 18
3 2 3 2 3 8 19 16 17
2 3 2 7 4 3 10 17 16
...

Test 7

Group: 1, 2, 3

Verdict:

input
10

correct output
0 3 2 3 2 3 4 5 4 5 
3 4 1 2 3 4 3 4 5 6 
2 1 4 3 2 3 4 5 4 5 
3 2 3 2 3 4 3 4 5 6 
2 3 2 3 4 3 4 5 4 5 
...

user output
0 3 2 11 2 3 12 15 28 25
3 4 1 2 3 12 15 16 25 26
2 1 10 3 2 3 12 15 16 27
3 2 3 2 3 10 15 16 17 24
2 3 2 9 4 3 10 15 16 17
...

Test 8

Group: 2, 3

Verdict:

input
25

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 25 2 3 26 65 66 67 70 69...

Test 9

Group: 2, 3

Verdict:

input
49

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 49 2 3 50 125 126 127 13...

Test 10

Group: 2, 3

Verdict:

input
50

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 51 2 3 52 123 124 125 12...

Test 11

Group: 3

Verdict:

input
75

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 75 2 3 76 189 186 187 19...

Test 12

Group: 3

Verdict:

input
99

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 99 2 3 100 249 246 247 2...

Test 13

Group: 3

Verdict:

input
100

correct output
0 3 2 3 2 3 4 5 4 5 6 7 6 7 8 ...

user output
0 3 2 101 2 3 102 253 250 251 ...