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
|
#include <iostream>
#include <array>
#include <random>
#include <algorithm>
#include <chrono>
#include <vector>
#include <thread>
#include <future>
using namespace std;
template< int N >
class NQueenSolver
{
public:
NQueenSolver(){ init(); }
bool solve()
{
int tries {0};
while( !trySolveRandom() && tries<maxTries )
{
++tries;
init();
}
return tries<maxTries ? true : false ;
}
void print()
{
if( N>30 )
{
cout << "Solution found!";
return;
}
for( int i {0} ; i<N ; ++i )
{
for( int j {0} ; j<N ; ++j ) cout << (board[i]==j ? "1" : "0") << " ";
cout << endl;
}
}
private:
void init()
{
iota( board.begin() , board.end() , 0 );
auto seed = chrono::system_clock::now().time_since_epoch().count();
shuffle( board.begin() , board.end() , default_random_engine(seed) );
findQueensInConflict();
}
void findQueensInConflict()
{
queensInConflict.clear();
for( int i {0} ; i<N ; ++i )
{
if( findQueensInConflict(i)>0 ) queensInConflict.push_back(i);
}
}
int getNumberOfQueensInConflict( int i , int j )
{
return i!=j ? findQueensInConflict(i) + findQueensInConflict(j) : 0;
}
int findQueensInConflict( int i )
{
int conflicts {0};
for( int a {1} ; a<N ; ++a )
{
if( (i-a) >= 0 )
{
if( abs(board[i] - board[i-a]) == a ) ++conflicts;
}
if( (i+a) < N )
{
if( abs(board[i] - board[i+a]) == a ) ++conflicts;
}
}
return conflicts;
}
bool trySolveRandom()
{
size_t tries {0};
while( queensInConflict.size()>0 && tries<10*queensInConflict.size() )
{
int ri = getRandom(0,queensInConflict.size()-1);
int rj = getRandom(0,queensInConflict.size()-1);
if( ri == rj ) continue;
auto beforeSwap { getNumberOfQueensInConflict(queensInConflict[ri],queensInConflict[rj]) };
swap( board[queensInConflict[ri]] , board[queensInConflict[rj]] );
auto afterSwap { getNumberOfQueensInConflict(queensInConflict[ri],queensInConflict[rj]) };
if( afterSwap >= beforeSwap )
{
swap( board[queensInConflict[ri]] , board[queensInConflict[rj]] );
tries++;
}
else
{
tries = 0;
findQueensInConflict();
}
}
cout << "=" << queensInConflict.size() << "\n";
return queensInConflict.size() == 0 ? true : false ;
}
int getRandom( int min , int max )
{
mt19937 mt {random_device{}()};
uniform_int_distribution<int> dist {min,max};
return dist(mt);
}
array<int,N> board;
vector<int> queensInConflict;
const int maxTries {1000};
};
using NQueenSolverM = NQueenSolver<30>;
int main()
{
NQueenSolverM solver;
auto start {chrono::steady_clock::now()};
auto future = async(launch::async,&NQueenSolverM::solve,&solver);
auto result = future.get();
cout << "\nProceed in " << chrono::duration<double>(chrono::steady_clock::now()-start).count() << " seconds" << endl;
if( result ) solver.print();
else cout << "Solution not found." << endl;
return 0;
}
| |