(算法)深度寻路

  1 #include <iostream>
  2 #include <vector>
  3 #define MAP_LENGTH 12
  4 using namespace std;
  5 
  6 struct MyPoint{//图中坐标点 
  7     int row;
  8     int col;
  9 };
 10 
 11 enum PathDir{ p_up, p_down, p_left, p_right };//寻路中点的方向 
 12 struct PathNode{//地图辅助信息 
 13     int val;//地图数据 
 14     PathDir dir;//当前路径方向 
 15     bool isFind;//该点是否被访问过 
 16 }; 
 17 
 18 
 19 int main(){
 20     int arr[MAP_LENGTH][MAP_LENGTH] = {//地图 
 21         { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
 22         { 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1 },
 23         { 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
 24         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 25         { 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1 },
 26         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 27         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 28         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 29         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 30         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
 31         { 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1 },
 32         { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
 33     };
 34     PathNode pathArr[MAP_LENGTH][MAP_LENGTH];//定义和地图数组一样大的辅助数组 
 35     for(int i = 0; i < MAP_LENGTH; ++i){
 36         for(int j = 0; j < MAP_LENGTH; ++j){//辅助数组初始化 
 37             pathArr[i][j].dir = p_up;
 38             pathArr[i][j].val = arr[i][j];
 39             pathArr[i][j].isFind = false;
 40         }
 41     }
 42     MyPoint beginPoint = {1, 1};//起点 
 43     MyPoint endPoint = {10, 10};//终点 
 44     MyPoint thisPoint;//当前位置 
 45     vector<MyPoint> ans;//用来保存路径点 
 46     ans.push_back(beginPoint);//起点入栈
 47     thisPoint = ans.back(); 
 48     while(true){
 49         switch(pathArr[thisPoint.row][thisPoint.col].dir){
 50             case p_up:
 51                 pathArr[thisPoint.row][thisPoint.col].dir = p_down;//不管向上是否能走,该点方向均要改变
 52                 if(pathArr[thisPoint.row - 1][thisPoint.col].val == 0 && 
 53                 pathArr[thisPoint.row - 1][thisPoint.col].isFind == false){//可以访问 
 54                     pathArr[thisPoint.row][thisPoint.col].isFind = true;    //该点标记为已访问 
 55                     MyPoint tempPoint = {thisPoint.row - 1, thisPoint.col};
 56                     ans.push_back(tempPoint);        //该点入栈,在栈里面保存路径 
 57                     thisPoint = tempPoint;            //当前坐标赋值为该点坐标 
 58                 } 
 59                 break;
 60             case p_down:
 61                 pathArr[thisPoint.row][thisPoint.col]. dir = p_left;//不管向下是否能走,该点方向均要改变
 62                 if(pathArr[thisPoint.row + 1][thisPoint.col].val == 0 &&
 63                 pathArr[thisPoint.row + 1][thisPoint.col].isFind == false){//可以访问 
 64                     pathArr[thisPoint.row][thisPoint.col].isFind = true;
 65                     MyPoint tempPoint = {thisPoint.row + 1, thisPoint.col}; 
 66                     ans.push_back(tempPoint);
 67                     thisPoint = tempPoint;
 68                 } 
 69                 break;
 70             case p_left:
 71                 pathArr[thisPoint.row][thisPoint.col].dir = p_right;//不管向左是否能走,该点方向均要改变
 72                 if(pathArr[thisPoint.row][thisPoint.col - 1].val == 0 && 
 73                 pathArr[thisPoint.row][thisPoint.col - 1].isFind == false){//可以访问 
 74                     pathArr[thisPoint.row][thisPoint.col].isFind = true;    //该点标记为已访问 
 75                     MyPoint tempPoint = {thisPoint.row, thisPoint.col - 1};
 76                     ans.push_back(tempPoint);        //该点入栈 
 77                     thisPoint = tempPoint;            //当前坐标赋值为该点坐标 
 78                 } 
 79                 break;
 80             case p_right://代表当前我自己规则的最后一个方向 
 81                 pathArr[thisPoint.row][thisPoint.col].dir = p_up;//不管向左是否能走,该点方向均要改变
 82                 if(pathArr[thisPoint.row][thisPoint.col + 1].val == 0 && 
 83                 pathArr[thisPoint.row][thisPoint.col + 1].isFind == false){//可以访问 
 84                     pathArr[thisPoint.row][thisPoint.col].isFind = true;    //该点标记为已访问 
 85                     MyPoint tempPoint = {thisPoint.row, thisPoint.col + 1};
 86                     ans.push_back(tempPoint);        //该点入栈 
 87                     thisPoint = tempPoint;            //当前坐标赋值为该点坐标 
 88                 } 
 89                 else{
 90                     MyPoint tempPoint = ans.back();
 91                     //退出栈,设置为已访问,防止下次再入栈 
 92                     pathArr[tempPoint.row][tempPoint.col].isFind = true;
 93                     ans.pop_back();
 94                     if(!ans.empty())
 95                         thisPoint = ans.back();
 96                 }
 97                 break;
 98         }
 99         //找到终点 
100         if(thisPoint.row == endPoint.row && thisPoint.col == endPoint.col) break;
101         //栈空了,没有路 
102         if(ans.empty()){
103             printf("没有找到路!!!\n") ;
104             break; 
105         } 
106     }
107     while(!ans.empty()){
108         MyPoint tempPoint = ans.back();
109         printf("row = %d\t col = %d\n", tempPoint.row, tempPoint.col);
110         ans.pop_back();
111     }
112     return 0;
113 } 

 

posted @ 2018-04-06 12:01  ♚奋斗的小丑  阅读(638)  评论(0编辑  收藏  举报