Advertisement
Davi0k

scale_e_serpenti.go

Jan 14th, 2023
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.66 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "io"
  7.     "math"
  8.     "os"
  9.     "strconv"
  10.     "strings"
  11. )
  12.  
  13. type item struct {
  14.     element  int
  15.     priority int
  16. }
  17.  
  18. type heap []*item
  19.  
  20. func heapify_up(H heap, j int) {
  21.     for true {
  22.         i := (j - 1) / 2
  23.         if i == j || H[j].priority > H[i].priority {
  24.             break
  25.         }
  26.         H[j], H[i] = H[i], H[j]
  27.         j = i
  28.     }
  29. }
  30.  
  31. func heapify_down(H heap, i int) {
  32.     for true {
  33.         if 2*i+1 >= len(H) {
  34.             break
  35.         }
  36.  
  37.         j := 2*i + 1
  38.  
  39.         if j+1 < len(H) && H[j+1].priority < H[j].priority {
  40.             j = j + 1
  41.         }
  42.  
  43.         if H[j].priority >= H[i].priority {
  44.             break
  45.         }
  46.  
  47.         H[i], H[j] = H[j], H[i]
  48.  
  49.         i = j
  50.     }
  51. }
  52.  
  53. func (H *heap) Insert(element, priority int) {
  54.     (*H) = append(*H, &item{element, priority})
  55.  
  56.     heapify_up(*H, len(*H)-1)
  57. }
  58.  
  59. func (H *heap) DeleteMin() int {
  60.     minimum := (*H)[0].element
  61.  
  62.     (*H)[0] = (*H)[len(*H)-1]
  63.     (*H) = (*H)[:len(*H)-1]
  64.     heapify_down(*H, 0)
  65.  
  66.     return minimum
  67. }
  68.  
  69. func (H *heap) ChangeKey(element, priority int) {
  70.     for i := 0; i < len(*H); i++ {
  71.         if (*H)[i].element == element {
  72.             prec := (*H)[i].priority
  73.  
  74.             (*H)[i].priority = priority
  75.  
  76.             if priority < prec {
  77.                 heapify_up(*H, i)
  78.             }
  79.  
  80.             if priority > prec {
  81.                 heapify_down(*H, i)
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. type graph map[int][]int
  88.  
  89. func convert(number string) int {
  90.     value, err := strconv.Atoi(number)
  91.  
  92.     if err != nil {
  93.         panic(err)
  94.     }
  95.  
  96.     return value
  97. }
  98.  
  99. func populate(G graph, N int, slices []int) {
  100.     for n := 1; n <= N; n++ {
  101.         for i := 1; i <= 6; i++ {
  102.             if n+i <= N {
  103.                 if slices[n+i-1] == 0 {
  104.                     G[n] = append(G[n], n+i)
  105.                 } else {
  106.                     G[n] = append(G[n], slices[n+i-1])
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113. func dijkstra(G graph, N, s int) ([]int, []int) {
  114.     var D, pred []int = make([]int, N), make([]int, N)
  115.  
  116.     D[s-1] = 0
  117.  
  118.     for n := 1; n <= N; n++ {
  119.         if n != s {
  120.             D[n-1] = math.MaxInt16
  121.         }
  122.     }
  123.  
  124.     var H heap = make(heap, 0)
  125.  
  126.     for n := 1; n <= N; n++ {
  127.         H.Insert(n, D[n-1])
  128.     }
  129.  
  130.     for len(H) > 0 {
  131.         u := H.DeleteMin()
  132.  
  133.         for _, v := range G[u] {
  134.             if D[u-1]+1 < D[v-1] {
  135.                 pred[v-1] = u - 1
  136.                 D[v-1] = D[u-1] + 1
  137.                 H.ChangeKey(v, D[v-1])
  138.             }
  139.         }
  140.     }
  141.  
  142.     return D, pred
  143. }
  144.  
  145. func main() {
  146.     var G graph = make(map[int][]int)
  147.  
  148.     reader := bufio.NewReader(os.Stdin)
  149.     setup, _, _ := reader.ReadLine()
  150.     split := strings.Split(string(setup), " ")
  151.  
  152.     R, C := convert(split[0]), convert(split[1])
  153.  
  154.     N := R * C
  155.  
  156.     slices := make([]int, N)
  157.  
  158.     for true {
  159.         line, _, err := reader.ReadLine()
  160.  
  161.         if err == io.EOF {
  162.             break
  163.         }
  164.  
  165.         split := strings.Split(string(line), " ")
  166.         X, Y := convert(split[0]), convert(split[1])
  167.         slices[X-1] = Y
  168.     }
  169.  
  170.     populate(G, N, slices)
  171.  
  172.     D, _ := dijkstra(G, N, 1)
  173.  
  174.     fmt.Println(D[N-1])
  175. }
  176.  
Tags: golang
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement