zoukankan      html  css  js  c++  java
  • BFS() DFS() 模板

     1 /*
     2 该框架是2D坐标范围内做BFS设计的,使用STL实现
     3 */
     4 
     5 #include<cstdio>
     6 #include<cstring>
     7 #include<queue>
     8 #include<algorithm>
     9 using namespace std;
    10 
    11 const int maxn=100;
    12 
    13 bool vst[maxn][maxn];  // 访问标记
    14 
    15 int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量
    16 struct State   // BFS队列中的状态数据结构
    17 {
    18     int x,y; // 坐标位置
    19     int Step_Counter; // 搜索步数统计器
    20 };
    21 State a[maxn];
    22 
    23 bool CheckState(State s)  // 约束条件检验
    24 {
    25     if(!vst[s.x][s.y] && ...)  // 满足条件
    26         return 1;
    27     else   // 约束条件冲突
    28         return 0;
    29 }
    30 
    31 void bfs(State st)
    32 {
    33     queue <State> q; // BFS队列
    34     State now,next;  // 定义2个状态,当前和下一个
    35     st.Step_Counter=0; // 计数器清零
    36     q.push(st);   // 入队
    37     vst[st.x][st.y]=1;  // 访问标记
    38     while(!q.empty())
    39     {
    40         now=q.front(); // 取队首元素进行扩展
    41         if(now==G) // 出现目标态,此时为Step_Counter的最小值,可以退出即可
    42         {
    43             ...... // 做相关处理
    44             return;
    45         }
    46         for(int i=0;i<4;i++)
    47         {
    48             next.x=now.x+dir[i][0];  // 按照规则生成下一个状态
    49             next.y=now.y+dir[i][1];
    50             next.Step_Counter=now.Step_Counter+1;  // 计数器加1
    51             if(CheckState(next))  // 如果状态满足约束条件则入队  
    52             {
    53                 q.push(next);   
    54                 vst[next.x][next.y]=1;  //访问标记
    55             }
    56         }
    57         q.pop(); // 队首元素出队
    58     }
    59     return;
    60 }
    61 
    62 int main()
    63 {
    64     ......
    65     return 0;
    66 }

     1 /*
     2 该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想。
     3 */
     4 
     5 #include<cstdio>
     6 #include<cstring>
     7 #include<cstdlib>
     8 using namespace std;
     9 
    10 const int maxn=100;
    11 
    12 bool vst[maxn][maxn];    // 访问标记
    13 
    14 int map[maxn][maxn]; // 坐标范围
    15 int dir[4][2]={0,1,0,-1,1,0,-1,0};  // 方向向量,(x,y)周围的四个方向
    16 
    17 bool CheckEdge(int x,int y) // 边界条件和约束条件的判断
    18 {
    19     if(!vst[x][y] && ...)  // 满足条件
    20         return 1;
    21     else   // 与约束条件冲突
    22         return 0;
    23 }
    24 
    25 void dfs(int x,int y)
    26 {
    27     vst[x][y]=1; // 标记该节点被访问过
    28     if(map[x][y]==G) // 出现目标态G
    29     {
    30         ......  // 做相应处理
    31         return;
    32     }
    33     for(int i=0;i<4;i++)
    34     {
    35         if(CheckEdge(x+dir[i][0],y+dir[i][1]))  // 按照规则生成下一个节点
    36             dfs(x+dir[i][0],y+dir[i][1]);
    37     }
    38     return;  // 没有下层搜索节点,回溯
    39 }
    40 
    41 int main()
    42 {
    43     ......
    44     return 0;
    45 }






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/XBWer/p/2555221.html
Copyright © 2011-2022 走看看