Advertisement
Egmell

6

Apr 18th, 2024
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.46 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. type point struct {
  12.     x          int
  13.     y          int
  14.     steps      int
  15.     figureType rune
  16. }
  17.  
  18. var directionsK = [][]int{{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}}
  19. var directionsG = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
  20.  
  21. func isValidMove(x, y, n int) bool {
  22.     return x >= 0 && x < n && y >= 0 && y < n
  23. }
  24.  
  25. func minMoves(chessboard [][]rune, n, startX, startY, targetX, targetY int) int {
  26.     queue := []point{{startX, startY, 0, 'K'}}
  27.  
  28.     visitedK := make(map[int]bool)
  29.     visitedK[startX*n+startY] = true
  30.  
  31.     visitedG := make(map[int]bool)
  32.  
  33.     for len(queue) > 0 {
  34.         curr := queue[0]
  35.         queue = queue[1:]
  36.  
  37.         if curr.x == targetX && curr.y == targetY {
  38.             return curr.steps
  39.         }
  40.         switch curr.figureType {
  41.         case 'G':
  42.             for _, dir := range directionsG {
  43.                 newX, newY := curr.x+dir[0], curr.y+dir[1]
  44.  
  45.                 if isValidMove(newX, newY, n) && !visitedG[newX*n+newY] {
  46.                     newFigureType := 'G'
  47.                     if chessboard[newX][newY] == 'K' {
  48.                         newFigureType = 'K'
  49.                         visitedK[newX*n+newY] = true
  50.                     }
  51.                     queue = append(queue, point{newX, newY, curr.steps + 1, newFigureType})
  52.                     visitedG[newX*n+newY] = true
  53.                 }
  54.             }
  55.         case 'K':
  56.             for _, dir := range directionsK {
  57.                 newX, newY := curr.x+dir[0], curr.y+dir[1]
  58.  
  59.                 if isValidMove(newX, newY, n) && !visitedK[newX*n+newY] {
  60.                     newFigureType := 'K'
  61.                     if chessboard[newX][newY] == 'G' {
  62.                         newFigureType = 'G'
  63.                         visitedG[newX*n+newY] = true
  64.                     }
  65.                     queue = append(queue, point{newX, newY, curr.steps + 1, newFigureType})
  66.                     visitedK[newX*n+newY] = true
  67.                 }
  68.             }
  69.         }
  70.  
  71.     }
  72.  
  73.     return -1
  74. }
  75.  
  76. func main() {
  77.     scanner := bufio.NewScanner(os.Stdin)
  78.     buf := make([]byte, 1e3)
  79.     scanner.Buffer(buf, 1e3)
  80.  
  81.     scanner.Scan()
  82.     n, _ := strconv.Atoi(strings.Split(scanner.Text(), " ")[0])
  83.  
  84.     chessboard := make([][]rune, n)
  85.     for i := range chessboard {
  86.         scanner.Scan()
  87.         fields := strings.Fields(scanner.Text())
  88.         chessboard[i] = make([]rune, n)
  89.         for j := 0; j < len(fields[0]); j++ {
  90.             chessboard[i][j] = []rune(fields[0])[j]
  91.         }
  92.     }
  93.     var startX, startY, targetX, targetY int
  94.     for i := 0; i < n; i++ {
  95.         for j := 0; j < n; j++ {
  96.             if chessboard[i][j] == 'S' {
  97.                 startX, startY = i, j
  98.             } else if chessboard[i][j] == 'F' {
  99.                 targetX, targetY = i, j
  100.             }
  101.         }
  102.     }
  103.     fmt.Println(minMoves(chessboard, n, startX, startY, targetX, targetY))
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement