poj2386——油田问题(简单搜索)

Lake Counting

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35186   Accepted: 17477

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.

Source

USACO 2004 November

这是一道很水的题

复习一下搜索,经典的油田问题,用的也是最简单的递归法。。。也要尝试一下非递归DFS和BFS的

//#include//poj不认。。。
#include
#include
#include
#include
#include
#include
#include
#include
#include
const int M=1005;
using namespace std;
char f[M][M];
bool vis[M][M];
int n,m;
bool judge(int i,int j){
    if(i<=0||j<=0||i>n||j>m||f[i][j]=='.')
        return false;
    else
        return true;
}
void dfs(int x,int y){
    f[x][y]='.';
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            int x1=x+dx;
            int y1=y+dy;
            if(0<=x1&&x1	///个人习惯了scanf,cin流输入可以避免回车问题
        getchar();//这里要吸收一个回车。。。
        for(i=0;i

栈实现(有bug,样例结果为5,目前无解。。。)

#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int M=1005;
char f[M][M];
bool vis[M][M];
int n,m;
struct point{
    int x,y;
};
stack  s;
int d[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
bool judge(int i,int j){
    if(i<=0||j<=0||i>n||j>m||f[i][j]=='.')
        return false;
    else
        return true;
}
void dfs(int x,int y){
    /*f[x][y]='.';
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            int x1=x+dx;
            int y1=y+dy;
            if(0<=x1&&x1=0&&t1.x=0&&t1.y>n>>m;
        //getchar();
        for(i=0;i>f[i][j];
            }
           // getchar();
        }
        int ans=0;
        for(i=0;i


BFS:

#include
#include
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int M=1005;
char f[M][M];
bool vis[M][M];
int n,m;
struct point{
    int x,y;
};
queue  q;
int d[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
bool judge(int i,int j){
    if(i<0||j<0||i>n-1||j>m-1||f[i][j]=='.')
        return false;
    else
        return true;
}
void bfs(int x,int y){
    point p;
    p.x=x;p.y=y;
    q.push(p);
    f[x][y]='.';    //更改遍历过的位置的状态
    int i,j,k;
    while(!q.empty()){
        point t1;
        t1=q.front();
        q.pop();
        for(i=0;i<8;i++){
            point t2;
            t2.x=t1.x+d[i][0];
            t2.y=t1.y+d[i][1];
            if(judge(t2.x,t2.y)){
                q.push(t2);
                f[t2.x][t2.y]='.';	//更改遍历过的点的状态
            }
        }

    }
    return;
}
int main(){
    freopen("F://1.txt","r",stdin);
    int i,j,k;
    while(!q.empty()){
        q.pop();
    }
    //scanf("%d %d",&n,&m);
    cin>>n>>m;
        //getchar();
        for(i=0;i>f[i][j];
            }
           // getchar();
        }
        int ans=0;
        for(i=0;i



你可能感兴趣的:(ACM--POJ)