-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path201.go
76 lines (68 loc) · 1.49 KB
/
201.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
// UVa 201 - Squares
package main
import (
"fmt"
"os"
)
type point struct{ h, v bool }
var n int
func isSquare(i, j, k int, grid [][]point) bool {
for d := 0; d < k; d++ {
if !grid[i+d][j].v || !grid[i+d][j+k].v || !grid[i][j+d].h || !grid[i+k][j+d].h {
return false
}
}
return true
}
func solve(grid [][]point) map[int]int {
squareMap := make(map[int]int)
for i := 0; i < n-1; i++ {
for j := 0; j < n-1; j++ {
max := min(n-1-i, n-1-j)
for k := 1; k <= max; k++ {
if isSquare(i, j, k, grid) {
squareMap[k]++
}
}
}
}
return squareMap
}
func main() {
in, _ := os.Open("201.in")
defer in.Close()
out, _ := os.Create("201.out")
defer out.Close()
var m, n1, n2 int
var direction byte
for kase := 1; ; kase++ {
if _, err := fmt.Fscanf(in, "%d", &n); err != nil {
break
}
grid := make([][]point, n)
for i := range grid {
grid[i] = make([]point, n)
}
for fmt.Fscanf(in, "%d", &m); m > 0; m-- {
switch fmt.Fscanf(in, "%c%d%d", &direction, &n1, &n2); direction {
case 'H':
grid[n1-1][n2-1].h = true
case 'V':
grid[n2-1][n1-1].v = true
}
}
squareMap := solve(grid)
if kase > 1 {
fmt.Fprint(out, "\n**********************************\n\n")
}
if fmt.Fprintf(out, "Problem #%d\n\n", kase); len(squareMap) == 0 {
fmt.Fprintln(out, "No completed squares can be found.")
} else {
for i := 1; i < n; i++ {
if num, ok := squareMap[i]; ok {
fmt.Fprintf(out, "%d square (s) of size %d\n", num, i)
}
}
}
}
}