-
Notifications
You must be signed in to change notification settings - Fork 2
/
queen.cpp
221 lines (199 loc) · 7.5 KB
/
queen.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*************************
* queen.cpp
* editor: amo
*************************/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <queen.h>
#include <algorithm> // std::for_each
#include <stack>
#include <map>
using namespace amo;
/* -------------------------------------------------------------------- */
/* my define macro */
/* -------------------------------------------------------------------- */
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
#define CYAN "\033[36m" /* Cyan */
#define WHITE "\033[37m" /* White */
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
// Allocating and initializing Class's static data member
Queen* Queen::instance = 0;
static int** chess; //A dynamic 2D array is basically an array of pointers to arrays
int row, col;
int track;
static int ans=0;
static std::vector<int> queens;
static std::map<int, int> moves;
/* -------------------------------------------------------------------- */
/* implements */
/* -------------------------------------------------------------------- */
void collect(std::map<int,int>& map, int count, ...) {
va_list ap;
count = count*2;
va_start(ap, (count));
int key;
int value;
std::pair<std::map<int,int>::iterator,bool> ret;
for (int i=0; i<count; i=i+2) {
key = va_arg(ap, int);
value = va_arg(ap, int);
ret = map.insert(std::make_pair(key, value));
//if(ret.second) std::cout << YELLOW << "newly inserted element key:" << ret.first->first << " and value:" << ret.first->second << WHITE << std::endl;
//else std::cout << YELLOW << "existing equivalent key element key:" << ret.first->first << " and value:" << ret.first->second << WHITE << std::endl;
}
va_end(ap);
}
amo::Queen::Queen() {
std::cout << "[Queen::Queen()]:" << this << std::endl;
}
amo::Queen::~Queen() {
std::cout << "[Queen::~Queen()]:" << this << std::endl;
//delete Queen::instance;
}
Queen& amo::Queen::getInstance() {
if (instance == nullptr) {
std::cout << "[Queen::getInstance()]: going to 'instance = new Queen()'" << std::endl;
instance = new Queen();
}
if (chess == nullptr) {
std::cout << "[Queen::getInstance()]: going to 'chess = new int[][]'"<< std::endl;
row = 4;
col = 4;
chess = new int*[row];
for (int i=0; i<row; i++) {
*(chess+i) = new int[col];
//chess[i] = new int[col];
for (int j=0; j<col; j++)
*(*(chess+i)+j) = 10*(i+1)+j;
//chess[i][j] = 10*(i+1)+j;
}
}
//std::cout << "[Queen::getInstance()]: returns:" << instance << std::endl;
return *instance;
}
void amo::Queen::traverse() {
std::cout << "[Queen::traverse()]: --- Traverse begin ---" << WHITE << std::endl;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
std::cout << "[" << i <<"][" << j << "]:" << chess[i][j];
if (j==col-1) std::cout << "" << std::endl;
}
}
std::cout << "[Queen::traverse()]: --- Traverse end ---" << WHITE << std::endl;
}
void amo::Queen::traverse(int marki, int markj) {
std::cout << BLUE <<"[Queen::traverse()]: --- Traverse begin ---" << WHITE << std::endl;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (i== marki && j==markj) std::cout << GREEN << "[" << i << "][" << j << "]" << WHITE;
else std::cout << "[" << i <<"][" << j << "]";
if (j==col-1) std::cout << "" << std::endl;
}
}
std::cout << BLUE << "[Queen::traverse()]: --- Traverse end ---" << WHITE << std::endl;
}
inline bool is_answer(std::map<int,int>& map, int i, int j) {
for (std::map<int,int>::iterator it=map.begin(); it!=moves.end(); it++)
if (i==it->first && j==it->second) return true;
return false;
}
void amo::Queen::traverse(std::map<int,int>& map) {
std::cout << BLUE <<"[Queen::traverse()]: --- Traverse begin ---" << WHITE << std::endl;
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (is_answer(map, i, j)) std::cout << GREEN << "[" << i <<"][" << j << "]" << WHITE;
else std::cout << "[" << i <<"][" << j << "]";
if (j==col-1) std::cout << "" << std::endl;
}
}
std::cout << BLUE << "[Queen::traverse()]: --- Traverse end ---" << WHITE << std::endl;
}
void amo::Queen::place(int probing, int n) {
if (n != row || n != col ) {
if (chess != nullptr) {
for (int i=0;i<row;i++) delete *(chess+i);
delete chess;
}
row = n;
col = n;
chess = new int*[n];
for (int i=0;i<n;i++) {
*(chess+i) = new int[col];
for (int j=0; j<col; j++)
*(*(chess+i)+j) = 100*(i+1)+j;
}
}
if (probing >= n) {
ans += 1;
std::cout << GREEN <<"[Queen::place()]: returns when met the pass-the-end of probing which means deduced one of answer:" << ans << WHITE << std::endl;
for (std::vector<int>::iterator it=queens.begin(); it!=std::next(queens.begin(), n); it++)
collect(moves, 1, std::distance(queens.begin(), it), *it);
traverse(moves);
moves.clear();
return;
}
for (int pruning=0; pruning<col; pruning++) {
track++;
//traverse(probing, pruning);
//if (probing==n-1 && pruning==col-1) std::cout << RED <<"[Queen::place()]: track:" << track << WHITE << std::endl; //final track=4^4+4^3+4^2+4^1=340 if n=4 or track=3^3+3^2+3^1=39 if n=3
if (!check(probing, pruning)) {
//if (false) { //watching all moves
//std::cout << "[Queen::place()]: going to prune" << WHITE << std::endl;
/**
* 'prunes' when met one of dead ends of this probing at this pruning
*/
continue;
}
else { //found one of rights of this probing at this pruning and digs into the one more deeper
//std::cout << "[Queen::place()]: going to probe" << WHITE << std::endl;
if (queens.size() < n) queens.resize(n);
queens.at(probing) = pruning;
/**
* 'probes' one more deeper by making one more call stack returning back here as backtracking and proceeding to another pruning
*/
place(probing+1, n);
}
}
/**
* 'backtracks'
*/
return;
}
bool amo::Queen::check(int row, int column) {
if (row == 0) {
//std::cout << CYAN << "okay chess[" << row <<"][" << column << "]" << WHITE << std::endl;
return true;
}
for (std::vector<int>::iterator it=queens.begin(); it!=std::next(queens.begin(), row); it++) {
int placed_row = std::distance(queens.begin(), it);
int placed_column = queens.at(placed_row);
if (placed_column == column) {
//std::cout << MAGENTA << "not across, queen[" << placed_row << "][" << placed_column << "] and chess[" << row <<"][" << column << "]" << WHITE << std::endl;
return false;
}
if (std::abs(placed_column-column) == std::abs(placed_row-row)) {
//std::cout << MAGENTA << "not diagonal, queen[" << placed_row << "][" << placed_column << "] and chess[" << row <<"][" << column << "]" << WHITE << std::endl;
return false;
}
}
//std::cout << CYAN << "okay chess[" << row <<"][" << column << "]" << WHITE << std::endl;
return true;
}
int amo::Queen::answer() {
return ans;
}