-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10279.go
87 lines (79 loc) · 1.6 KB
/
10279.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
// UVa 10279 - Mine Sweeper
package main
import (
"fmt"
"os"
"strings"
)
var (
n int
directions = [][2]int{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}
)
func check(y, x int, grid [][]byte) int {
var count int
for _, direction := range directions {
if newY, newX := y+direction[0], x+direction[1]; newY >= 0 && newY < n && newX >= 0 && newX < n && grid[newY][newX] == '*' {
count++
}
}
return count
}
func mark(grid, result [][]byte) {
for y, row := range grid {
for x, cell := range row {
if cell == '*' {
result[y][x] = '*'
}
}
}
}
func solve(grid, touched [][]byte) [][]byte {
result := make([][]byte, n)
for i := range result {
result[i] = []byte(strings.Repeat(".", n))
}
lose := false
for y, row := range touched {
for x, cell := range row {
if cell == 'x' {
if grid[y][x] == '*' {
lose = true
} else {
result[y][x] = byte('0' + check(y, x, grid))
}
}
}
}
if lose {
mark(grid, result)
}
return result
}
func main() {
in, _ := os.Open("10279.in")
defer in.Close()
out, _ := os.Create("10279.out")
defer out.Close()
var kase int
var line string
for fmt.Fscanf(in, "%d", &kase); kase > 0; kase-- {
fmt.Fscanf(in, "\n%d", &n)
grid := make([][]byte, n)
for i := range grid {
fmt.Fscanf(in, "%s", &line)
grid[i] = []byte(line)
}
touched := make([][]byte, n)
for i := range touched {
fmt.Fscanf(in, "%s", &line)
touched[i] = []byte(line)
}
result := solve(grid, touched)
for _, row := range result {
fmt.Fprintln(out, string(row))
}
if kase > 1 {
fmt.Fprintln(out)
}
}
}