zoukankan      html  css  js  c++  java
  • DFS路径规划

      1 #include <stdio.h>
      2 #include <iostream>
      3 
      4 int width = 6;
      5 int height = 5;
      6 int map[5][6] = { {0,0,0,0,0,0},
      7                            {1,0,0,1,0,1},
      8                            {0,0,1,0,0,0},
      9                            {1,0,0,1,0,1},
     10                            {0,1,0,0,0,0} };
     11 
     12 int path[20] = { 0 }; //0--stop,1--forward,2--left,3--right,4--back 
     13 
     14 int find_path(int x, int y, int flag,   //x,y为在map中的位置,flag代表之前走的方向(0--上,1--下,2--左,3--右),
     15     int x_,                  //x代表path的下标 
     16     int target_x, int target_y)  //target_x,target_y代表要找到的具体位置 
     17 {
     18     map[x][y] = 1;
     19     int temp;
     20     int flag_ = flag;
     21     int x__ = x_;
     22     if (x == target_x && y == target_y)
     23     {
     24         return x_;
     25     }
     26 
     27     if (flag == 0) {
     28         if (x > 0) {
     29             if (map[x - 1][y] == 0) {
     30                 path[x_] = 1; 
     31                 temp = find_path(x - 1, y, flag, x_ + 1, target_x, target_y);
     32                 if (temp == 0) {
     33                     map[x - 1][y] = 1;
     34                     flag = flag_;
     35                     x_ = x__;
     36                 }
     37                 else {
     38                     return temp;
     39                 }
     40             }
     41         }
     42 
     43         if (y < width-1 ) {
     44             if (map[x][y + 1] == 0) {
     45                 path[x_] = 3;
     46                 path[++x_] = 1; 
     47                 //if (flag == 2) { path[x_] = 4; }
     48                 flag = 3;
     49                 temp = find_path(x, y + 1, flag, x_ + 1, target_x, target_y);
     50                 if (temp == 0) {
     51                     map[x][y + 1] = 1;
     52                     flag = flag_;
     53                     x_ = x__;
     54                 }
     55                 else { return temp; }
     56             }
     57         }
     58 
     59         if (y > 0) {
     60             if (map[x][y - 1] == 0) {
     61                 path[x_] = 2;
     62                 path[++x_] = 1;
     63                 flag = 2;
     64                 temp = find_path(x, y - 1, flag, x_ + 1, target_x, target_y);
     65                 if (temp == 0) {
     66                     map[x][y - 1] = 1;
     67                     flag = flag_;
     68                     x_ = x__;
     69                 }
     70                 else {
     71                     return temp;
     72                 }
     73             }
     74         }
     75     }
     76 
     77 
     78     if (flag == 1) {
     79         if (x < height - 1) {
     80             if (map[x + 1][y] == 0) {
     81                  path[x_] = 1; 
     82                 temp = find_path(x + 1, y, flag, x_ + 1, target_x, target_y);
     83                 if (temp == 0) {
     84                     map[x + 1][y] = 1;
     85                     flag = flag_;
     86                     x_ = x__;
     87                 }
     88                 else {
     89                     return temp;
     90                 }
     91             }
     92         }
     93 
     94         if (y < width - 1) {
     95             if (map[x][y + 1] == 0) {
     96                 path[x_] = 2;
     97                 path[++x_] = 1;
     98                 flag = 3;
     99                 temp = find_path(x, y + 1, flag, x_ + 1, target_x, target_y);
    100                 if (temp == 0) {
    101                     map[x][y + 1] = 1;
    102                     flag = flag_;
    103                     x_ = x__;
    104                 }
    105                 else { return temp; }
    106             }
    107         }
    108 
    109         if (y > 0) {
    110             if (map[x][y - 1] == 0) {
    111                 path[x_] = 3;
    112                 path[++x_] = 1;
    113                 flag = 2;
    114                 temp = find_path(x, y - 1, flag, x_ + 1, target_x, target_y);
    115                 if (temp == 0) {
    116                     map[x][y - 1] = 1;
    117                     flag = flag_;
    118                     x_ = x__;
    119                 }
    120                 else {
    121                     return temp;
    122                 }
    123             }
    124         }
    125     }
    126 
    127     if (flag == 2) {
    128         if (y > 0) {
    129             if (map[x][y - 1] == 0) {
    130                 path[x_] = 1; 
    131                 temp = find_path(x, y - 1, flag, x_ + 1, target_x, target_y);
    132                 if (temp == 0) {
    133                     map[x][y - 1] = 1;
    134                     flag = flag_;
    135                     x_ = x__;
    136                 }
    137                 else {
    138                     return temp;
    139                 }
    140             }
    141         }
    142 
    143         if (x > 0) {
    144             if (map[x - 1][y] == 0) {
    145                 path[x_] = 3;
    146                 path[++x_] = 1;
    147                 flag = 0;
    148                 temp = find_path(x - 1, y, flag, x_ + 1, target_x, target_y);
    149                 if (temp == 0) {
    150                     map[x - 1][y] = 1;
    151                     flag = flag_;
    152                     x_ = x__;
    153                 }
    154                 else {
    155                     return temp;
    156                 }
    157             }
    158         }
    159 
    160         if (x < height - 1) {
    161             if (map[x + 1][y] == 0) {
    162                 path[x_] = 2; 
    163                 path[++x_] = 1; 
    164                 flag = 1;
    165                 temp = find_path(x + 1, y, flag, x_ + 1, target_x, target_y);
    166                 if (temp == 0) {
    167                     map[x + 1][y] = 1;
    168                     flag = flag_;
    169                     x_ = x__;
    170                 }
    171                 else {
    172                     return temp;
    173                 }
    174             }
    175         }
    176     }
    177 
    178     if (flag == 3) {
    179 
    180         if (y < width - 1) {
    181             if (map[x][y + 1] == 0) {
    182                 path[x_] = 1;
    183                 temp = find_path(x, y + 1, flag, x_ + 1, target_x, target_y);
    184                 if (temp == 0) {
    185                     map[x][y + 1] = 1;
    186                     flag = flag_;
    187                     x_ = x__;
    188                 }
    189                 else { return temp; }
    190             }
    191         }
    192 
    193         if (x > 0) {
    194             if (map[x - 1][y] == 0) {
    195                 path[x_] = 2;
    196                 path[++x_] = 1;
    197                 flag = 0;
    198                 temp = find_path(x - 1, y, flag, x_ + 1, target_x, target_y);
    199                 if (temp == 0) {
    200                     map[x - 1][y] = 1;
    201                     flag = flag_;
    202                     x_ = x__;
    203                 }
    204                 else {
    205                     return temp;
    206                 }
    207             }
    208         }
    209 
    210         if (x < height - 1) {
    211             if (map[x + 1][y] == 0) {
    212                 path[x_] = 3;
    213                 path[++x_] = 1; 
    214                 flag = 1;
    215                 temp = find_path(x + 1, y, flag, x_ + 1, target_x, target_y);
    216                 if (temp == 0) {
    217                     map[x + 1][y] = 1;
    218                     flag = flag_;
    219                     x_ = x__;
    220                 }
    221                 else {
    222                     return temp;
    223                 }
    224             }
    225         }
    226     }
    227     //map[x][y] = 0;
    228     return 0;
    229 }
    230 
    231 int main() {
    232     int x, y, x_, y_;
    233     int flag;
    234     for (int i = 0; i < 5; i++) {
    235         for (int j = 0; j < 6; j++) {
    236             printf_s("%d ", map[i][j]);
    237         }
    238         printf_s("
    ");
    239     }
    240     printf_s("请输入您的起点: ");
    241     scanf_s("%d%d", &x, &y);
    242     printf_s("请输入您的终点:");
    243     scanf_s("%d%d", &x_, &y_);
    244     printf_s("请输入默认的初始方向(0 -- 上,1 -- 下,2 -- 左,3 -- 右)");
    245     scanf_s("%d", &flag);
    246     x = find_path(x, y, flag, 0, x_, y_);
    247     for (int i = 0; i < x; i++) {
    248         if (path[i] == 0) break;
    249         if (path[i] == 1) printf_s("forward
    ");
    250         if (path[i] == 2) printf_s("left
    ");
    251         if (path[i] == 3) printf_s("right
    ");
    252     }
    253     system("pause");
    254     return 0;
    255 }
  • 相关阅读:
    权限和分组
    验证与授权
    CSRF、XSS、clickjacking、SQL 的攻击与防御
    上下文处理器及中间件
    类和实例
    偏函数
    生成器
    迭代 复习
    函数调用 复习
    复习 条件判断,循环
  • 原文地址:https://www.cnblogs.com/19991201xiao/p/11353092.html
Copyright © 2011-2022 走看看