Lake Counting
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 41727 | Accepted: 20704 |
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.
Sample Output
3
此题是一道经典的搜索题,深搜和广搜都可解。由于初学广搜,做此题遇到诸多问题,不过都已解决。
广搜是一层一层的搜索。此题的关键在于如何去标记搜索过了的点(本人在此犯了一个错误)。
错误的代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, m;
char mp[105][105]; //用来装地图
int dir[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; //八个方向
int num;
struct point{ // 坐标
int x;
int y;
}now, c;
void bfs(int i, int j)
{
queue<point> que;
now.x = i;
now.y = j;
que.push(now);
while (!que.empty()) {
now = que.front();
que.pop();
mp[now.x][now.y] = '.'; //这里清除访问到的点
for (int i = 0; i < 8; ++i) {
c.x = now.x + dir[i][0];
c.y = now.y + dir[i][1];
if ((c.x >= 0 && c.x < n) &&(c.y >= 0 && c.y < m)&&mp[c.x][c.y] == 'W') {
que.push(c);
}
}
}
}
int main()
{
while(~scanf("%d %d", &n, &m)) {
for (int i = 0; i < n; ++i) { //初始化地图
scanf("%s", mp[i]);
}
num = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mp[i][j] == 'W') {
bfs(i, j);
++num;
}
}
}
printf("%d\n", num);
}
return 0;
}
这个错误很明显,广搜是一层一层的搜,这个只是标记一层中的一个点,在访问下一层中,会回访上一层的点。
正确代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, m;
char mp[105][105]; //用来装地图
int dir[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}; //八个方向
int num;
struct point{ // 坐标
int x;
int y;
}now, c;
void bfs(int i, int j)
{
queue<point> que;
now.x = i;
now.y = j;
mp[now.x][now.y] = '.'; //清除访问过的点
que.push(now);
while (!que.empty()) {
now = que.front();
que.pop();
for (int i = 0; i < 8; ++i) {
c.x = now.x + dir[i][0];
c.y = now.y + dir[i][1];
if ((c.x >= 0 && c.x < n) &&(c.y >= 0 && c.y < m)&&mp[c.x][c.y] == 'W') {
que.push(c);
mp[c.x][c.y] = '.'; //清除访问过的点
}
}
}
}
int main()
{
while(~scanf("%d %d", &n, &m)) {
for (int i = 0; i < n; ++i) { //初始化地图
scanf("%s", mp[i]);
}
num = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mp[i][j] == 'W') {
bfs(i, j);
++num;
}
}
}
printf("%d\n", num);
}
return 0;
}
如代码所示,应该把每一层点都消除掉。