-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.cpp
More file actions
57 lines (51 loc) · 1.26 KB
/
sudoku.cpp
File metadata and controls
57 lines (51 loc) · 1.26 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
#include <stdio.h>
#include <stdlib.h>
int M[9][9]={{5,0,0,4,0,7,0,6,0},
{0,9,0,0,5,0,0,1,0},
{1,0,7,0,0,0,0,0,0},
{2,0,0,7,0,0,0,4,1},
{0,0,0,0,0,0,0,0,0},
{8,7,0,0,0,2,0,0,6},
{0,0,0,0,0,0,4,0,8},
{0,6,0,0,4,0,0,3,0},
{0,5,0,9,0,1,0,0,2}};
int busca_vazia(int *L,int *C) {
for ((*L)=0;(*L)<9;(*L)++)
for ((*C)=0;(*C)<9;(*C)++)
if (!M[*L][*C]) {/*printf("Vazia em %d %d\n",*L,*C);*/return 1;}
return 0;
}
void mostra() {
int i,j;
for (i=0;i<9;i++) {
for (j=0;j<9;j++)
printf("%d ",M[i][j]);
printf("\n");
}
printf("\n");
}
int valida(int N,int L,int C) {
int i,j;
for (i=0;i<9;i++)
if (N==M[L][i]|| N==M[i][C]) return 0;
int LG=L/3*3;
int CG=C/3*3;
for (i=LG;i<=LG+2;i++)
for (j=CG;j<=CG+2;j++)
if (M[i][j]==N) return 0;
//printf("Pode colocar %d em %d %d\n",N,L,C);
return 1;
}
void su() {
int L,C,i;
if (!busca_vazia(&L,&C)) {
mostra();return;
}
for (i=1;i<=9;i++)
if (valida(i,L,C))
{M[L][C]=i;su();M[L][C]=0;}
}
int main() {
su();
system("pause");
}