我是靠谱客的博主 清爽鸵鸟,这篇文章主要介绍BFS/BFS模板,现在分享给大家,希望可以做个参考。

 

DFS:

#include<bits/stdc++.h>  
using namespace std;  
#define max 100 
bool vis[max][max];  
int mp[max][max];  
int dir[4][2]={-1,0,0,1,1,0,0,-1};  
void dfs(int x,int y) {  
    vis[x][y]=1;  
    if(mp[x][y]==G) { 
     ...... return;  
     }  
     for(int i=0;i<4;i++) {  
        if())  
        dfs(x+dir[i][0],y+dir[i][1]);  
                vis[][]=0;  
    } 
     return; 
}  
int main() { ......  
     if( ) 
    dfs(); 
    vis[][]=0; return 0;     
}  
BFS:
#include<bits/stdc++.h>  
using namespace std;  
#definemaxn 100  
bool vis[max][max];  
int dir[4][2]={-1,0,0,1,1,0,0,-1}; //(具体情况设置)  
struct state{  
    int x,y;  
    int step;  
}a;  
queue<State>q;  
bool check(state s){  
    if(...) return 1;  
    else return 0;  
}  
void bfs(state st){ 
    state now,next;  
     st.step=0;  
     q.push(st);  
     vis[st.x][st.y]=1;  
    while(!q.empty()){  
    now=q.front();  
    if(now==最终状态){ 
     ...... return; } 
      for(int i=0;i<4;i++){ 
        next.x=now.x+dir[i][0];  
        next.y=now.y+dir[i][1];  
        next.stepr=now.step+1;  
        if(check(next)){  
        q.push(next);  
        vis[next.x][next.y]=1;  
        }  
      }  
    q.pop();  
    }  
}

 

 

最后

以上就是清爽鸵鸟最近收集整理的关于BFS/BFS模板的全部内容,更多相关BFS/BFS模板内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(130)

评论列表共有 0 条评论

立即
投稿
返回
顶部