深 广搜 入门

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.
 
题意 :
  W 代表 水 ,  ' . ' 代表空地 , 问有多少个水坑 。
 
深搜广搜都可以做这个题
 
深搜代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

char mp[105][105];
int n, m;
bool check(int x, int y){
    if (x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == 'W') return true;
    else return false;
}

void dfs(int x, int y){
    mp[x][y] = '.';
        
    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            int xx = x + i;
            int yy = y + j;
            if (check(xx, yy)){
                dfs(xx, yy);
            }
        }
    } 
    return;
}

int main() { 
    
    while (~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; i++){
            scanf("%s", mp[i] + 1);
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if (mp[i][j] == 'W'){
                    dfs(i, j);
                    ans++;
                }
            }
        } 
        printf("%d\n", ans);
    }

    return 0;
}

广搜 :

/*
 * Author:  ry 
 * Created Time:  2017/10/14 9:38:03
 * File Name: 2.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

char mp[105][105];
int n, m;
typedef pair<int, int>p;

bool check(int x, int y){
    if (x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == 'W'){
        return true;    
    }
    else return false;
}

void bfs(int x, int y){
    queue<p>que;
    mp[x][y] = '.';
    que.push(p(x,y));
    
    while (!que.empty()){
        p v = que.front();
        que.pop();
        
        for(int i = -1; i <= 1; i++){
            for(int j = -1; j <= 1; j++){
                int xx = v.first + i;
                int yy = v.second + j;
                if (check(xx, yy)){
                    mp[xx][yy] = '.';
                    que.push(p(xx,yy));
                }
            }
        }
    }
}

int main() {
    
     while (~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; i++){
            scanf("%s", mp[i] + 1);
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if (mp[i][j] == 'W'){
                    bfs(i, j);
                    ans++;
                }
            }
        } 
        printf("%d\n", ans);
    }

    return 0;
}

 

posted @ 2017-10-14 09:51  楼主好菜啊  阅读(299)  评论(0编辑  收藏  举报