BFS和DFS模板

BFS

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
struct A
{
    int x,y,time;
}t,now;
int map1[350][350];
int m,n;
int vis[350][350];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
using namespace std;
int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    queue <A> q;
    vis[x][y]=1;
    now.x=x;
    now.y=y;
    now.time=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(map1[now.x][now.y]=='E')
        {
            return now.time;
        }
        for(int i=0;i<4;i++)
        {
            t.x=now.x+dir[i][0];
            t.y=now.y+dir[i][1];
            t.time=now.time+1;
            if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&vis[t.x][t.y]==0&&map1[t.x][t.y]!='#')
            {
                q.push(t);
                vis[t.x][t.y]=1;
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,c=0,d=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
    {
        getchar();
        for(j=0;j<m;j++)
        {
            scanf(" %c",&map1[i][j]);
            if(map1[i][j]=='S')
            {
                c=i;
                d=j;
            }
        }
    }
    int ans=bfs(c,d);
    printf("%d
",ans);
    return 0;
}

 样例:

4 4
. . . .
. S . #
. # . .
. . . .

DFS

G - Lake Counting

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 2386

use MathJax to parse formulas

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

#include <iostream>
using namespace std;
char mapp[101][101];
int dir[8][2]={0,1,0,-1,-1,0,1,0,-1,1,1,1,-1,-1,1,-1};
int n,m;
void DFS(int x,int y)
{

    if(x>=0&&x<m&&y>=0&&y<n&&mapp[x][y]=='W')
    {
        mapp[x][y]='.';
        for(int i=0;i<8;i++)
            DFS(x+dir[i][0],y+dir[i][1]);
    }
    return ;
}
int main()
{
    int i,j,k;
    cin>>m>>n;
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
         cin>>mapp[i][j];
    int ans=0;

    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            if(mapp[i][j]=='W')
            {
                DFS(i,j);
                ans++;
            }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/jk17211764/p/9677376.html