-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10503.go
68 lines (61 loc) · 1.11 KB
/
10503.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
// UVa 10503 - The dominoes solitaire
package main
import (
"fmt"
"os"
)
type piece struct{ l, r int }
var (
n, right int
pieces []piece
visited map[int]bool
)
func backtracking(dots, curr int) bool {
if curr == n {
return dots == right
}
for i, p := range pieces {
if !visited[i] {
if p.l == dots {
visited[i] = true
if backtracking(p.r, curr+1) {
return true
}
visited[i] = false
}
if p.r == dots {
visited[i] = true
if backtracking(p.l, curr+1) {
return true
}
visited[i] = false
}
}
}
return false
}
func main() {
in, _ := os.Open("10503.in")
defer in.Close()
out, _ := os.Create("10503.out")
defer out.Close()
var m, left, tmp int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
fmt.Fscanf(in, "%d", &m)
fmt.Fscanf(in, "%d%d", &tmp, &left)
fmt.Fscanf(in, "%d%d", &right, &tmp)
pieces = make([]piece, m)
for i := range pieces {
fmt.Fscanf(in, "%d%d", &pieces[i].l, &pieces[i].r)
}
visited = make(map[int]bool)
if backtracking(left, 0) {
fmt.Fprintln(out, "YES")
} else {
fmt.Fprintln(out, "NO")
}
}
}