-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path11110.go
86 lines (78 loc) · 1.52 KB
/
11110.go
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
// UVa 11110 - Equidivisions
package main
import (
"fmt"
"os"
)
var (
n int
cells [][]int
visited [][]bool
directions = [][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}
)
type node struct{ y, x int }
func bfs(y, x, v int) int {
count := 1
visited[y][x] = true
for queue := []node{{y, x}}; len(queue) > 0; queue = queue[1:] {
curr := queue[0]
for _, direction := range directions {
if newY, newX := curr.y+direction[0], curr.x+direction[1]; newY >= 0 && newY < n && newX >= 0 && newX < n && cells[newY][newX] == v && !visited[newY][newX] {
visited[newY][newX] = true
count++
queue = append(queue, node{newY, newX})
}
}
}
return count
}
func solve() bool {
visited = make([][]bool, n)
for i := range visited {
visited[i] = make([]bool, n)
}
here:
for i := 1; i <= n; i++ {
for y, row := range cells {
for x, cell := range row {
if cell == i {
if bfs(y, x, i) != n {
return false
}
continue here
}
}
}
}
return true
}
func main() {
in, _ := os.Open("11110.in")
defer in.Close()
out, _ := os.Create("11110.out")
defer out.Close()
var y, x int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
cells = make([][]int, n)
for i := range cells {
cells[i] = make([]int, n)
for j := range cells[i] {
cells[i][j] = n
}
}
for i := 1; i < n; i++ {
for j := 0; j < n; j++ {
fmt.Fscanf(in, "%d%d", &y, &x)
cells[y-1][x-1] = i
}
}
if solve() {
fmt.Fprintln(out, "good")
} else {
fmt.Fprintln(out, "wrong")
}
}
}