Go语言实现走迷宫

package main

import (
	"fmt"
	"os/exec"
	"os"
	"time"
)

//定义全局变量
var(
	//定义变量保存R当前位置
	currentRow = 1
	currentCol = 1
	//定义变量保存迷宫出口位置(索引)
	endRow = 1
	endCol = 5
)

func main() {

	//1.定义一个二维数组保存迷宫地图
	sce := [][] byte{
		{'*','*','*','*','*','*'},
		{'*','R',' ','*',' ',' '},
		{'*',' ','*','*',' ','*'},
		{'*',' ',' ','*',' ','*'},
		{'*','*',' ',' ',' ','*'},
		{'*','*','*','*','*','*'},
	}
	//2.定义一个函数打印地图
	printMap(sce)
	for{
		//1.提示用户如何输入
		fmt.Println("请输入w a s d,以回车结束")
		fmt.Println("w-->上 a-->左 s-->下 d-->右")

		//2.接收用户输入的数据
		ch := input()
	
		//3.定于函数让R根据用户输入行走
		move(sce,ch)
		//4.判断是否已经走出迷宫
		if(currentRow == endRow && currentCol == endCol){
			break
		}
		//5.打印地图
		printMap(sce)
	}
	fmt.Println("恭喜你通过关卡...")
	time.Sleep(5000)
}

func move(m[][]byte,ch string){
	switch ch {
	case "w","W":
		fmt.Println("向上走")
		if m[currentRow -1][currentCol] != '*'{
			m[currentRow][currentCol] = ' '
			m[currentRow - 1][currentCol] = 'R'
			currentRow --
		}
	case "a","A":
		fmt.Println("向左走")
		if m[currentRow][currentCol - 1] != '*'{
			m[currentRow][currentCol] = ' '
			m[currentRow][currentCol - 1] = 'R'
			currentCol --
		}
	case "s","S":
		fmt.Println("向下走")
		if m[currentRow + 1][currentCol] != '*'{
			m[currentRow][currentCol] = ' '
			m[currentRow + 1][currentCol] = 'R'
			currentRow ++
		}
	case "d","D":
		fmt.Println("向右走")
		if m[currentRow][currentCol + 1] != '*'{
			m[currentRow][currentCol] = ' '
			m[currentRow][currentCol + 1] = 'R'
			currentCol ++
		}
	}
}

func input() (ch string){
	//1.接收用户输入的数据
	fmt.Scanln(&ch)
	//2.将接收到的数据返回给调用者
	return
}

func printMap(m[][]byte){

	//清空屏幕代码
	cmd := exec.Command("cmd","/c","cls")
	cmd.Stdout = os.Stdout
	cmd.Run()

	//循环打印地图
	for _,v1 := range m{
		for _, v2 := range v1{
			fmt.Printf("%c",v2)
		}
	fmt.Printf("\n")
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列实现走迷宫的Java代码的主要思路如下: 1. 首先,创建一个二维数组来表示迷宫的布局,其中1代表墙壁,0代表通道。 2. 创建一个队列来保存迷宫中待探索的位置。 3. 将起点(1, 1)加入队列,并将其标记为已访问。 4. 进入循环,直到队列为空或找到出口(m, n)为止: - 从队列中取出一个位置(x, y)。 - 检查该位置的上、下、左、右四个邻居是否为通道且未访问过。 - 如果是,则将邻居位置加入队列,并标记为已访问。 - 继续下一次循环。 5. 如果找到出口(m, n),则通过回溯找到从起点到出口的路径。 6. 如果队列为空,表示无法找到通往出口的路径。 下面是一段示例代码,用于实现队列走迷宫的功能: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private int[][] maze; private boolean[][] visited; private int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; public MazeSolver(int[][] maze) { this.maze = maze; this.visited = new boolean[maze.length][maze[0].length]; } public boolean solve() { int m = maze.length; int n = maze[0].length; Queue<int[]> queue = new LinkedList<>(); queue.offer(new int[]{1, 1}); visited[1][1] = true; while (!queue.isEmpty()) { int[] position = queue.poll(); int x = position[0]; int y = position[1]; if (x == m && y == n) { return true; // 找到出口 } for (int[] direction : directions) { int newX = x + direction[0]; int newY = y + direction[1]; if (newX >= 1 && newX <= m && newY >= 1 && newY <= n && maze[newX][newY] == 0 && !visited[newX][newY]) { queue.offer(new int[]{newX, newY}); visited[newX][newY] = true; } } } return false; // 无法找到通往出口的路径 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值