This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queens.cpp
164 lines (139 loc) · 4.29 KB
/
queens.cpp
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
161
162
163
164
//--------------------------------------------------------------------------
// File: queens.cpp
//
// Created by: Joel S. McCance
// Creation date: Thu Feb 24 12:54:40 2011
// Last modified: Mon Feb 28 19:04:13 2011
//
//--------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <unistd.h>
#include "QState.h"
//--------------------------------------------------------------------------
// Operation prototypes
//--------------------------------------------------------------------------
void hillClimbSearch(QState& startState);
/*
Takes a QState and performs a basic hill-climbing search on it until
either a goal-state is reached or a plateau is reached.
*/
void printUsage( );
/*
Prints usage instructions to std::cout
*/
//--------------------------------------------------------------------------
// Program Main
//--------------------------------------------------------------------------
int main(int argc, char* argv[])
{
//... Option flags
bool useRestarts = false;
bool quietMode = false;
//... Program parameters
QState state;
unsigned int boardDimension = 8;
int restartCount = 0;
//... Process command-line options
bool doneGettingOptions = false;
while (not doneGettingOptions)
{
char optCh = getopt(argc, argv, "qrd:?");
if (optCh == -1)
{
doneGettingOptions = true;
}
else
{
switch (optCh)
{
case 'q':
quietMode = true;
break;
case 'r':
useRestarts = true;
break;
case 'd':
boardDimension = atoi(optarg);
if (boardDimension == 2 or boardDimension == 3)
{
std::cerr <<
"The 2- and 3-queens problems have zero solutions."
<< std::endl;
exit(1);
}
break;
case '?':
default:
printUsage( );
exit(1);
}
}
}
//... Run the hill-climbing search
do
{
state = QState(boardDimension);
hillClimbSearch(state);
++restartCount;
}
while (state.getScore( ) > 0 and useRestarts);
//... Report results
if (not quietMode)
{
state.printBoard( );
}
if (useRestarts)
{
std::cout << "Restarts required: " << restartCount << std::endl;
}
else
{
std::cout << "Attacking pairs: " << state.getScore( ) << std::endl;
}
return 0;
}
//--------------------------------------------------------------------------
// Operation Definitions
//--------------------------------------------------------------------------
void hillClimbSearch(QState& state)
{
bool atPlateau = false;
while (state.getScore( ) > 0 and not atPlateau)
{
std::vector<QState> successors;
state.generateSuccessors(successors);
//... Determine the smallest successor
QState smallest = *successors.begin( );
for (std::vector<QState>::iterator it = successors.begin( );
it != successors.end( );
++it)
{
if (it->getScore( ) < smallest.getScore( ))
{
smallest = *it;
}
}
//... Check for plateaux.
// If we're not at one, set state to the smallest successor.
if (smallest.getScore( ) >= state.getScore( ))
{
atPlateau = true;
}
else
{
state = smallest;
}
}
}
//--------------------------------------------------------------------------
void printUsage( )
{
std::cout << "Usage: queens [-rcq?] [-d N]" << std::endl;
std::cout << "\t-r\tUse random restarts to find solution" << std::endl;
std::cout << "\t-q\tSuppress printing the board and number of" << std::endl;
std::cout << "\t \tattacking queens" << std::endl;
std::cout << "\t-d N\tSets the board dimension and number of" << std::endl;
std::cout << "\t \tqueens to N (Default = 8)" << std::endl;
std::cout << "\t-?\tPrint this usage message" << std::endl;
}