-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.h
83 lines (75 loc) · 2.42 KB
/
grid.h
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
/*This program uses my own implementation of the backtracking
algorithm described in this wikipedia article:
https://en.wikipedia.org/wiki/Sudoku_solving_algorithms
*/
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <cassert>
#include <utility>
const int ROW_FACTOR = 9;
const int CONNECT_NUM_ROWS = 9;
const int RP_MARK_I = 0;
const int RP_MARK_II = 3;
const int RP_MARK_III = 6;
const int EXHAUSTED = 10;
class Grid {
private:
struct Node {
Node() {}
Node(int datum_in, Node * top_in, Node * bottom_in,
Node * left_in, Node * right_in)
: datum(datum_in), top(top_in), bottom(bottom_in),
left(left_in), right(right_in) {}
int datum;
Node * top;
Node * bottom;
Node * left;
Node * right;
};
void push_back(int & datum_in, bool &start_row);
void connect_rows();
std::vector<Node *> row_ptrs;
Node * bottom_right;
public:
void make_grid(std::ifstream &in_file);
void print_grid();
void output_to_file(std::ofstream &out_file);
void solve();
~Grid();
class Iterator {
friend class Grid;
public:
Iterator();
Iterator(Node * node_in);
Iterator& operator++();
Iterator& operator--();
Iterator& up();
Iterator& down();
bool r_safe();
bool l_safe();
int operator*();
bool operator==(Iterator rhs);
bool operator!=(Iterator rhs);
void fill_value(int new_datum);
bool get_direction();
void change_direction();
private:
Node * node_ptr;
bool direction;
};
Iterator begin(const int row);
Iterator end();
private:
std::vector<Iterator> empty_nodes;
bool check_row(Iterator &iter);
bool check_col(Iterator &iter);
bool check_box(Iterator &iter);
void determine_box_row(Grid::Iterator &iter);
bool scan_box_row(Iterator &iter, Iterator &original);
std::pair<bool, bool> move_around_box(Iterator &iter, Iterator &orig);
void move_iterator(Iterator &iter);
void make_empty_cell_list();
};