首页 > 代码库 > Lake Counting
Lake Counting
Problem 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.
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.
* 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.
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
题目大意: W表示水域,可以连接8个方向,要求你求出约翰的田里能形成几个水域
思路:这是一个典型的搜索题目,可以考虑深搜和广搜。
//1009 Lake Counting 有 DFS写的 //深搜
#include <stdio.h>
#include <string.h>
题目大意: W表示水域,可以连接8个方向,要求你求出约翰的田里能形成几个水域
思路:这是一个典型的搜索题目,可以考虑深搜和广搜。
//1009 Lake Counting 有 DFS写的 //深搜
#include <stdio.h>
#include <string.h>
const int maxn = 100 + 5;
int dx[8]= {1,1,1,-1,-1,-1,0,0};
int dy[8]= {1,0,-1,1,0,-1,1,-1};
//定义八个方向
char a[maxn][maxn]; //存储地图,以及标记该点能否再访问
int n, m;
int dy[8]= {1,0,-1,1,0,-1,1,-1};
//定义八个方向
char a[maxn][maxn]; //存储地图,以及标记该点能否再访问
int n, m;
void dfs(int x, int y)
{
a[x][y] = ‘.‘; //标记已访问过。
for(int i=0; i<8; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i];
if( tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]==‘W‘)
//如果(tx,ty)位置是合法的,则搜索下去
{
dfs(tx,ty);
}
}
}
{
a[x][y] = ‘.‘; //标记已访问过。
for(int i=0; i<8; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i];
if( tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]==‘W‘)
//如果(tx,ty)位置是合法的,则搜索下去
{
dfs(tx,ty);
}
}
}
int main()
{
int i, j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",a[i]);
int total = 0;
for(i=0; i<n; ++i)
for(j=0; j<m; ++j)
{
if(a[i][j]==‘W‘)
{
//只要找到了一个‘w‘则对其所在的块进行标记
total++;//结果加一
dfs(i,j);
}
}
printf("%d\n",total);
}
return 0;
}
{
int i, j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",a[i]);
int total = 0;
for(i=0; i<n; ++i)
for(j=0; j<m; ++j)
{
if(a[i][j]==‘W‘)
{
//只要找到了一个‘w‘则对其所在的块进行标记
total++;//结果加一
dfs(i,j);
}
}
printf("%d\n",total);
}
return 0;
}
//-------------------------------------------
//BFS, 用数组实现队列 //广搜
#include <stdio.h>
#include <string.h>
//BFS, 用数组实现队列 //广搜
#include <stdio.h>
#include <string.h>
const int maxn = 105;//地图大小
char Map[maxn][maxn];
int visit[maxn][maxn];
int n, m;
char Map[maxn][maxn];
int visit[maxn][maxn];
int n, m;
const int queue_size = 10000;//队列大小
struct node
{
int x, y;
};
node q[queue_size];
int front, rear; //队首指针和队尾指针
struct node
{
int x, y;
};
node q[queue_size];
int front, rear; //队首指针和队尾指针
int dx[8]= {1,1,1,-1,-1,-1,0,0};
int dy[8]= {1,0,-1,1,0,-1,1,-1};
int dy[8]= {1,0,-1,1,0,-1,1,-1};
void bfs(int x, int y)
{
int i, tx, ty;
node t, next;
front = rear = 0;
t.x = x;
t.y = y;
q[front] = t; //起点入队
visit[x][y] = 1; //将起点标记为已访问
while(front<=rear) //如果队列不为空
{
t = q[front]; front++; //拿出队首元素
for(i=0; i<8; ++i)
{
tx = t.x + dx[i];
ty = t.y + dy[i];
if( tx>=0&&tx<n&&ty>=0&&ty<m&&Map[tx][ty]==‘W‘&&visit[tx][ty]==0)
//如果(tx,ty)位置合法,则加入队列
{
next.x = tx, next.y = ty;
q[++rear] = next;
//并且将其标记为已访问
Map[next.x][next.y] = ‘.‘;
}
}
}
}
{
int i, tx, ty;
node t, next;
front = rear = 0;
t.x = x;
t.y = y;
q[front] = t; //起点入队
visit[x][y] = 1; //将起点标记为已访问
while(front<=rear) //如果队列不为空
{
t = q[front]; front++; //拿出队首元素
for(i=0; i<8; ++i)
{
tx = t.x + dx[i];
ty = t.y + dy[i];
if( tx>=0&&tx<n&&ty>=0&&ty<m&&Map[tx][ty]==‘W‘&&visit[tx][ty]==0)
//如果(tx,ty)位置合法,则加入队列
{
next.x = tx, next.y = ty;
q[++rear] = next;
//并且将其标记为已访问
Map[next.x][next.y] = ‘.‘;
}
}
}
}
int main()
{
int i, j, total;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",Map[i]);
total = 0;
memset( visit, 0, sizeof visit );
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
if(Map[i][j]==‘W‘ && visit[i][j] == 0)
{
total++;
bfs(i,j);
}
}
}
printf("%d\n",total);
}
return 0;
}
{
int i, j, total;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",Map[i]);
total = 0;
memset( visit, 0, sizeof visit );
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
if(Map[i][j]==‘W‘ && visit[i][j] == 0)
{
total++;
bfs(i,j);
}
}
}
printf("%d\n",total);
}
return 0;
}
//-------------------------------------------
//1009 Lake Counting 用BFS,队列使用C++_STL中的封装的queue,包含在头文件#include<queue>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
//1009 Lake Counting 用BFS,队列使用C++_STL中的封装的queue,包含在头文件#include<queue>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 105;//地图大小
char Map[maxn][maxn];
int n, m;
char Map[maxn][maxn];
int n, m;
int dx[8]= {1,1,1,-1,-1,-1,0,0};
int dy[8]= {1,0,-1,1,0,-1,1,-1};
int dy[8]= {1,0,-1,1,0,-1,1,-1};
struct node
{
int x, y;
};
void bfs(int x, int y)
{
queue<node> q;
int i, tx, ty;
node t, next;
t.x = x; t.y = y;
q.push(t);
Map[x][y] = ‘.‘;
while(!q.empty())
{
t = q.front(); q.pop();
for(i=0; i<8; ++i)
{
tx = t.x + dx[i];
ty = t.y + dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&Map[tx][ty]==‘W‘)
{
Map[tx][ty] =‘.‘;
next.x = tx, next.y = ty;
q.push(next);
}
}
}
}
{
int x, y;
};
void bfs(int x, int y)
{
queue<node> q;
int i, tx, ty;
node t, next;
t.x = x; t.y = y;
q.push(t);
Map[x][y] = ‘.‘;
while(!q.empty())
{
t = q.front(); q.pop();
for(i=0; i<8; ++i)
{
tx = t.x + dx[i];
ty = t.y + dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&Map[tx][ty]==‘W‘)
{
Map[tx][ty] =‘.‘;
next.x = tx, next.y = ty;
q.push(next);
}
}
}
}
int main()
{
int i, j, total;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",Map[i]);
total = 0;
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
if(Map[i][j]==‘W‘)
{
total++;
bfs(i,j);
}
}
}
printf("%d\n",total);
}
return 0;
}
{
int i, j, total;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; ++i) scanf("%s",Map[i]);
total = 0;
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
if(Map[i][j]==‘W‘)
{
total++;
bfs(i,j);
}
}
}
printf("%d\n",total);
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。