先对比一下DFS和BFS

        深度优先搜索DFS                                   宽度优先搜索BFS

明显可以看出搜索顺序不同。

DFS是搜索单条路径到底部,再回溯。

BFS是搜索近的状态,直到底部,一般在求解最短路径或者最短步数上应用。

BFS要用到队列呢。。

队列的用法看一看

map的c++用法也简单看一看  高端一点的 

 map的基本操作函数:
C++Maps 是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数

map的简单操作函数

练习题系列………………………………………………………

 

因为步数(折返)的问题撸了一天。。

如果那个点可以走,那么就推入队列,等待搜索。

 #include <stdio.h>
 #include <queue>
 #include <string.h>
 using namespace std;
 ][],step[][];
 typedef pair<int, int> P;
 struct stu{
 int x; int y; int t;
 };
 ];

 ]={, , , -, };
 ]={, , , , -};

 void zha(int M) {
     memset(map1, -, sizeof(map1));
     ; i < M; i++)
         ; k < ; k++){//five points are burst.
             int nx = ch[i].x + dx[k];
             int ny = ch[i].y + dy[k];
              && nx <  && ny >=  && ny <  && (ch[i].t < map1[ny][nx] || map1[ny][nx] == -))
                 map1[ny][nx] = ch[i].t;//give everypoint the minimum burst time.
         }
 }

 int bfs() {
 queue <P> que;
 que.push( P (, ));
 memset(step, -, sizeof(step));
 step[][]=;
 while(que.size()) {
     P p= que.front();
     que.pop();
         ){//find map1's -1 and print it
             return step[p.first][p.second];
         }
     if(step[p.first][p.second] >= map1[p.first][p.second])continue;//the point had benn burst,so jump it
     ; i < ; i++) {//this is the point of four diretions that does not been destroy.
         int nx = p.first + dx[i],ny = p.second + dy[i];
          && ny >= &&step[nx][ny] == -)//if it can arrive and does not exceed the map
             {
                  que.push(P(nx, ny));//push it to the queue and go
                  step[nx][ny]=step[p.first][p.second]+;//of course,step must add 1 when go.
             }
     }
 }
 ;
 }

 int main()
 {
     int M,a;
     while(~scanf("%d",&M))
     {
          ; i<M; i++)
             scanf("%d%d%d",&ch[i].x,&ch[i].y,&ch[i].t);
          zha(M);
          a=bfs();
          printf("%d\n",a);
     }
     ;
 }

POJ3669

题目:AOJ0558  中文 

还是要用个地图标记一下步数,不然会计算错误。

 #include <iostream>
 #include <queue>
 #include <string.h>
 using namespace std;
 typedef pair<int, int> P;
 int sx,sy,H,W,N;
 ][];
 ][];
 ] = {,,-,};
 ] = {,,,-};
 void find1(int g){
 ; i < W; i++)
     ; k < H; k++)
         )){
         if(map1[i][k] == 'S') map1[i][k] ='.';
         sx = i; sy = k;
         return ;
     }
 }

 int bfs(int n)
 {
     memset(step,-,sizeof(step));
     queue <P> que;
     que.push(P (sx,sy));
     step[sx][sy] = ;
     while(que.size()){
         int x = que.front().first, y = que.front().second;
         que.pop();
         ; i < ; i++){
             int xn = x + dx[i], yn = y + dy[i];
              && xn < W && yn >=  && yn < H && map1[xn][yn] != )
                 {
                     step[xn][yn] = step[x][y] + ;
                     ))
                         {
                             return step[xn][yn];
                         }
                     que.push(P(xn, yn));
                 }
         }
     }
 ;
 }

 int main()
 {
     cin >> W >> H >> N;
     ; i < W; i++)
         ; k < H; k++)
         cin >> map1[i][k];
     ;
     find1();
     step += bfs();
     ; i < N; i++)
     {

         find1(i);
         step += bfs(i+);
     }
     cout << step << endl;
     ;
 }

AOJ0558

