1 //  Copyright (c) 2011 Vinay C Amatya
2 //  Copyright (c) 2011 Bryce Lelbach
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
5 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(HPX_9FEC203D_0AAB_4213_BA36_456BE578ED3D)
8 #define HPX_9FEC203D_0AAB_4213_BA36_456BE578ED3D
9 
10 #include <hpx/hpx.hpp>
11 #include <hpx/include/components.hpp>
12 #include <hpx/include/actions.hpp>
13 
14 #include <cstddef>
15 #include <vector>
16 
17 namespace nqueen
18 {
19     typedef std::vector<std::size_t> list_type;
20 
21 namespace server
22 {
23     class board : public hpx::components::component_base<board>
24     {
25     private:
26         list_type list_;
27         std::size_t count_;
28 
29         // here board is a component
30 //         friend class hpx::serialization::access;
31 //         template <class Archive>
32 //         void serialize(Archive& ar, const unsigned int version)
33 //         {
34 //             ar & size_;
35 //             ar & list_;
36 //             ar & level_;
37 //             ar & count_;
38 //         }
39 
40     public:
board()41         board() : list_(0), count_(0)
42         {}
43 
board(list_type const & list,std::size_t,std::size_t)44         board(list_type const& list, std::size_t, std::size_t)
45           : list_(list), count_(0)
46         {}
47 
48         ~board() = default;
49 
50 
init_board(std::size_t size)51         void init_board(std::size_t size)
52         {
53             std::size_t i = 0;
54             while(i!=size)
55             {
56                 list_.push_back(size);
57                 ++i;
58              }
59          }
60 
check_board(list_type const & list,std::size_t level)61         bool check_board(list_type const& list, std::size_t level)
62         {
63             for (std::size_t i = 0; i < level; ++i)
64             {
65                 if ((list.at(i) == list.at(level)) ||
66                     (list.at(level) - list.at(i) == level - i) ||
67                     (list.at(i) - list.at(level) == level - i))
68                 {
69                     return false;
70                 }
71             }
72             return true;
73         }
74 
access_board()75         list_type access_board()
76         {
77             return list_;
78         }
79 
update_board(std::size_t pos,std::size_t val)80         void update_board(std::size_t pos, std::size_t val)
81         {
82             list_.at(pos) = val;
83         }
84 
clear_board()85         void clear_board()
86         {
87             board::list_.clear();
88         }
89 
solve_board(list_type const & list,std::size_t size,std::size_t level,std::size_t col)90         std::size_t solve_board(list_type const& list, std::size_t size,
91             std::size_t level, std::size_t col)
92         {
93             board b(list, size, level);
94 
95             if (level == size)
96             {
97                 return 1;
98             }
99             else if (level == 0)
100             {
101                 b.update_board(level, col);
102                 if (b.check_board(b.access_board(), level))
103                 {
104                     b.count_ +=
105                         solve_board(b.access_board(), size, level + 1, col);
106                 }
107             }
108             else
109             {
110                 for (std::size_t i = 0; i < size; ++i)
111                 {
112                     b.update_board(level, i);
113                     if (b.check_board(b.access_board(), level))
114                     {
115                         b.count_ +=
116                             solve_board(b.access_board(), size, level + 1, col);
117                     }
118                 }
119             }
120             return b.count_;
121         }
122 
123         HPX_DEFINE_COMPONENT_ACTION(board, init_board, init_action);
124         HPX_DEFINE_COMPONENT_ACTION(board, access_board, access_action);
125         HPX_DEFINE_COMPONENT_ACTION(board, update_board, update_action);
126         HPX_DEFINE_COMPONENT_ACTION(board, check_board, check_action);
127         HPX_DEFINE_COMPONENT_ACTION(board, solve_board, solve_action);
128         HPX_DEFINE_COMPONENT_ACTION(board, clear_board, clear_action);
129     };
130 }}
131 
132 // Declaration of serialization support for the board actions
133 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::init_action,
134     board_init_action);
135 
136 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::check_action,
137     board_check_action);
138 
139 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::access_action,
140     board_access_action);
141 
142 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::update_action,
143     board_update_action);
144 
145 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::solve_action,
146     board_solve_action);
147 
148 HPX_REGISTER_ACTION_DECLARATION(nqueen::server::board::clear_action,
149     board_clear_action);
150 
151 #endif    // HPX_9FEC203D_0AAB_4213_BA36_456BE578ED3D
152