-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path585.go
60 lines (52 loc) · 1.27 KB
/
585.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
// UVa 585 - Triangles
package main
import (
"fmt"
"os"
)
var (
n int
paper [][]byte
)
func dfsUp(i, j, level int) int {
if i > 0 && paper[i-1][j] == '-' && paper[i-1][j+1] == '-' && paper[i-1][j+2] == '-' {
return min(dfsUp(i-1, j, level+1), min(dfsUp(i-1, j+1, level+1), dfsUp(i-1, j+2, level+1)))
}
return level * (2 * level) / 2
}
func dfsDown(i, j, level int) int {
if i+1 < n && j < len(paper[i])-1 && j >= 2 && paper[i+1][j-2] == '-' && paper[i+1][j-1] == '-' && paper[i+1][j] == '-' {
return min(dfsDown(i+1, j-2, level+1), min(dfsDown(i+1, j-1, level+1), dfsDown(i+1, j, level+1)))
}
return level * (2 * level) / 2
}
func solve() int {
var largest int
for i, row := range paper {
for j, cell := range row {
if cell == '-' {
largest = max(largest, max(dfsUp(i, j, 1), dfsDown(i, j, 1)))
}
}
}
return largest
}
func main() {
in, _ := os.Open("585.in")
defer in.Close()
out, _ := os.Create("585.out")
defer out.Close()
var line string
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
paper = make([][]byte, n)
for i := range paper {
fmt.Fscanf(in, "%s", &line)
paper[i] = []byte(line)
}
fmt.Fprintf(out, "Triangle #%d\n", kase)
fmt.Fprintf(out, "The largest triangle area is %d.\n\n", solve())
}
}