1
Fork 0

Now prints the solution (if it exists)

This commit is contained in:
Joshua Moerman 2014-02-13 22:03:39 +01:00
parent a137e31ba0
commit 6a82323713
3 changed files with 26 additions and 15 deletions

View file

@ -7,7 +7,7 @@
namespace detail { namespace detail {
template <int W, int H, typename URNG, size_t... I> template <int W, int H, typename URNG, size_t... I>
auto random_field(URNG&& r, std::index_sequence<I...>){ auto random_field(URNG&& r, std::index_sequence<I...>){
std::uniform_int_distribution<int> dis(0, 4); std::uniform_int_distribution<int> dis(1, 2);
return create_rectangular_field<W, H>({ return create_rectangular_field<W, H>({
((void)I, dis(r))... ((void)I, dis(r))...
}); });

View file

@ -1,9 +1,15 @@
#pragma once #pragma once
#include "clusters.hpp" #include "clusters.hpp"
#include <deque>
template <typename Field> template <typename Field>
auto solve(Field const & field){ struct Solution {
std::deque<typename Field::Position> taps;
};
template <typename Field>
auto solve_impl(Field const & field, Solution<Field> & s){
for(auto&& p : field.all_positions()){ for(auto&& p : field.all_positions()){
if(!field.empty(p)) goto analyse; if(!field.empty(p)) goto analyse;
} }
@ -11,9 +17,19 @@ auto solve(Field const & field){
analyse: analyse:
for(auto&& c : all_clusters(field)){ for(auto&& c : all_clusters(field)){
if(c.size() < 2) continue; if(c.size() < 3) continue;
auto new_field = make_empty(field, c); auto new_field = make_empty(field, c);
if(solve(new_field)) return true; if(solve_impl(new_field, s)) {
s.taps.push_front(*c.begin());
return true;
}
} }
return false; return false;
} }
template <typename Field>
auto solve(Field const & field){
Solution<Field> s;
solve_impl(field, s);
return s;
}

View file

@ -9,24 +9,19 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
template <typename Field>
auto is_void(Field const & field){
for(auto&& p : field.all_positions()){
if(!field.empty(p)) return false;
}
return true;
}
int main(){ int main(){
using namespace std; using namespace std;
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
auto field = random_field<5, 5>(gen); std::cout << std::endl;
auto field = random_field<3, 3>(gen);
field.print(std::cout); field.print(std::cout);
cout << solve(field) << std::endl; auto solution = solve(field);
for(auto&& t : solution.taps){
std::cout << "(" << t.first << ", " << t.second << ")\n";
}
} }