-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path백준-스도쿠.cpp.txt
135 lines (117 loc) · 2.07 KB
/
백준-스도쿠.cpp.txt
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
133
134
#include <iostream>
#include <cstdio>
using namespace std;
int** arr;
bool** c1;
bool** c2;
bool** c3;
int square(int x, int y)
{
if ((0 <= x && x < 3) && (0 <= y && y < 3))
return 0;
if ((0 <= x && x < 3) && (3 <= y && y < 6))
return 1;
if ((0 <= x && x < 3) && (6 <= y && y < 9))
return 2;
if ((3 <= x && x < 6) && (0 <= y && y < 3))
return 3;
if ((3 <= x && x < 6) && (3 <= y && y < 6))
return 4;
if ((3 <= x && x < 6) && (6 <= y && y < 9))
return 5;
if ((6 <= x && x < 9) && (0 <= y && y < 3))
return 6;
if ((6 <= x && x < 9) && (3 <= y && y < 6))
return 7;
if ((6 <= x && x < 9) && (6 <= y && y < 9))
return 8;
}
void go(int z)
{
if (z == 81)
{
for (int j = 0; j < 9; j++)
{
for (int i = 0; i < 9; i++)
{
cout << arr[j][i] << " ";
}
cout << endl;
}
exit(0);
}
int x = z / 9;
int y = z % 9;
if (arr[x][y] != 0)
{
go(z + 1);
}
else
{
for (int i = 1; i <= 9; i++)
{
if (c1[x][i] == false && c2[y][i] == false && c3[square(x, y)][i] == false)
{
c1[x][i] = true;
c2[y][i] = true;
c3[square(x, y)][i] = true;
arr[x][y] = i;
go(z + 1);
arr[x][y] = 0;
c1[x][i] = false;
c2[y][i] = false;
c3[square(x, y)][i] = false;
}
}
}
}
int main()
{
arr = new int*[9];
for (int i = 0; i < 9; i++)
arr[i] = new int[9];
c1 = new bool*[9];
for (int i = 0; i < 9; i++)
c1[i] = new bool[10];
c2 = new bool*[9];
for (int i = 0; i < 9; i++)
c2[i] = new bool[10];
c3 = new bool*[9];
for (int i = 0; i < 9; i++)
c3[i] = new bool[10];
for (int i = 0; i< 9; i++)
{
for (int j = 0; j < 9; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < 9; i++)
{
for (int j = 1; j < 10; j++)
{
c1[i][j] = false;
c2[i][j] = false;
c3[i][j] = false;
}
}
for (int i = 0; i< 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (arr[i][j] != 0)
{
c1[i][arr[i][j]] = true;
c2[j][arr[i][j]] = true;
c3[square(i,j)][arr[i][j]] = true;
}
}
}
go(0);
for (int i = 0; i < 9; i++)
{
delete [] arr[i];
}
delete [] arr;
return 0;
}