-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
132 lines (128 loc) · 4.38 KB
/
sudoku.cpp
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
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
bool row(int r ,int col ,int n);
bool coloumn(int r ,int col ,int n);
bool square(int r ,int col ,int n);
int matrix[9][9];
int main()
{
//variable use
int gap = 0, r, col, t;
bool x = 1;
char c[20];
cout << " Write the sudoku problem manually (w) or read from file (r)? ";
cin >> c[0];
if(c[0]=='r')
{
cout << "Write the sudoku filename: ";
cin >> c;
cout << endl;
ifstream file(c);
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
file >> matrix[i][j];
if(!matrix[i][j])
gap++;
}
}
file.close();
}else if(c[0] == 'w')
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
{
do{
cout << "Please enter " << i+1 << ". row " << j+1 << ". number: ";
cin >> matrix[i][j];
}while( matrix[i][j] <= 0 && matrix[i][j] > 10);
if( matrix[i][j] == 0)
gap++;
}
}else
{
cout << "exiting..." << endl;
return 0;
}
while(gap && x)
{
x=0;
for(int i=0; i<9; i++)
{
for(int j = 0; j < 9; j++)
{
if(matrix[i][j] != 0)
continue;
t=0;
for(int y=1; y<10; y++)
{
if(row(i, j, y) && coloumn(i, j, y) && square(i, j, y))
{
if(t == 0)
t = y;
else
{
t = 0;
break;
}
}
}
if(t != 0)
{
matrix[i][j] = t;
x = 1;
gap--;
}
}
}
}
if(!x)
cout << "It cannot be solved" << endl;
else if(!gap)
cout << "solved" << endl;
cout << endl;
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
bool row(int r, int col, int n)
{
for(int g=0; g<9; g++)
{
if(matrix[r][g] == n)
{
return 0;
}
}
return 1;
}
bool coloumn(int r, int col, int n)
{
for(int g=0; g<9; g++)
{
if(matrix[g][col] == n)
return 0;
}
return 1;
}
bool square(int r, int col, int n)
{
int row = ceil((r+1)/3.);
int coloumn = ceil((col+1)/3.);
for(int g=(row - 1)*3; g < ((row - 1)*3 +3); g++)
for(int h = (coloumn-1)*3; h <((coloumn - 1)*3 +3);h++)
{
if(matrix[g][h] == n)
return 0;
}
return 1;
}