BFS的基本例题
来源:互联网 发布:用java输出梯形 编辑:程序博客网 时间:2024/05/19 00:56
DFS----深度优先搜索
基本框架
DFS(int x, int y, int z){ for(遍历分支) DFS(a, b, c)}一般用vector容器
例题:
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
sosoonrivergoesthemgotmoonbeginbig0
Yes. Harry 可以念这个咒语:"big-got-them".
Hint思路:首先将首尾字母转换为数字以便存储在容器中,从首字母为b的开始,一直找到尾字母为m停止,注意避免死循环,可以通过定义visit数组;
#include <iostream>#include <cstdio>#include <vector>#include <cstring>using namespace std;bool vis[30];//判重vector<int> e[30];bool flag;void dfs(int x){ vis[x] = 1; if(x == 12) { flag = 1; return; } for(auto &v : e[x]) { if (!vis[v]) dfs(v);//利用auto实现自动遍历 }}int main(){ int len, be, en, i; string str; while(cin >> str) { flag = 0; memset(vis, 0,sizeof(vis)); for(i = 0; i < 30; i++) e[i].clear(); while(str[0] != '0') { len = str.length(); be = (int)(str[0] - 'a'); en = (int)(str[len - 1] - 'a');//将首尾字母-a并强制转化为数字 e[be].push_back(en);//将尾字母储存于首字母所在的容器 cin >> str; } dfs(1);//b转化为的数字是1 if(flag) printf("Yes.\n"); else printf("No.\n"); }}
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
0122思路:首先要找到@符号,再寻找它的8个方向(不考虑矩阵以外的方向),注意不能重复寻找
#include <iostream>#include <vector>#include <cstdio>#include <cstring>using namespace std;char a[100][100];int m, n;bool vis[100][100];int loop[8][2] = {1, 0, 1, 1, 1, -1, 0, 1, 0, -1, -1, 0, -1, 1, -1, -1};//此处注明8个方向bool check(int x, int y){ if(x >= 1 && x <= m && y >= 1 && y <= n && !vis[x][y] && a[x][y] == '@')//判断8个方向是否在矩阵内且没有拜访过 return true; else return false;}void dfs(int x, int y){ vis[x][y] = 1; int tem_x, tem_y, i; for(i = 0; i < 8; i++) { tem_x = x + loop[i][0]; tem_y = y + loop[i][1];//找到8个方向的位置 if(check(tem_x, tem_y)) { dfs(tem_x, tem_y); } }}int main(){ int i, j, oil; while(scanf("%d %d",&m, &n),m+n) { memset(vis, 0, sizeof(vis));//注意清空 for(i = 1; i <= m; i++) for(j = 1; j <= n; j++) cin>>a[i][j]; oil = 0; for(i = 1; i <= m; i++){ for(j = 1; j <= n; j++){ if(a[i][j] == '@' && !vis[i][j])//找@且没有拜访过 { dfs(i, j); oil++; } } } printf("%d\n", oil); } return 0;}
阅读全文
0 0
- BFS的基本例题
- BFS例题
- 紫书上的例题,关于BFS
- JAVA的基本例题(1)
- BFS的基本模版
- BFS和队列例题
- BFS例题:A计划
- BFS例题详解
- 例题6-7 树的层次遍历 BFS遍历
- 数据表的基本操作 例题 stage1-6
- bfs例题:血色先锋军
- BFS经典例题Isenbaev number
- BFS经典例题:Maze迷宫
- BFS经典例题合集
- splay的基本操作及相关例题 未完待续
- 关于基本勾股数规律的探讨总结与例题!
- UVa524 例题7-9 万圣节后的早晨 (单向+双向)BFS
- POJ 2251 Dungeon Master (BFS例题)
- Multiplication Puzzle (poj 1651)
- node使用zlib模块通过管道压缩数据
- LDA主题模型
- Intellij IDEA 15中文输入框不跟随怎么办?
- Git 学习(一)
- BFS的基本例题
- 队列的操作及实现
- Win7启动Virtualbox时提示failed to open a session for the virtural machine
- Pyhon3 在ubuntu上的使用
- MATLAB拟合函数使用说明
- Ajax+SpringMVC requestBody
- javascript基础练习-批量修改样式
- 基于QML的播放器实现
- tcpdump抓包技巧和使用