-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10344.go
54 lines (48 loc) · 897 Bytes
/
10344.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
// UVa 10344 - 23 out of 5
package main
import (
"fmt"
"os"
)
var (
n [5]int
visited []bool
)
func dfs(level, result int) bool {
if level == 5 {
return result == 23
}
for i := range n {
if !visited[i] {
visited[i] = true
if level == 0 {
if dfs(level+1, n[i]) {
return true
}
} else {
if dfs(level+1, result+n[i]) || dfs(level+1, result-n[i]) || dfs(level+1, result*n[i]) {
return true
}
}
visited[i] = false
}
}
return false
}
func main() {
in, _ := os.Open("10344.in")
defer in.Close()
out, _ := os.Create("10344.out")
defer out.Close()
for {
if fmt.Fscanf(in, "%d%d%d%d%d", &n[0], &n[1], &n[2], &n[3], &n[4]); n[0] == 0 && n[1] == 0 && n[2] == 0 && n[3] == 0 && n[4] == 0 {
break
}
visited = make([]bool, 5)
if dfs(0, 0) {
fmt.Fprintln(out, "Possible")
} else {
fmt.Fprintln(out, "Impossible")
}
}
}