题目:AOJ0121  中文

 #include <iostream>
 #include <queue>
 #include <string.h>
 #include <map>
 #include <algorithm>
 using namespace std;

 ]={ , -, , -};
 map<string, int> dp;

 void bfs(){
 queue<string> que;
 que.push(");
 dp[;
 while(que.size()){
     string now =que.front();
     que.pop();
     ;
     ; j < ; j++)
         '){
             p=j;
             break;
         }
     ; i < ; i++){
         int pn= p + d[i];
          && pn <  && !(p ==  && i == )
          && !(p == && i == )){
             string next = now;
             swap(next[p],next[pn]);
             if(dp.find(next) == dp.end()){
                 dp[next] = dp[now] + ;
                 que.push(next);
                 }
             }
         }
     }
 return ;
 }

 int main()
 {
     string line;
     bfs();
     while(getline(cin,line))
     {
      line.erase(remove(line.begin(), line.end(), ' '), line.end());
      cout << dp[line] << endl;
     }
     ;
 }

AOJ0121

挑战程序2.1.5 穷竭搜索>>宽度优先搜索的更多相关文章

  1. 挑战程序2.1.4 穷竭搜索>>深度优先搜索

      深度优先搜索DFS,从最开始状态出发,遍历一种状态到底,再回溯搜索第二种. 题目:POJ2386  思路:(⊙v⊙)嗯  和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者 ...

  2. 【算法入门】广度/宽度优先搜索(BFS)

    广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...

  3. 搜索与图论②--宽度优先搜索(BFS)

    宽度优先搜索 例题一(献给阿尔吉侬的花束) 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫. 今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔 ...

  4. [宽度优先搜索] FZU-2150 Fire Game

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  5. 【BFS宽度优先搜索】

    一.求所有顶点到s顶点的最小步数   //BFS宽度优先搜索 #include<iostream> using namespace std; #include<queue> # ...

  6. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  7. BFS算法的优化 双向宽度优先搜索

    双向宽度优先搜索 (Bidirectional BFS) 算法适用于如下的场景: 无向图 所有边的长度都为 1 或者长度都一样 同时给出了起点和终点 以上 3 个条件都满足的时候,可以使用双向宽度优先 ...

  8. 宽度优先搜索--------迷宫的最短路径问题(dfs)

    宽度优先搜索运用了队列(queue)在unility头文件中 源代码 #include<iostream>#include<cstdio>#include<queue&g ...

  9. 算法基础⑦搜索与图论--BFS(宽度优先搜索)

    宽度优先搜索(BFS) #include<cstdio> #include<cstring> #include<iostream> #include<algo ...

随机推荐

  1. ElasticSearch 自定义排序处理

    使用function_score进行分组处理,利用分组函数script_score进行自定义分值处理, 注意:使用script功能需要在配置中打开脚本功能: script.inline: on   s ...

  2. Jquery实现静态切换tab

    1. <div id="tabs"> <ul> <li><a href=</a></li> <li>& ...

  3. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  4. BI实施过程中的工具与服务

    成功的BI项目,不仅仅是应用了BI工具软件,还要具备完善的BI服务体系,才能称之为真正成功的商业智能bi项目. 现在的BI(商业智能)比起几年前的ERP一样,成为CIO们关注的焦点.在ERP等基础信息 ...

  5. WORD学习之插入分页符

    我们在用Word编辑文档时,不可避免的需要对文档进行页面布局,才能使得文档看起来更加美观.有时候会使用到分页符,下面就简单介绍一下分页符 分页符 主要作用: 1.若要把两段分开在两页显示时,把光标定位 ...

  6. jQuery插件的开发之$.extend(),与$.fn.extend()

        jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种 ...

  7. swift基础:第一部分:基本数据类型及结构

    首先谈点开心的:今天是周二,广州的天气格外明朗,早上上班的心情也不一样,最值得高兴事,很快到五一劳动节了,说到劳动节,放假是吧.你懂的.再来谈谈我上周的工作总结,上周可以说是黑轮压城城欲摧,甲光向日金 ...

  8. linux下flash的安装

    linux的初学者可能会遇到各种各样的问题,其中的问题就有一个,linux下的flash插件怎么安装呢? 首先前往flash官网下载好对应的文件,然后提取里面的一个叫做 libflashplayer. ...

  9. UWP消息通知

    在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...

  10. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数017·point点函数

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数017·point点函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“p ...