-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_sudoku_leetcode.cpp
More file actions
66 lines (61 loc) · 1.42 KB
/
validate_sudoku_leetcode.cpp
File metadata and controls
66 lines (61 loc) · 1.42 KB
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
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
bool validate_sudoku(char board[9][9])
{
unordered_set<string> records;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]=='.')
continue;
//generate unique strings row,col and 3x3 block
string row="";
row+=board[i][j];
row+=" rows ";
row+=i;
string col="";
col=board[i][j];
col+=" col ";
col+=j;
string block="";
block+=board[i][j];
block+=" box ";
block+=(i/3);
block+=" - ";
block+=(j/3);
if(records.find(row)!=records.end())
return false;
if(records.find(col)!=records.end())
return false;
if(records.find(block)!=records.end())
return false;
//otherwise add row
records.insert(row);
records.insert(col);
records.insert(block);
}
}
return true;
}
int main()
{
//board array
//empty cell are represented by a .
char board[9][9]={{'5','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}};
//calling validation function
if(validate_sudoku(board))
cout<<"VALID SUDOKU";
else
cout<<"INVALID SUDOKU";
return 0;
}