Summary Files Commits Branches @commit @diff @stats
path: root / Year 4 / Artificial Intelligence / hw02 / src / main.rs
blob: 970a04c788616ead6ebea352c676963db992752d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![allow(non_snake_case)]
use text_io::read;
use rand::rngs::ThreadRng;
use std::fmt::{Display, Formatter, Result};
use std::time::Instant;

pub const MAXN: usize = 10010;

#[derive(Debug)]
pub struct Puzzle {
    in_col:  [usize; MAXN],       // Позиция в колона
    per_row: [i32; MAXN],         // Бройка на ред
    per_d1:  [i32; 2 * MAXN - 1], // Бройка на главен диагонал
    per_d2:  [i32; 2 * MAXN - 1], // Бройка на втори диагонал

    N: usize,
    rng: ThreadRng,
}

impl PartialEq for Puzzle {
    fn eq(&self, other: &Puzzle) -> bool {
        (self.in_col == other.in_col) &&
        (self.per_row == other.per_row) &&
        (self.per_d1 == other.per_d1) &&
        (self.per_d2 == other.per_d2) &&
        (self.N == other.N)
    }
}

mod helper;
mod cell;
mod alg;

impl Display for Puzzle {
    fn fmt(&self, f: &mut Formatter) -> Result {
        for row in 0..self.N {
            for col in 0..self.N {
                if self.in_col[col] == row {
                    write!(f, "*")?;
                }
                else {
                    write!(f, "_")?;
                }
                if col < self.N - 1 {
                    write!(f, " ")?;
                }
            }
            writeln!(f, "")?;
        }
        Ok(())
    }
}

fn main() {
    let N: usize = read!();
    if N == 2 || N == 3 {
        println!("-1");
        return;
    }
    let mut task = Puzzle::new(N);

    let start = Instant::now();
    task.solve();
    let elapsed = start.elapsed();

    if N > 100 {
        println!("{:.2}", elapsed.as_secs_f64());
    }
    else {
        println!("{}", task);
    }
}