-
Notifications
You must be signed in to change notification settings - Fork 27
/
598.go
75 lines (67 loc) · 1.26 KB
/
598.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
// UVa 598 - Bundling Newspapers
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var (
out io.WriteCloser
n, size int
newspapers []string
)
func dfs(level, now int, path []string) {
if level == size {
fmt.Fprintln(out, strings.Join(path, ", "))
return
}
for i := now; i < n; i++ {
dfs(level+1, i+1, append(path, newspapers[i]))
}
}
func parse(line string) (int, int) {
if line == "*" {
return 1, n
}
tokens := strings.Split(line, " ")
size1, _ := strconv.Atoi(tokens[0])
if len(tokens) == 1 {
return size1, size1
}
size2, _ := strconv.Atoi(tokens[1])
return size1, size2
}
func main() {
in, _ := os.Open("598.in")
defer in.Close()
out, _ = os.Create("598.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var newspaper string
s.Scan()
kase, _ := strconv.Atoi(s.Text())
for s.Scan(); kase > 0 && s.Scan(); kase-- {
line := s.Text()
newspapers = nil
for s.Scan() {
if newspaper = s.Text(); newspaper == "" {
break
}
newspapers = append(newspapers, newspaper)
}
n = len(newspapers)
for s1, s2 := parse(line); s1 <= s2; s1++ {
fmt.Fprintf(out, "Size %d\n", s1)
size = s1
dfs(0, 0, nil)
fmt.Fprintln(out)
}
if kase > 1 {
fmt.Fprintln(out)
}
}
}