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

#include <vector>
#include <iostream>

using namespace std;

bool Checked[10];
void ResetChecked()
{
    for (int i = 0; i < 10; ++i)
        Checked[i] = false;
}

bool IsValidSudoku(char board[][10]) 
{
    //Validate rows
    for (int i = 0; i < 9; ++i)
    {
        ResetChecked();
        for (int j = 0; j < 9; ++j)
            if (board[i][j] != '.')
            {
                int index = board[i][j] - '0';
                //number collision
                if (Checked[index]) return false;

                Checked[index] = true;
            }
    }

    //Validate columns
    for (int j = 0; j < 9; ++j)
    {
        ResetChecked();
        for (int i = 0; i < 9; ++i)
            if (board[i][j] != '.')
            {
                int index = board[i][j] - '0';
                //number collision
                if (Checked[index]) return false;

                Checked[index] = true;
            }
    }

    //Validate sub-boxes
    for (int i = 0; i < 9; ++i)
    {
        ResetChecked();
        for (int j = 0; j < 9; ++j)
		{
			int r = i/3 *3 + j/3;
			int c = i%3 *3 + j%3;
			if (board[r][c] != '.')
            {
                int index = board[r][c] - '0';
                //number collision
                if (Checked[index]) 
				{
					return false;
				}
				
                Checked[index] = true;
            }
			
		}
	}

    return true;
}


int main(int argc, char** argv)
{

    
    char a[9][10] = {"53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"};
    bool isValid = IsValidSudoku(a);
	
	cout << isValid;
    return 0;
}
View Program Text


Test Status