DFS与BFS模板

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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值