【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索

http://blog.csdn.net/xiajun07061225/article/details/8018226

状态空间搜索,如果按专业点的说法就是将问题求解过程表现为从初始状态到目标状态寻找这个路径的过程。通俗点说,就是在解一个问题时,找到一条解题的过程可以从求解的开始到问题的结果(好象并不通俗哦)。由于求解问题的过程中分枝有很多,主要是求解过程中求解条件的不确定性,不完备性造成的,使得求解的路径很多这就构成了一个图,我们说这个图就是状态空间问题的求解实际上就是在这个图中找到一条路径可以从开始到结果。这个寻找的过程就是状态空间搜索。

  常用的状态空间搜索有深度优先和广度优先。下面就分别介绍这两张遍历算法。


一、深度优先遍历(DFS:Depth-First-Search)


深度优先是按照一定的顺序查找完一个分支,再查找另外一个分支,直到找到目标为止。图一为一个二叉树深度遍历的例子。

《【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索》

图一 二叉树深度遍历实例

下面以一个实际的迷宫地图,来看一下深度优先算法在程序中如何实现。迷宫的布局如图二。

《【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索》

图二 迷宫地图

游戏规则是:

有一个起始点和终止点,分别位于图中的左上角和右下角。迷宫中白色方格是可通过区域,黑色方格是不可通过区域,迷宫以外的区域也是不可通过区域。

算法思想:

首先定义一些变量来记录游戏中的状态信息:

二维数组:block:记录迷宫的布局。0为可通过,1代表不可通过。

dx、dy:记录移动的方向:左、右、上、下四种情况下x、y坐标需要做的更改。

table二维数组:记录某个坐标上的方格是否达到过。


函数解释:

test:判断是否已经达到目标位置

actOK:判断当前移动方向是否合理,排除不合理的情况:越界、是否有障碍等等。

back:当前路径无法继续下去,执行回退操作,更改相应变量。

DFS:核心函数,执行深度优先遍历操作。在每一个方格点A处,接下来都有4种走法,针对4种方向,依次判断,如果第一种方向合理,则移动到下一格,进行相同的操作。如果继续下去不能走到终点,则最终回退到方格点A,继续第二种方向,重复操作。如果4中方向都无法达到目标,则搜寻失败。

 

具体代码(C++编程实现):

