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请教,并且被迫听一大堆好好学习的道理.
Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
Sample Input
sosoonrivergoesthemgotmoonbeginbig0
Sample Output
Yes.          Harry 可以念这个咒语:"big-got-them".
Hint
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.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0 
Sample Output
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;}


原创粉丝点击