-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10309.go
97 lines (89 loc) · 1.38 KB
/
10309.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
87
88
89
90
91
92
93
94
95
96
97
// UVa 10309 - Turn the Lights Off
package main
import (
"fmt"
"math"
"os"
)
var (
minSteps int
grid [][]bool
)
func allOff() bool {
for _, row := range grid {
for _, cell := range row {
if cell {
return false
}
}
}
return true
}
func press(y, x int) {
grid[y][x] = !grid[y][x]
if y-1 >= 0 {
grid[y-1][x] = !grid[y-1][x]
}
if x-1 >= 0 {
grid[y][x-1] = !grid[y][x-1]
}
if y+1 < 10 {
grid[y+1][x] = !grid[y+1][x]
}
if x+1 < 10 {
grid[y][x+1] = !grid[y][x+1]
}
}
func dfs(y, x, steps int) {
if steps >= minSteps {
return
}
if x == 10 {
if y++; y == 10 {
if allOff() && steps < minSteps {
minSteps = steps
}
return
}
x = 0
}
if y == 0 {
dfs(y, x+1, steps)
press(y, x)
dfs(y, x+1, steps+1)
press(y, x)
} else {
if grid[y-1][x] {
press(y, x)
dfs(y, x+1, steps+1)
press(y, x)
} else {
dfs(y, x+1, steps)
}
}
}
func main() {
in, _ := os.Open("10309.in")
defer in.Close()
out, _ := os.Create("10309.out")
defer out.Close()
var name, line string
for {
if fmt.Fscanf(in, "%s", &name); name == "end" {
break
}
grid = make([][]bool, 10)
for i := range grid {
grid[i] = make([]bool, 10)
fmt.Fscanf(in, "%s", &line)
for j := range line {
if line[j] == 'O' {
grid[i][j] = true
}
}
}
minSteps = math.MaxInt32
dfs(0, 0, 0)
fmt.Fprintf(out, "%s %d\n", name, minSteps)
}
}