-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.cc
64 lines (61 loc) · 1.42 KB
/
find.cc
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
#include <iostream>
using namespace std;
#define SIZE 10
//number of tables to find
#define NFIND 3
int cnt = NFIND;
void tprint(int a[][SIZE]){
cout << "Result " << (NFIND - cnt + 1) << endl;
for(int row = 0; row < SIZE; row++){
for(int col = 0; col < SIZE; col ++)
cout << a[row][col] << " ";
cout << endl;
//print for latex
//cout << row;
//for(int col = 0; col < SIZE; col ++)
//cout << " & " << a[row][col];
//cout << "\\\\\n\\hline\n";
}
cout << endl;
}
bool f(int a[][SIZE], int row, int col, int val){
if (!cnt) return false;
a[row][col] = val;
int nrow, ncol;
if (col < SIZE - 1){
ncol = col + 1;
nrow = row;
} else if (row < SIZE - 1){
ncol = 0;
nrow = row + 1;
} else {
bool found = false;
for(int i = 0; i < SIZE; i++)
if (a[i][i] != i){
found = true;
break;
}
if (!found) return false;
tprint(a);
cnt--;
return !cnt;
}
bool checked[SIZE];
fill(checked, checked + SIZE * sizeof(bool), false);
for(int i = 0; i < ncol; i++)
checked[a[nrow][i]] = true;
for(int i = 0; i < nrow; i++)
checked[a[i][ncol]] = true;
for(int v = 0; v < SIZE; v++)
if (!checked[v] && f(a, nrow, ncol, v))
return true;
return false;
}
int main(){
int a[SIZE][SIZE];
for(int i = 0; i < SIZE; i++)
f(a, 0, 0, i);
if (cnt == NFIND)
cout << "not found" << endl;
return 0;
}