forked from 1989chenguo/CloudComputingLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neighbor.cc
123 lines (108 loc) · 2.91 KB
/
neighbor.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
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
#include <assert.h>
#include <stdio.h>
#include <strings.h>
#include "sudoku.h"
#include <algorithm>
int neighbors[N][NEIGHBOR];
static void mark_adjacent(bool adjacent[ROW][COL], int row, int col)
{
for (int i = 0; i < NUM; ++i) {
adjacent[row][i] = true;
adjacent[i][col] = true;
}
int top = (row/3)*3;
int left = (col/3)*3;
adjacent[top][left] = true;
adjacent[top][left+1] = true;
adjacent[top][left+2] = true;
adjacent[top+1][left] = true;
adjacent[top+1][left+1] = true;
adjacent[top+1][left+2] = true;
adjacent[top+2][left] = true;
adjacent[top+2][left+1] = true;
adjacent[top+2][left+2] = true;
}
static void collect_neighbors(const bool adjacent[ROW][COL], int row, int col, int myneighbors[NEIGHBOR])
{
int n = 0;
for (int y = 0; y < ROW; ++y) {
for (int x = 0; x < COL; ++x) {
if (adjacent[y][x] && !(y == row && x == col)) {
assert(n < NEIGHBOR);
myneighbors[n++] = y*COL + x;
}
}
}
assert(n == NEIGHBOR);
}
static void print_neighbors(const bool adjacent[ROW][COL], int row, int col, int myneighbors[NEIGHBOR])
{
for (int y = 0; y < ROW; ++y) {
for (int x = 0; x < COL; ++x) {
if (y == row && x == col)
putchar('X');
else
putchar(adjacent[y][x] ? 'o' : '.');
}
printf("\n");
}
for (int i = 0; i < NEIGHBOR; ++i) {
printf("%2d, ", myneighbors[i]);
}
puts("\n");
}
/*public*/ void init_neighbors()
{
for (int row = 0; row < ROW; ++row) {
for (int col = 0; col < COL; ++col) {
bool adjacent[ROW][COL];
bzero(adjacent, sizeof adjacent);
mark_adjacent(adjacent, row, col);
int me = row*COL + col;
collect_neighbors(adjacent, row, col, neighbors[me]);
if (DEBUG_MODE)
print_neighbors(adjacent, row, col, neighbors[me]);
}
}
}
bool solved()
{
for (int row = 0; row < ROW; ++row) {
// check row
int occurs[10] = { 0 };
for (int col = 0; col < COL; ++col) {
int val = chess[row][col];
assert(1 <= val && val <= NUM);
++occurs[val];
}
if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}
for (int col = 0; col < COL; ++col) {
int occurs[10] = { 0 };
for (int row = 0; row < ROW; ++row) {
int val = chess[row][col];
// assert(1 <= val && val <= NUM);
++occurs[val];
}
if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}
for (int row = 0; row < ROW; row += 3) {
for (int col = 0; col < COL; col += 3) {
int occurs[10] = { 0 };
++occurs[chess[row ][col]];
++occurs[chess[row ][col+1]];
++occurs[chess[row ][col+2]];
++occurs[chess[row+1][col]];
++occurs[chess[row+1][col+1]];
++occurs[chess[row+1][col+2]];
++occurs[chess[row+2][col]];
++occurs[chess[row+2][col+1]];
++occurs[chess[row+2][col+2]];
if (std::count(occurs, occurs+10, 1) != NUM)
return false;
}
}
return true;
}