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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::iter;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct NQueens {
    side_length: usize,
}

impl NQueens {
    pub fn new(side_length: usize) -> NQueens {
        NQueens { side_length }
    }

    pub fn solve(&self) -> Solutions {
        (0..self.side_length).fold(
            Solutions(vec![Rows(Vec::with_capacity(self.side_length))]),
            |acc, row| self.add_new_layer(&acc, &Row(row)),
        )
    }

    fn add_new_layer(
        &self,
        &Solutions(ref previous_solutions): &Solutions,
        row: &Row,
    ) -> Solutions {
        let rows = previous_solutions
            .iter()
            .flat_map(|previous_solution| {
                (0..self.side_length)
                    .filter_map(|col_num| {
                        let column = Column(col_num);
                        if is_safe(previous_solution, row, &column) {
                            let mut cloned_data = previous_solution.0.clone();
                            cloned_data.push(column);
                            Some(Rows(cloned_data))
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<Rows>>()
            })
            .collect();
        Solutions(rows)
    }
}

/// Check to see if a given Row and Column are safe to use
/// in the context of the provided existing board configuration
/// of other Queens
///
/// Note that this only makes sense when the board configuration is for
/// Queen pieces because of the check that we do (row, col, diagonals).
fn is_safe(&Rows(ref solution_rows): &Rows, &Row(row): &Row, &Column(column): &Column) -> bool {
    solution_rows.iter().enumerate().all(
        |(solution_row, &Column(solution_column))| {
            // Test for same column
            column != solution_column &&
            // Test for same row
            row != solution_row &&
            // Test for same NE-SW diagonal
                column as isize + row as isize !=
                    solution_column as isize + solution_row as isize &&
            // Test for same NW-SE diagonal
                column as isize - row as isize !=
                    solution_column as isize - solution_row as isize
        },
    )
}


#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Solutions(Vec<Rows>);

impl Solutions {
    pub fn render(&self) -> Vec<String> {
        self.0
            .iter()
            .map(|&Rows(ref columns)| {
                let board_slots = columns.len() ^ 2;
                let mut s = String::with_capacity(board_slots + columns.len());
                for &Column(q_column) in columns.iter() {
                    let mut row_vec: Vec<char> =
                        iter::repeat('_').take(columns.len() + 1).collect();
                    row_vec[q_column] = '〇';
                    row_vec[columns.len()] = '\n';
                    let row_s: String = row_vec.iter().collect();
                    s.push_str(&row_s);
                }
                s
            })
            .collect()
    }

    pub fn data(&self) -> &Vec<Rows> {
        &self.0
    }
}


#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rows(Vec<Column>);

impl Rows {
    pub fn data(&self) -> &Vec<Column> {
        &self.0
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Column(usize);

impl Column {
    pub fn data(&self) -> usize {
        self.0
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Row(usize);

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_nqueens() {
        let solutions = NQueens::new(8).solve();
        let rendered = solutions.render();
        for render in rendered.iter() {
            println!("{}", render);
        }

        fn build_checking_tuple(
            (row_idx, &Column(col)): (usize, &Column),
        ) -> (usize, isize, isize) {
            (
                col,
                row_idx as isize + col as isize,
                row_idx as isize - col as isize,
            )
        }

        for solution in solutions.data() {
            let checker: Vec<_> = solution
                .data()
                .iter()
                .enumerate()
                .map(build_checking_tuple)
                .collect();
            for (row_idx, &Column(column)) in solution.data().iter().enumerate() {
                let checker_without_current = {
                    let mut cloned = checker.clone();
                    cloned.remove(row_idx);
                    cloned
                };
                assert!(checker_without_current.iter().all(|&checking_tuple| {
                    build_checking_tuple((row_idx, &Column(column))) != checking_tuple
                }));
            }
        }
    }
}