How to check elements of a vector<vector<short> > for a condition

How would it be possible to check the correctness of the a Sudoku board for inconsistencies, especially in the event that a number is removed from the board. User defined classes have been included in the code for better description of the problem.

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
  class cell{
    bool m_occu; //occupied is shown '.'
    int m_num;

public:
    cell() : m_occu(false), m_num(0) {}
    void setMark(const int num){m_num = num; m_occu = true;}
    bool isMarked() const { return m_occu; }
    int getNum(){ return m_num;}
    friend ostream& operator << (ostream& o, const cell& c){
        if (!c.m_occu) return o << setw(2) << '-';
        return o << setw(2) << c.m_num;
    }
};

class board {
    vector<vector <cell> >m_map;
    bool col_row;

public:
    board() {
        vector<cell> a_row(9);
        col_row = false;
for (int i = 0; i < 9; ++i)
        {
            for(int j = 0; j < 9; j++)
            {
                a_row[j].setMark(j+1);
            }
            random_shuffle(a_row.begin(), a_row.end());
            m_map.push_back(a_row);
        }
    }
bool verify()
    {
        // check rows
        int compareNUM = 0;
        for(int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                compareNUM = m_map[row][col].getNum();

                // compares first element in every row with every other element in the same row.
                // However, issue is comparing compareNUM variable to itself in this loop.
                // come up with condition to handle comparison to itself. Will always return false
                // if  not handled.
                for(int iter = 8 - col; iter <= 8; iter--)
                {
                    if(compareNUM == m_map[row][iter].getNum())
                    {
                        return false;
                    }
                }

            }
        }

        //check cols 
}


The following is incomplete and I need to be able to verify both columns and rows for inconsistencies.

Below is a sample:

A B C D E F G H I
P 3 4 5 9 1 - 7 6 8
Q 6 7 8 3 4 5 1 9 2
R 9 1 2 6 7 8 4 3 5
S 2 3 4 8 9 1 6 5 7
T 5 6 7 2 3 4 9 8 1
U 8 9 1 5 6 7 3 2 4
V 1 2 3 7 8 9 5 4 6
W 4 5 6 1 2 3 8 7 9
X - 8 9 4 5 6 2 1 3
> verify
- Found inconsistency in row P...
- Found inconsistency in row X...
- Found inconsistency in column A...
- Found inconsistency in column F...
- Found inconsistency in component starting at row P and column D...
- Found inconsistency in component starting at row V and column A...
> quit
Bye...


Last edited on
I don't understand what is inconsistent about the puzzle. It's solvable, and has exactly one solution.

If by verify you mean "check for a complete solution", I suppose I understand the first four lines of output, but you still need to explain what's inconsistent about row P and column D, row V and column A.
My apologies for the lack of clarity, by inconsistency I mean that the program must be able to check to see which block of 3 x 3 has a missing number.
Part of the code includes a function that replaces a cell of type cell with value of 0 which is then shown as "-" via ostream.

For example: if the "-" was in the 3x3 block from Row P, Col D to Row V, Col A, then then the program notes the inconsistency or if there is a number repetition in the same 3x3 block then once again program says inconsistency.
No worries. :)

Add all the values into a set, then check the size of the set.
1
2
3
4
5
6
7
8
auto s = std::set <short>{};
while (is_valid(i) && s.insert(i).second)  { 
  // ...advance i to the next element you need to check
}

if (! is_valid(i)) /* ...node consistency constraint violation at row x, y */
if (s.size() < n) /* ...alldifferent constraint violation at row x, y */
else { /* no error */ }

Last edited on
I am not quite familiar with using sets, is there a way I can make the function with for loops and booleans based on the classes I have made?

-Thanks
Topic archived. No new replies allowed.