[cpp] 
view plain
copy
print
?

  1. //迷宫寻路:深度优先算法  
  2.   
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. const int width = 10;//宽度  
  8. const int height = 10;//高度  
  9. //四种移动方向(左、右、上、下)对x、y坐标的影响  
  10. //x坐标:竖直方向,y坐标:水平方向  
  11. int dx[4] = {0,0,-1,1};  
  12. int dy[4] = {-1,1,0,0};  
  13.   
  14. //障碍表  
  15. int block[height][width] = {  
  16.     0,1,0,0,0,0,0,0,0,0,  
  17.     0,1,1,0,1,1,1,0,0,0,  
  18.     0,0,0,0,0,0,0,0,0,0,  
  19.     1,1,1,0,1,0,0,0,0,0,  
  20.     0,1,0,0,1,0,1,1,1,0,  
  21.     0,1,0,0,1,1,1,1,1,0,  
  22.     0,0,0,1,1,0,0,0,1,0,  
  23.     0,1,0,0,0,0,1,0,1,0,  
  24.     0,1,1,1,0,1,1,0,1,1,  
  25.     0,0,0,0,0,0,1,0,0,0,  
  26. };  
  27.   
  28. const int maxLevels = 1000;//最大移动步数  
  29. int maxAct = 4;//移动方向总数  
  30. int table[width][height] = {0};//标记是否已达到  
  31. int level = -1;//第几步  
  32. int levelComplete = 0;//这一步的搜索是否完成  
  33. int allComplete = 0;//全部搜索是否已完成  
  34. int Act[maxLevels] = {0};//每一步的移动方向(1、2、3、4)  
  35.   
  36. int x = 0,y = 0;//现在的坐标  
  37. int targetX = height – 1,targetY = width – 1;//目标坐标  
  38.   
  39. void test()//测试是否已达到目标  
  40. {  
  41.     if (x == targetX && y == targetY)  
  42.     {  
  43.         levelComplete = allComplete = 1;  
  44.         cout << “Get to destination Success” << endl;  
  45.     }  
  46. }  
  47.   
  48. int actOK()//判断移动方向是否合理  
  49. {  
  50.     int nextX = x + dx[Act[level] – 1];  
  51.     int nextY = y + dy[Act[level] – 1];  
  52.   
  53.     if (Act[level] > maxAct)//方向是否错误  
  54.         return 0;  
  55.     if(nextX >= height || nextX < 0)//x坐标是否越界  
  56.         return 0;  
  57.     if(nextY >= width || nextY < 0)//y坐标是否越界  
  58.         return 0;  
  59.     if(table[nextX][nextY] == 1)//是否已达到过  
  60.         return 0;  
  61.     if(block[nextX][nextY] == 1)//是否有障碍  
  62.         return 0;  
  63.   
  64.     x = nextX;  
  65.     y = nextY;//移动  
  66.     table[nextX][nextY] = 1;//标记已达到  
  67.     return 1;  
  68. }  
  69.   
  70. void back()  
  71. {  
  72.     table[x][y] = 0;  
  73.     x -= dx[Act[level – 1] – 1];  
  74.     y -= dy[Act[level – 1] – 1];//回退到原来的坐标  
  75.     Act[level] = 0;//清除方向  
  76.     –level;//回到上一层  
  77. }  
  78.   
  79. //深度优先搜索  
  80. void DFS()  
  81. {  
  82.     table[x][y] = 1;  
  83.     while(!allComplete)  
  84.     {  
  85.         ++level;//搜索下一步  
  86.         levelComplete = 0;//这一步的搜索还未完成  
  87.         while(!levelComplete)  
  88.         {  
  89.             ++Act[level];//改变移动方向  
  90.             if (actOK())//方向合理  
  91.             {  
  92.                 test();  
  93.                 levelComplete = 1;//该步搜索完成  
  94.             }  
  95.             else  
  96.             {  
  97.                 if (Act[level] > maxLevels)//已经搜索完所有方向  
  98.                 {  
  99.                     back();//回退,到上一个分支  
  100.                 }  
  101.                 if (level < 0)//全部搜索完,仍然没有搜索到目标  
  102.                 {  
  103.                     levelComplete = allComplete = 1;//退出  
  104.                 }  
  105.             }  
  106.         }  
  107.     }  
  108. }  
  109.   
  110. void print()  
  111. {  
  112.     cout << “The path is “ << endl;  
  113.     for (int i = 0;i < height;++i)  
  114.     {  
  115.         for (int j = 0;j < width;++j)  
  116.         {  
  117.             /*cout << table[i][j] << ” “;*/  
  118.             if(table[i][j])  
  119.                 cout << “1 “;  
  120.             else cout << ”  “;  
  121.         }  
  122.         cout << endl;  
  123.     }  
  124. }  
  125.   
  126. int main(){  
  127.     DFS();  
  128.     print();  
  129. }  

针对上面的迷宫地图的运行结果:

《【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索》

二、广度优先遍历(BFS:Breadth-First-Search)


广度优先是从初始状态一层一层向下找,直到找到目标为止。非常类似于树的层次遍历。下图是一个广度优先搜索的示意图:
《【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索》

广度优先搜索算法思想:
在这里采用一个辅助数据结构:队列。
(1)顶点v入队列。
(2)当队列非空时则继续执行,否则算法结束。
(3)出队列取得队头顶点v;访问顶点v并标记顶点v已被访问。
(4)查找顶点v的第一个邻接顶点col。
(5)若v的邻接顶点col未被访问过的,则col入队列。
(6)继续查找顶点v的另一个新的邻接顶点col,转到步骤(5)。直到顶点v的所有未被访问过的邻接点处理完。转到步骤(2)。



下面仍然以深度优先搜索算法中的迷宫为例,编写程序实现广度优先搜索算法。
代码(C++实现):

