Unity3D之迷宫寻路_A*最短路径寻路

A*寻路相信很多人都听说过,也是比较基本的一种算法,具体概念我就不多说了,网上查找一下一大堆,我直接贴上我的A*应用方法,以下是地图:


接下来是地图数组:

using UnityEngine;
using System.Collections;

public static class MapsArray {

    public static int[,] MazeItem = new int[15, 10]   //初始化迷宫
		{
			{1,1,1,1,1,1,1,1,1,1},
			{1,0,1,1,1,0,0,0,1,1},
			{1,0,0,1,1,0,1,0,1,1},
			{1,0,0,0,0,0,1,0,1,1},
			{1,1,0,1,0,1,1,0,1,1},
			{1,1,0,1,0,0,0,0,1,1},
			{1,0,0,0,1,1,1,0,1,1},
			{1,1,0,0,0,0,0,0,1,1},
			{1,1,0,1,1,1,0,0,0,1},
			{1,1,0,0,1,1,1,0,1,1},
            {1,1,1,0,0,0,0,0,1,1},
            {1,1,1,1,0,0,1,0,1,1},
            {1,0,0,0,0,1,1,0,0,1},
            {1,0,1,1,1,0,1,1,0,1},
            {1,1,1,1,1,1,1,1,1,1}
		};
}

接下来是A*算法的使用代码:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public enum Grids3DType { 
    Normal,
    Obstacle,
    Start,
    End
}

public class Grids_3D : IComparable
{
    public int x;
    public int y;
    public int G;
    public int F;
    public int H;
    public Grids_3D fatherMode;
    public Grids3DType gridsType;
    public int CompareTo(object obj)
    {
        Grids_3D grid = (Grids_3D)obj;
        if (this.F
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的迷宫算法的C语言实现,但是最路径需要使用更复杂的算法,比如Dijkstra算法或A*算法。以下是一个基于BFS算法的迷宫程序,可记录最路径: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_ROW 10 #define MAX_COL 10 int maze[MAX_ROW][MAX_COL] = { {0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0, 0} }; typedef struct { int row, col; } Position; Position queue[MAX_ROW * MAX_COL]; int head = 0, tail = 0; void enqueue(Position p) { queue[tail++] = p; } Position dequeue(void) { return queue[head++]; } bool is_empty(void) { return head == tail; } void print_maze(void) { for (int i = 0; i < MAX_ROW; i++) { for (int j = 0; j < MAX_COL; j++) { printf("%d ", maze[i][j]); } printf("\n"); } printf("*************\n"); } void visit(int row, int col, Position pre) { Position visit_pos; visit_pos.row = row; visit_pos.col = col; maze[row][col] = 2; maze[pre.row][pre.col] = 2; enqueue(visit_pos); } int main(void) { Position p = {0, 0}; enqueue(p); maze[p.row][p.col] = 2; while (!is_empty()) { Position cur = dequeue(); if (cur.row == MAX_ROW - 1 && cur.col == MAX_COL - 1) { printf("Find the exit.\n"); break; } if (cur.row > 0 && maze[cur.row - 1][cur.col] == 0) { visit(cur.row - 1, cur.col, cur); } if (cur.row < MAX_ROW - 1 && maze[cur.row + 1][cur.col] == 0) { visit(cur.row + 1, cur.col, cur); } if (cur.col > 0 && maze[cur.row][cur.col - 1] == 0) { visit(cur.row, cur.col - 1, cur); } if (cur.col < MAX_COL - 1 && maze[cur.row][cur.col + 1] == 0) { visit(cur.row, cur.col + 1, cur); } print_maze(); } return 0; } ``` 在此代码中,我们使用BFS算法来遍历迷宫找出。我们用一个队列来存储当前待访问的位置,然后找到周围的可访问位置并将其加入队列。一旦我们找到出口,程序就会结束。在visit函数中,我们将当前位置和先前位置标记为2,以便在迷宫中显示路径。程序运行时,它会打印迷宫及其当前状态,以便我们可以跟踪其进展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值