bfs模板
来源:互联网 发布:淘宝评价规则 编辑:程序博客网 时间:2024/05/21 09:01
bfs模板:
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<queue>using namespace std;#define N 1000+5char map[N][N];bool visit[N][N]; //标记访问int n, m;int dx[4]={1, 0, -1, 0};int dy[4]={0, 1, 0, -1};struct point //定义结构体{ int x, y; int ncount; //记录步数};bool check(point a){ //检查是否符合要求 if(a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!visit[a.x][a.y]) //视具体情况而定 return true; else return false;}void bfs(point a){ queue<point>q; //定义队列 a.ncount=0; //步数清零 point now, next; //定义两个状态 q.push(a); //将a压入队列 visit[a.x][a.y]=1; //标记已访问 while(!q.empty()){ //判断是否为空队列 now=q.front(); //取出队首元素 if(now==G){ //出现目标状态,此时ncount最小 .......//做出相关处理 return ; } for(int i=0; i<4; i++){ next.x=now.x+dx[i]; next.y=now.y+dy[i]; //计算下一个状态 next.ncount=now.ncount+1; if(check(next)){ q.push(next); //将next压入队列 visit[next.x][next.y]=1; } } q.pop(); //移除队首元素 } return ;}int main(){ ...... //相关操作 return 0;}
0 0
- 【模板】BFS
- bfs模板
- BFS模板
- 模板,BFS
- BFS模板
- BFS 模板
- BFS模板
- bfs模板
- bfs模板
- BFS 模板
- BFS模板
- BFS模板
- bfs模板
- bfs模板
- bfs模板
- BFS模板
- BFS 模板
- bfs模板
- javaEE
- CSS圣杯布局与双飞翼布局
- 牛客网每日打卡-Java基础-20160326
- 机器学习实战--apriori
- js之this
- bfs模板
- HDU 5651 xiaoxin juju needs help
- The type javax.servlet.ServletRequest cannot be resolved. It is indirectly referenced from required
- 计算机网络协议层
- 兰州出差记之第一篇
- 变量定义与const变量定义
- HDU 5654 xiaoxin and his watermelon candy
- Laravel学习笔记-1-环境搭建
- 如何系统地自学一门Python 语言?https://mp.weixin.qq.com/s?__biz=MzA3ODY0MzEyMA==&mid=408122444&idx=2&sn=ae6f3ddd