-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpuzzle.go
66 lines (62 loc) · 1 KB
/
npuzzle.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
//https://open.kattis.com/problems/npuzzle
package main
import (
"fmt"
)
type Position struct {
Row int
Col int
}
func main() {
pos := map[int32]Position{}
pos['.'] = Position{
Row: 3,
Col: 3,
}
var row, col int
for i := 'A'; i < 'P'; i++ {
pos[i] = Position{
Row: row,
Col: col,
}
col++
if col%4 == 0 {
row++
col = 0
}
}
var cur string
var curRow, curCol, scatter int
for i := 0; i < 4; i++ {
fmt.Scanln(&cur)
for j := 0; j < 4; j++ {
if cur[j] == '.' {
curCol++
if curCol%4 == 0 {
curCol = 0
curRow++
}
continue
}
var expPos = pos[int32(cur[j])]
if expPos.Row != curRow || expPos.Col != curCol {
if curRow > expPos.Row {
scatter += curRow - expPos.Row
} else {
scatter += expPos.Row - curRow
}
if curCol > expPos.Col {
scatter += curCol - expPos.Col
} else {
scatter += expPos.Col - curCol
}
}
curCol++
if curCol%4 == 0 {
curCol = 0
curRow++
}
}
}
fmt.Println(scatter)
}