Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF676D(_r io.Reader, _w io.Writer) {11 doorTable := [...]string{12 '+': "++++",13 '-': "-|-|",14 '|': "|-|-",15 '^': "^>v<",16 '>': ">v<^",17 'v': "v<^>",18 '<': "<^>v",19 'U': "URDL",20 'R': "RDLU",21 'D': "DLUR",22 'L': "LURD",23 }24 dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} 25 dirTable := [...][]int{26 '+': {0, 1, 2, 3},27 '-': {1, 3},28 '|': {0, 2},29 '^': {0},30 '>': {1},31 'v': {2},32 '<': {3},33 'U': {1, 2, 3},34 'R': {0, 2, 3},35 'D': {0, 1, 3},36 'L': {0, 1, 2},37 }38 canBack := func(from int, backs []int) bool {39 for _, back := range backs {40 if (back+2)&3 == from {41 return true42 }43 }44 return false45 }46 47 in := bufio.NewReader(_r)48 out := bufio.NewWriter(_w)49 defer out.Flush()50 var n, m, ax, ay, bx, by int51 Fscan(in, &n, &m)52 g := make([][]byte, n)53 for i := range g {54 Fscan(in, &g[i])55 }56 Fscan(in, &ax, &ay, &bx, &by)57 bx--58 by--59 60 type stat struct{ x, y, rot int }61 qs := [4][]stat{{{ax - 1, ay - 1, 0}}}62 allEmpty := func() bool {63 for _, q := range qs {64 if len(q) > 0 {65 return false66 }67 }68 return true69 }70 vis := [1000][1000][4]bool{}71 for time := 0; !allEmpty(); time++ {72 q := qs[time&3]73 qs[time&3] = []stat{}74 for _, s := range q {75 if s.x == bx && s.y == by {76 Fprint(out, time)77 return78 }79 if vis[s.x][s.y][s.rot] {80 continue81 }82 vis[s.x][s.y][s.rot] = true83 for rotTimes := 0; rotTimes < 4; rotTimes++ {84 rot := (s.rot + rotTimes) & 385 door := doorTable[g[s.x][s.y]][rot]86 for _, i := range dirTable[door] {87 d := dir4[i]88 x, y := s.x+d[0], s.y+d[1]89 if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y][rot] {90 continue91 }92 door1 := doorTable[g[x][y]][rot]93 if !canBack(i, dirTable[door1]) {94 continue95 }96 tarTime := time + rotTimes + 197 qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})98 }99 }100 }101 }102 Fprint(out, -1)103}104 105