[cpp] 
view plain
copy
print
?

  1. //迷宫寻路:广度优先搜索  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. const int rows = 10;//行数  
  6. const int cols = 10;//列数  
  7. const int numDirections = 4;//每一步,下一步可以走的方向:4个  
  8.   
  9. //四种移动方向(左、右、上、下)对x、y坐标的影响  
  10. //x坐标:竖直方向,y坐标:水平方向  
  11. const char dx[numDirections] = {0,0,-1,1};  
  12. const char dy[numDirections] = {-1,1,0,0};  
  13.   
  14. //障碍表  
  15. char block[rows][cols] = {  
  16.     0,1,0,0,0,0,0,0,0,0,  
  17.     0,1,1,0,1,1,1,0,0,0,  
  18.     0,0,0,0,0,0,0,0,0,0,  
  19.     1,1,1,0,1,0,0,0,0,0,  
  20.     0,1,0,0,1,0,1,1,1,0,  
  21.     0,1,0,0,1,1,1,1,1,0,  
  22.     0,0,0,1,1,0,0,0,1,0,  
  23.     0,1,0,0,0,0,1,0,1,0,  
  24.     0,1,1,1,0,1,1,0,1,1,  
  25.     0,0,0,0,0,0,1,0,0,0,  
  26. };  
  27.   
  28. char path[rows][cols] = {0};//记录路径  
  29.   
  30. int startX = 0,startY = 0;//起始点坐标  
  31. int endX = rows – 1,endY = cols – 1;//目标点坐标  
  32.   
  33. //保存节点位置坐标的数据结构  
  34. typedef struct tagQNode{  
  35.     char x,y;  
  36.     int parentNode;//父节点索引  
  37. }QNode;  
  38.   
  39. //打印路径  
  40. void printPath()  
  41. {  
  42.     cout << “Success : the path is “ << endl;  
  43.     for (int i = 0;i < rows;++i)  
  44.     {  
  45.         for (int j = 0;j < cols;++j)  
  46.         {  
  47.             if (1 == path[i][j])  
  48.             {  
  49.                 cout << “1 “;  
  50.             }  
  51.             else  
  52.                 cout << ”  “;  
  53.         }  
  54.         cout << endl;  
  55.     }  
  56. }  
  57.   
  58. void BFS()  
  59. {  
  60.     int num = rows * cols;  
  61.     //利用数组来模拟队列  
  62.     QNode *queue = (QNode *)malloc(num * sizeof(QNode));  
  63.     //起始点入队列  
  64.     queue[0].x = queue[0].y = 0;  
  65.     queue[0].parentNode = -1;//起始点没有父节点  
  66.   
  67.     int front = 0,rear = 1;//队列的头和尾  
  68.     while(front != rear)//队列不为空  
  69.     {  
  70.         for (int i = 0;i < numDirections;++i)  
  71.         {  
  72.             char nextX,nextY;//下一步的坐标  
  73.             nextX = queue[front].x + dx[i];  
  74.             nextY = queue[front].y + dy[i];  
  75.             //下一个节点可行  
  76.             if (nextX >= 0 && nextX < rows &&  
  77.                 nextY >= 0 && nextY < cols &&  
  78.                 0 == block[nextX][nextY])  
  79.             {  
  80.                 //寻找到目标点  
  81.                 if (nextX == endX && nextY == endY)  
  82.                 {  
  83.                     //生成路径  
  84.                     path[nextX][nextY] = 1;  
  85.                     int tempParentIndex = front;  
  86.                     while(tempParentIndex != -1)  
  87.                     {  
  88.                         path[queue[tempParentIndex].x][queue[tempParentIndex].y] = 1;  
  89.                         tempParentIndex = queue[tempParentIndex].parentNode;  
  90.                     }  
  91.                     printPath();  
  92.                 }  
  93.                 //入栈  
  94.                 queue[rear].x = nextX;  
  95.                 queue[rear].y = nextY;  
  96.                 queue[rear].parentNode = front;  
  97.                 ++rear;  
  98.                 //标记此点已被访问  
  99.                 block[nextX][nextY] = 1;  
  100.             }  
  101.         }  
  102.         ++front;  
  103.     }  
  104. }  
  105.   
  106. int main()  
  107. {  
  108.     BFS();  
  109.   
  110.     //printPath();  
  111. }  

运行结果:
《【游戏编程】AI-迷宫寻路算法-深度优先搜索和广度优先搜索》

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/woshizoe/article/details/27345541
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