File: sudoku-solve.H

package info (click to toggle)
gnudoq 0.94-2.2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 2,732 kB
  • ctags: 240
  • sloc: cpp: 2,696; makefile: 3
file content (341 lines) | stat: -rw-r--r-- 6,291 bytes parent folder | download | duplicates (4)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#pragma once
#ifndef SUDOKU_SOLVE_H
#define SUDOKU_SOLVE_H

//#include <list>
#include <vector>
#include "sudoku.H"
#include <iostream>
// just a bunch of data structure declarations for the new solving algorithm...
// NOTE: 'box' means 1x1 square, 'bigbox' means 3x3 square, 'board' is the whole 9x9 shebang.
namespace sudoku
{
	enum CONSTANTS
	{
		ROWS = 9,
		COLS = ROWS,
		BIGBOXES = ROWS,
		BOXES = ROWS * COLS,
	};

	enum MOVE_KIND
	{
		// setup move: this move is part of the problem and can't possibly be undone
		MOVE_FIXED,
		// forced move: not to make this move would be incorrect
		MOVE_FORCED,
		// move predicted to be likely to be correct, not purely random
		MOVE_LIKELY,
		// randomised move: randomly chosen within the constraints of the current position
		MOVE_RANDOM,
	};

	struct rcb
	{
		int row, col, bigbox;
	};

	rcb RCB(unsigned int index);
	unsigned int Index(const rcb& rcb);
	inline unsigned int Index(unsigned int row, unsigned int col)
	{
		return col + row * COLS;
	}

	template<typename digit, digit digits> class Possible
	{
	protected:
		digit count;
		bool val[digits];
	public:
		void clear()
		{
			count = 0;
			for (digit i = 0; i < digits; ++i)
				val[i] = false;
		}
		Possible()
		{
			all();
		}

		/* add digit to possibilities, if not already on there. Returns whether the digit was
		 * previously present. */
		bool push(digit n)
		{
			if (!val[n])
			{
				val[n] = true;
				++count;
				return false;
			}
			return true;
		}
		/* remove digit from possibilities, return true if it was actually a member. */
		bool pop(digit n)
		{
			if (val[n])
			{
				val[n] = false;
				--count;
			}
			return n;
		}
		void all()
		{
			for (count = 0; count < digits; ++count)
				val[count] = true;
		}

		digit num() const
		{
			return count;
		}

		bool operator[](digit n) const
		{
			return val[n];
		}

		Possible operator&(const Possible& rhs) const
		{
			Possible tmp;
			tmp.clear();
			for (digit i = 0; i < digits; ++i)
			{
				tmp.val[i] = val[i] && rhs[i];
				if (tmp[i])
					++tmp.count;
			}
			return tmp;
		}
		Possible& operator&=(const Possible& rhs)
		{
			for (digit i = 0; i < digits; ++i)
			{
				if (val[i])
				{
					val[i] = rhs[i];
					if (!val[i])
						--count;
				}
			}
			return *this;
		}
		Possible operator|(const Possible& rhs) const
		{
			Possible tmp;
			tmp.clear();
			for (digit i = 0; i < digits; ++i)
			{
				tmp.val[i] = val[i] || rhs[i];
				if (tmp[i])
					++tmp.count;
			}
			return tmp;
		}
		Possible& operator|=(const Possible& rhs)
		{
			for (digit i = 0; i < digits; ++i)
			{
				if (!val[i])
				{
					val[i] = rhs[i];
					if (val[i])
						++count;
				}
			}
			return *this;
		}
	};


/*	struct group
	{
		Possible<int, 9> unset;
		stack<int, 9> positions[9];

		bool Solveable()
		{
			// for every unset digit, make sure that there are spaces available to
			// place that digit
			for (size_t n = 0; n < 9; ++n)
			{
				if (unset[n])
				{
					if (positions[n].size() == 0)
						return false;
				}
			}
			return true;
		}
	};*/

	class Board
	{
	public:
		struct choice
		{
			// -1 = unset, 0..8: digits 1-9
			int val;
			Possible<int, 9> poss;

			choice()
			{
				val = -1;
			}
		};

	protected:
		choice item[BOXES];
		Possible<int, 9> row[ROWS];
		Possible<int, 9> col[COLS];
		Possible<int, 9> bigbox[BIGBOXES];

		unsigned int filled;
	public:
		Board()
		{
			filled = 0;
		}
		bool UnSet(unsigned int index);
		bool Set(unsigned int index, int n);
		const choice& operator[](unsigned int index) const
		{
			return item[index];
		}

		unsigned int Blanks()
		{
			return BOXES - filled;
		}
		unsigned int Filled()
		{
			return filled;
		}

		// this isn't guaranteed - even if true, it might not be solveable. but if false, it's certainly not solveable
		bool Solveable();

		template<bool debug> void Print();
	};

	struct move
	{
		move()
		{}

		// convert from old 'attempt' struct.
		move(const Sudoku::attempt& old, MOVE_KIND type)
		{
			value = old.value;
			tries = old.tries;
			index = old.index;
			kind = type;
		}

		operator Sudoku::attempt() const
		{
			Sudoku::attempt old;
			old.value = value;
			old.tries = tries;
			old.index = index;
			rcb pos = RCB(index);
			old.row = pos.row;
			old.col = pos.col;
			old.box = pos.bigbox;
			return old;
		}

		unsigned int index;
		/*unsigned int row;
		unsigned int col;
		unsigned int bigbox;*/
		MOVE_KIND kind;
		// used for likely and random moves:
		// value assigned
		unsigned int value;
		// attempts of values at this location (once we hit 9 and can't solve it, we need to go back)
		unsigned int tries;

		bool Do(Board& b)
		{
			return b.Set(index, value);
		}
		bool Undo(Board& b)
		{
			return b.UnSet(index);
		}
		
		void Print();
	};

	bool ForcedPossibilities(Board& board, std::vector<move>& moves);
	bool FillOptions(Board& board, std::vector<move>& moves, const std::vector<unsigned int>& indices);
	bool ForcedOptions(Board& board, std::vector<move>& moves);
	// solve given board with some moves already performed.
	bool Solve(Board& board, std::vector<move>& moves);
	// solve given board
	inline bool Solve(Board& board)
	{
		std::vector<move> moves;
		return Solve(board, moves);
	}

	template<bool debug> void Board::Print()
	{
		const char top[]    = "    A   B   C   D   E   F   G   H   I  \n";
		const char across[] = "  +---+---+---#---+---+---#---+---+---+\n";
		const char boss[]   = "  +===+===+===#===+===+===#===+===+===+\n";
		
		std::cout << top;
		for (int r = 0; r < 9; ++r)
		{
			if (r % 3 == 0)
				std::cout << boss;
			else
				std::cout << across;
			
			if (!debug)
				std::cout << r + 1;
			else
				std::cout << r + 1 << " ";
			
			for (int c = 0; c < 9; ++c)
			{
				if (!debug)
					std::cout << " | ";
				else
					std::cout << "|";
				
				if (item[r * 9 + c].val >= 0)
					std::cout << item[r * 9 + c].val + 1;
				else
					std::cout << " ";
				
				if (debug)
					std::cout << ":" << item[r * 9 + c].poss.num();
				
			}
			if (debug)
				std::cout << "| " << row[r].num() << "\n";
			else
				std::cout << " |\n";
		}
		std::cout << boss;
		
		if (debug)
		{
			std::cout << " ";
			for (int c = 0; c < 9; ++c)
			{
				std::cout << "   ";
				std::cout << col[c].num();
			}
			std::cout << " |\n";
		}
	}
}


#endif