您的位置:首页 > 其它

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

2017-07-01 16:20 288 查看
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<bits/stdc++.h>//poj不认。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<vector>
#include<map>
#include<set>
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<n&&0<=y1&&y1<m&&f[x1][y1]=='W')
dfs(x1,y1);
}
}
return;
}
int main(){
freopen("F://1.txt","r",stdin);
int i,j,k;
scanf("%d %d",&n,&m); ///个人习惯了scanf,cin流输入可以避免回车问题
getchar();//这里要吸收一个回车。。。
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%c",&f[i][j]);
}
getchar();//还有这里。。。
}
int ans=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(f[i][j]=='W'){
dfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
return 0;
}
/*
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
*/

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int M=1005;
char f[M][M];
bool vis[M][M];
int n,m;
struct point{
int x,y;
};
stack <point> 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<n&&0<=y1&&y1<m&&f[x1][y1]=='W')
dfs(x1,y1);
}
}
return;*/
int i,j,k;
point t;
t.x=x;
t.y=y;
f[t.x][t.y]='.';
s.push(t);
while(!s.empty()){
point temp=s.top();
f[temp.x][temp.y]='.';
s.pop();
for(i=0;i<8;i++){
point t1;
t1.x=temp.x+d[i][0];
t1.y=temp.y+=d[i][1];
if(t1.x>=0&&t1.x<n&&t1.y>=0&&t1.y<m&&f[t1.x][t1.y]=='W'){
f[t1.x][t1.y]='.';
s.push(t1);
}
/*else{
s.pop();
}*/
}
}
return;
}
int main(){
freopen("F://1.txt","r",stdin);
int i,j,k;
//scanf("%d %d",&n,&m);
while(!s.empty()){
s.pop();
}
cin>>n>>m;
//getchar();
for(i=0;i<n;i++){
for(j=0;j<m;j++){
// scanf("%c",&f[i][j]);
cin>>f[i][j];
}
// getchar();
}
int ans=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(f[i][j]=='W'){
dfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
return 0;
}

BFS:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int M=1005;
char f[M][M];
bool vis[M][M];
int n,m;
struct point{
int x,y;
};
queue <point> 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<n;i++){
for(j=0;j<m;j++){
// scanf("%c",&f[i][j]);
cin>>f[i][j];
}
// getchar();
}
int ans=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(f[i][j]=='W'){
bfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: