Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <set>
- using namespace std;
- int cnt = 0;
- class Queen{
- public:
- int col, row;
- Queen* nb;
- bool canAttack(int Col, int Row);
- bool advance();
- Queen(int Col, Queen* Nb){
- col = Col;
- nb = Nb;
- row = 1;
- }
- ~Queen(){
- if(nb)
- delete nb;
- }
- bool findSolution();
- void print();
- };
- bool Queen::canAttack(int Col, int Row){
- if(Row == row)
- return true;
- if(abs(Row - row) == abs(Col - col))
- return true;
- if(nb)
- return nb->canAttack(Col, Row);
- return false;
- }
- bool Queen::advance(){
- if(row < 8){
- row++;
- return this->findSolution();
- }
- if(!nb)
- return false;
- if(nb->advance()){
- row = 1;
- return this->findSolution();
- }
- return false;
- }
- bool Queen::findSolution(){
- if(!nb)
- return true;
- while(nb->canAttack(col, row))
- if(!this->advance())
- return false;
- return true;
- }
- void Queen::print(){
- if(this->findSolution()){
- cnt++;
- }
- }
- int main(){
- Queen* lastQueen = NULL;
- set<Queen*> s;
- for(int i = 1; i < 9; i++){
- lastQueen = new Queen(i, lastQueen);
- lastQueen->findSolution();
- }
- while(lastQueen->findSolution()){
- lastQueen->print();
- lastQueen->advance();
- }
- cout << cnt << endl;
- delete lastQueen;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement