dfs bfs
来源:互联网 发布:有那些存有网络银行 编辑:程序博客网 时间:2024/05/17 03:48
DFS:
/*
该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想。
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=100;
bool vst[maxn][maxn]; // 访问标记
int map[maxn][maxn]; // 坐标范围
int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量,(x,y)周围的四个方向
bool CheckEdge(int x,int y) // 边界条件和约束条件的判断
{
if(!vst[x][y] && ...) // 满足条件
return 1;
else // 与约束条件冲突
return 0;
}
void dfs(int x,int y)
{
vst[x][y]=1; // 标记该节点被访问过
if(map[x][y]==G) // 出现目标态G
{
...... // 做相应处理
return;
}
for(int i=0;i<4;i++)
{
if(CheckEdge(x+dir[i][0],y+dir[i][1])) // 按照规则生成下一个节点
dfs(x+dir[i][0],y+dir[i][1]);
}
return; // 没有下层搜索节点,回溯
}
int main()
{
......
return 0;
}
------------------------------------------------------华丽分割线----------------------------------------------------------------------------
BFS:
/*
该框架是2D坐标范围内做BFS设计的,使用STL实现
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=100;
bool vst[maxn][maxn]; // 访问标记
int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量
struct State // BFS队列中的状态数据结构
{
int x,y; // 坐标位置
int Step_Counter; // 搜索步数统计器
};
State a[maxn];
bool CheckState(State s) // 约束条件检验
{
if(!vst[s.x][s.y] && ...) // 满足条件
return 1;
else // 约束条件冲突
return 0;
}
void bfs(State st)
{
queue <State> q; // BFS队列
State now,next; // 定义2个状态,当前和下一个
st.Step_Counter=0; // 计数器清零
q.push(st); // 入队
vst[st.x][st.y]=1; // 访问标记
while(!q.empty())
{
now=q.front(); // 取队首元素进行扩展
if(now==G) // 出现目标态,此时为Step_Counter的最小值,可以退出即可
{
...... // 做相关处理
return;
}
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0]; // 按照规则生成下一个状态
next.y=now.y+dir[i][1];
next.Step_Counter=now.Step_Counter+1; // 计数器加1
if(CheckState(next)) // 如果状态满足约束条件则入队
{
q.push(next);
vst[next.x][next.y]=1; //访问标记
}
}
q.pop(); // 队首元素出队
}
return;
}
int main()
{
......
return 0;
一天,小明不小心进入了一个迷宫,现在请你帮助他判断能否出走出迷宫,如果可能,则输出一共有多少种不同的走法(对于某种特定的走法,必须保证不能多次走到同一个位置). 如果不能走到出口,则输出impossible. 每次走只能是上下左右4个方向.
Input
每次首先2个数n,m(0<n,m<=100),代表迷宫的高和宽,然后n行,每行m个字符,
'S'代表你现在所在的位置,
'T'代表迷宫的出口,
'#'代表墙,你是不能走的,
'.'代表路,可以走.
Output
输出一共有多少种不同的走法,不能走出输出impossible.
Sample Input
/*
该DFS框架以2D坐标范围为例,来体现DFS算法的实现思想。
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=100;
bool vst[maxn][maxn]; // 访问标记
int map[maxn][maxn]; // 坐标范围
int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量,(x,y)周围的四个方向
bool CheckEdge(int x,int y) // 边界条件和约束条件的判断
{
if(!vst[x][y] && ...) // 满足条件
return 1;
else // 与约束条件冲突
return 0;
}
void dfs(int x,int y)
{
vst[x][y]=1; // 标记该节点被访问过
if(map[x][y]==G) // 出现目标态G
{
...... // 做相应处理
return;
}
for(int i=0;i<4;i++)
{
if(CheckEdge(x+dir[i][0],y+dir[i][1])) // 按照规则生成下一个节点
dfs(x+dir[i][0],y+dir[i][1]);
}
return; // 没有下层搜索节点,回溯
}
int main()
{
......
return 0;
}
------------------------------------------------------华丽分割线----------------------------------------------------------------------------
BFS:
/*
该框架是2D坐标范围内做BFS设计的,使用STL实现
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=100;
bool vst[maxn][maxn]; // 访问标记
int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量
struct State // BFS队列中的状态数据结构
{
int x,y; // 坐标位置
int Step_Counter; // 搜索步数统计器
};
State a[maxn];
bool CheckState(State s) // 约束条件检验
{
if(!vst[s.x][s.y] && ...) // 满足条件
return 1;
else // 约束条件冲突
return 0;
}
void bfs(State st)
{
queue <State> q; // BFS队列
State now,next; // 定义2个状态,当前和下一个
st.Step_Counter=0; // 计数器清零
q.push(st); // 入队
vst[st.x][st.y]=1; // 访问标记
while(!q.empty())
{
now=q.front(); // 取队首元素进行扩展
if(now==G) // 出现目标态,此时为Step_Counter的最小值,可以退出即可
{
...... // 做相关处理
return;
}
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0]; // 按照规则生成下一个状态
next.y=now.y+dir[i][1];
next.Step_Counter=now.Step_Counter+1; // 计数器加1
if(CheckState(next)) // 如果状态满足约束条件则入队
{
q.push(next);
vst[next.x][next.y]=1; //访问标记
}
}
q.pop(); // 队首元素出队
}
return;
}
int main()
{
......
return 0;
}
迷宫问题(2)
Time Limit: 1 Second(s)
Total Submission(s): 89 Accepted Submission(s): 49
Problem Description
Input
每次首先2个数n,m(0<n,m<=100),代表迷宫的高和宽,然后n行,每行m个字符,
'S'代表你现在所在的位置,
'T'代表迷宫的出口,
'#'代表墙,你是不能走的,
'.'代表路,可以走.
Output
输出一共有多少种不同的走法,不能走出输出impossible.
Sample Input
4 4 S... #..# ..#. ...T 4 4 S... #..# ..#. ..#TSample Output
4 impossible
题目原址:http://cs.happylearn.net/AspNet/Question.aspx?qid=8806
-
//DFS - #include<iostream>
- using
namespace std; -
- int
f[4][2]={1,0,0,-1,-1,0,0,1},vis[101][101]; - char
map[101][101]; - int
n,m,i,j,sx,sy,count; -
- void
dfs( intxx, intyy) - {
-
k,x,y; -
-
count++; -
-
{ -
x=xx+f[k][0]; -
y=yy+f[k][1]; -
-
{ -
vis[x][y]=1; -
dfs(x,y); -
vis[x][y]=0; -
} -
} - }
-
- int
main() - {
-
-
{ -
getchar(); -
-
{ -
-
{ -
scanf( -
vis[i][j]=0; -
-
{ -
sx=i;sy=j; -
} -
} -
getchar(); -
} -
vis[sx][sy]=1; -
count=0; -
dfs(sx,sy); -
-
printf( -
-
printf( -
} -
0; - }
0 0
- BFS-DFS
- BFS DFS
- DFS+BFS
- BFS DFS
- DFS BFS
- DFS & BFS
- BFS,DFS
- dfs bfs
- BFS&DFS
- DFS BFS
- BFS && DFS
- BFS DFS
- DFS & BFS
- DFS&BFS
- BFS&DFS
- dfs && bfs
- dfs bfs
- BFS/DFS
- JDBC连接oracle、mysql数据库
- BZOJ 刷题记录 PART 2
- dsgdgfg
- Nginx下实现pathinfo及ThinkPHP的URL Rewrite模式支持
- Java中static、final用法小结
- dfs bfs
- 程序员生存定律--交换是职场里一切的根本
- 移动游戏后端:开发还是购买?你必须知道的6件事
- 小苹果
- 推理游戏·教授选出两个从2到9的数,让两人猜...
- Compiling a MEX file with Visual Studio
- android GridView,Gallery,ListView
- [代码阅读]gem5中的classic cache 初步(1)
- 如何在HTML网页中设置弹出窗口的办法