Skip to content

Commit

Permalink
2021-06-03 boj 14226
Browse files Browse the repository at this point in the history
  • Loading branch information
wookiist committed Jun 3, 2021
1 parent d328d78 commit 3680b1b
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions baekjoon/14226.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"bufio"
"fmt"
"os"
"strconv"
)

var (
w = bufio.NewWriter(os.Stdout)
sc = bufio.NewScanner(os.Stdin)
)

var (
D [1004][1004]int
)

type pair struct {
x, c int
}

func main() {
defer w.Flush()
S := scanInt()
for i := range D {
for j := range D[i] {
D[i][j] = -1
}
}
D[1][0] = 0
q := make([]pair, 0)
q = append(q, pair{1, 0})
for len(q) != 0 {
nx, nc := q[0].x, q[0].c
q = q[1:]
// clipboard copy
if D[nx][nx] == -1 {
D[nx][nx] = D[nx][nc] + 1
q = append(q, pair{nx, nx})
}
// clipboard paste
if nx+nc < 1004 && D[nx+nc][nc] == -1 {
D[nx+nc][nc] = D[nx][nc] + 1
if nx+nc == S {
fmt.Fprintln(w, D[nx+nc][nc])
break
}
q = append(q, pair{nx + nc, nc})
}
// delete
if 0 <= nx-1 && D[nx-1][nc] == -1 {
D[nx-1][nc] = D[nx][nc] + 1
if nx-1 == S {
fmt.Fprintln(w, D[nx-1][nc])
break
}
q = append(q, pair{nx - 1, nc})
}
}
}

func scanInt() int {
sc.Scan()
n, _ := strconv.Atoi(sc.Text())
return n
}

0 comments on commit 3680b1b

Please sign in to comment.