CSES - Datatähti 2021 alku - Results
Submission details
Task:Ratsun reitit
Sender:jkan
Submission time:2020-10-07 15:16:58 +0300
Language:Rust
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED27
#2ACCEPTED31
#3ACCEPTED42
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.01 s1, 2, 3details
#6ACCEPTED0.01 s1, 2, 3details
#7ACCEPTED0.01 s1, 2, 3details
#8ACCEPTED0.01 s2, 3details
#9ACCEPTED0.01 s2, 3details
#10ACCEPTED0.01 s2, 3details
#11ACCEPTED0.02 s3details
#12ACCEPTED0.04 s3details
#13ACCEPTED0.04 s3details

Compiler report

warning: function is never used: `recurse_knight_moves`
  --> input/code.rs:37:4
   |
37 | fn recurse_knight_moves(
   |    ^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

Code

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

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 iterate_knight_moves(
        board: &mut Vec<Vec<Option<i32>>>,
        pos0: Move
        ) {
    let mut note: Vec<Move> = Vec::new();
    let mut queue: VecDeque<(Move, i32)> = VecDeque::from(vec![(pos0,0)]);
    let moves_vec = 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 mut moves;
    let mut pos: Move;
    let mut depth: i32;
    let bsize = board.len() as i32;

    while !queue.is_empty() { 
        match queue.pop_front().unwrap() {
            (x,y) => {pos = x; depth = y;},
        };
        moves = moves_vec.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)})
        for t in moves {
            let v = board[t.0 as usize][t.1 as usize];
            if v.unwrap_or(i32::MAX) > depth {
                board[t.0 as usize][t.1 as usize] = Some(depth);
                if !note.contains(&t) {
                    queue.push_back((t,depth+1))
                }
            }
        }
        note.push(pos);
    }
}

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);
    iterate_knight_moves(&mut board, Move(-2,-1));
    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: ACCEPTED

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 3 2
3 4 1 2 3
2 1 4 3 2
3 2 3 2 3
2 3 2 3 4

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

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

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

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

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

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

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

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

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

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

Test 8

Group: 2, 3

Verdict: ACCEPTED

input
25

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

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

Test 9

Group: 2, 3

Verdict: ACCEPTED

input
49

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

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

Test 10

Group: 2, 3

Verdict: ACCEPTED

input
50

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

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

Test 11

Group: 3

Verdict: ACCEPTED

input
75

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

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

Test 12

Group: 3

Verdict: ACCEPTED

input
99

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

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

Test 13

Group: 3

Verdict: ACCEPTED

input
100

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

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