Ir notas de estudio de idiomas 11 - búsqueda en amplitud de laberintos

Ir notas de estudio de idiomas 11 - búsqueda en amplitud de laberintos

  • Crear un corte 2D con un bucle
  • Uso de segmentos para implementar colas
  • Leer archivos con Fscanf

Fscanf termina cuando encuentra \n. Cuando
encuentra \r, reemplazará \r con 0.
Esto es un problema. Debe prestar atención a cuál es su salto de línea de texto. Es \r\n en Windows y \r \n en Linux y Mac. El siguiente es \n, lo que significa que aquí hay un hoyo. Es
normal que el código lea archivos de datos en Linux y Mac. En Windows, encontrará un 0 al final de varios líneas.
Puede usar el IDE incorporado para abrirlo. Cambie el salto de línea del archivo de datos establecido a LF (Linux, salto de línea de Mac)

  • abstracción de punto

contenido del documento

6 5
0 1 0 0 0
0 0 0 1 0
0 1 0 1 0
1 1 1 0 0
0 1 0 0 1
0 1 0 0 0

Código de muestra

package main

import (
	"fmt"
	"os"
)

func readMaze(filename string) [][]int {
    
    
	file, err := os.Open(filename)
	if err != nil {
    
    
		panic(err)
	}
	var row, col int
	fmt.Fscanf(file, "%d %d", &row, &col)

	maze := make([][]int, row)
	for i := range maze {
    
    
		maze[i] = make([]int, col)
		for j := range maze[i] {
    
    
			fmt.Fscanf(file, "%d", &maze[i][j])
		}
	}
	return maze
}

type point struct {
    
    
	i, j int
}

var dirs = [4]point{
    
    
	{
    
    -1, 0}, {
    
    0, -1}, {
    
    1, 0}, {
    
    0, 1},
}

func (p point) add(r point) point {
    
    
	return point{
    
    p.i + r.i, p.j + r.j}
}

func (p point) at(grid [][]int) (int, bool) {
    
    
	if p.i < 0 || p.i >= len(grid) {
    
    
		return 0, false
	}
	if p.j < 0 || p.j >= len(grid[p.i]) {
    
    
		return 0, false
	}
	return grid[p.i][p.j], true
}

func walk(maze [][]int, start, end point) [][]int {
    
    
	steps := make([][]int, len(maze))
	for i := range steps {
    
    
		steps[i] = make([]int, len(maze[0]))
	}

	Q := []point{
    
    start}
	for len(Q) > 0 {
    
    
		cur := Q[0]
		Q = Q[1:]
		if cur == end {
    
    
			break
		}
		for _, dir := range dirs {
    
    
			next := cur.add(dir)
			val, ok := next.at(maze)
			if !ok || val == 1 {
    
    
				continue
			}
			val, ok = next.at(steps)
			if !ok || val != 0 {
    
    
				continue
			}
			if next == start {
    
    
				continue
			}
			curSteps, _ := cur.at(steps)
			steps[next.i][next.j] = curSteps + 1
			Q = append(Q, next)
		}
	}

	逆序打印路径
	//cur := end
	//for a, _ := cur.at(steps); a != 0; {
    
    
	//	fmt.Printf("(%d,%d)->", cur.i, cur.j)
	//	for _, dir := range dirs {
    
    
	//		next := cur.add(dir)
	//		if s, _ := next.at(steps); s == a-1 {
    
    
	//			cur = next
	//			break
	//		}
	//	}
	//	a, _ = cur.at(steps)
	//}
	//fmt.Printf("(%d,%d)\n", start.i, start.j)
	return steps
}
func main() {
    
    
	maze := readMaze("maze/maze.in")
	steps := walk(maze, point{
    
    0, 0}, point{
    
    len(maze) - 1, len(maze[0]) - 1})
	for _, row := range steps {
    
    
		for _, val := range row {
    
    
			fmt.Printf("%3d", val)
		}
		fmt.Println()
	}
}

Supongo que te gusta

Origin blog.csdn.net/shn111/article/details/122710869
Recomendado
Clasificación