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;

}


迷宫问题(2)

Time Limit: 1 Second(s)    Memory Limit: 32 MB

Total Submission(s): 89   Accepted Submission(s): 49
Problem Description

一天,小明不小心进入了一个迷宫,现在请你帮助他判断能否出走出迷宫,如果可能,则输出一共有多少种不同的走法(对于某种特定的走法,必须保证不能多次走到同一个位置). 如果不能走到出口,则输出impossible. 每次走只能是上下左右4个方向.


Input

每次首先2个数n,m(0<n,m<=100),代表迷宫的高和宽,然后n行,每行m个字符,
'S'代表你现在所在的位置,
'T'代表迷宫的出口,
'#'代表墙,你是不能走的,
'.'代表路,可以走.

Output

输出一共有多少种不同的走法,不能走出输出impossible.

Sample Input
4 4 S... #..# ..#. ...T 4 4 S... #..# ..#. ..#T
Sample Output
4 impossible
 
题目原址:http://cs.happylearn.net/AspNet/Question.aspx?qid=8806 
 
  1.  //DFS   
  2. #include<iostream>   
  3. using namespace std;   
  4.   
  5. int f[4][2]={1,0,0,-1,-1,0,0,1},vis[101][101];   
  6. char map[101][101];   
  7. int n,m,i,j,sx,sy,count;   
  8.   
  9. void dfs(int xx,int yy)   
  10. {   
  11.     int k,x,y;   
  12.     if(map[xx][yy]=='T')   
  13.         count++;   
  14.     for(k=0;k<4;k++)   
  15.     {   
  16.         x=xx+f[k][0];   
  17.         y=yy+f[k][1];   
  18.         if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'&&(!vis[x][y]))   
  19.         {   
  20.             vis[x][y]=1;   
  21.             dfs(x,y);   
  22.             vis[x][y]=0;   
  23.         }   
  24.     }   
  25. }   
  26.   
  27. int main()   
  28. {   
  29.     while(~scanf("%d%d",&n,&m))   
  30.     {   
  31.         getchar();   
  32.         for(i=0;i<n;i++)   
  33.         {   
  34.             for(j=0;j<m;j++)   
  35.             {   
  36.                 scanf("%c",&map[i][j]);   
  37.                 vis[i][j]=0;//初始化   
  38.                 if(map[i][j]=='S')   
  39.                 {   
  40.                     sx=i;sy=j;   
  41.                 }   
  42.             }   
  43.             getchar();   
  44.         }   
  45.         vis[sx][sy]=1;//标记走过   
  46.         count=0;   
  47.         dfs(sx,sy);   
  48.         if(count)   
  49.             printf("%d\n",count);   
  50.         else  
  51.             printf("impossible\n");   
  52.     }   
  53.     return 0;   
  54.  


0 0
原创粉丝点击