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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
func isValidCrossword(grid [][]int) bool {
if len(grid) == 0 {
return false
}
n := len(grid)
if n < 3 {
return false
}
dirs := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
isValidPosition := func(r, c, n int) bool {
return r >= 0 && r < n && c >= 0 && c < n
}
// Check rotational symmetry
checkRotationalSymmetry := func() bool {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if grid[i][j] != grid[n-1-i][n-1-j] {
return false
}
}
}
return true
}
// Check connectivity
checkConnectivity := func() bool {
visited := make([][]bool, n)
for i := range visited {
visited[i] = make([]bool, n)
}
startR, startC := -1, -1
whiteCount := 0
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
whiteCount++
if startR == -1 {
startR, startC = i, j
}
}
}
}
if whiteCount == 0 {
return true
}
var dfs func(r, c int)
dfs = func(r, c int) {
visited[r][c] = true
for _, dir := range dirs {
nr, nc := r+dir[0], c+dir[1]
if isValidPosition(nr, nc, n) && !visited[nr][nc] && grid[nr][nc] == 1 {
dfs(nr, nc)
}
}
}
dfs(startR, startC)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 && !visited[i][j] {
return false
}
}
}
return true
}
// Check word lengths
checkWordLengths := func() bool {
// Check horizontal words
for i := 0; i < n; i++ {
var wordLengths []int
currentLength := 0
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
currentLength++
} else {
if currentLength > 0 {
wordLengths = append(wordLengths, currentLength)
currentLength = 0
}
}
}
if currentLength > 0 {
wordLengths = append(wordLengths, currentLength)
}
for _, length := range wordLengths {
if length < 3 {
return false
}
}
}
// Check vertical words
for j := 0; j < n; j++ {
var wordLengths []int
currentLength := 0
for i := 0; i < n; i++ {
if grid[i][j] == 1 {
currentLength++
} else {
if currentLength > 0 {
wordLengths = append(wordLengths, currentLength)
currentLength = 0
}
}
}
if currentLength > 0 {
wordLengths = append(wordLengths, currentLength)
}
for _, length := range wordLengths {
if length < 3 {
return false
}
}
}
return true
}
// Validate all rules
if !checkRotationalSymmetry() {
return false
}
if !checkConnectivity() {
return false
}
if !checkWordLengths() {
return false
}
return true
}
|