Lake Counting
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 18826 |
|
Accepted: 9463 |
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
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中的基础题,跟 nyist 27 有点类似,但是这道题wa了我好久啊。。。。最后终于ac了
#include<stdio.h>
char map[101][101];
int n,m;
int move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};//这里定义八个可行方向
void dfs(int x,int y)
{
map[x][y]='.'; //标记走过的路径
for(int i=0;i<8;i++)
{
int dx=x+move[i][0];
int dy=y+move[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]=='W')
dfs(dx,dy);
}
/*for(int dx=-1;dx<=1;dx++) //这种方法是看到书上写的
for(int dy=-1;dy<=1;dy++)
{
int nx=x+dx,ny=y+dy;
if(nx>=0&&nx<n&&ny>=0&&ny<m&&map[nx][ny]=='W')
dfs(nx,ny);
}*/
}
int main()
{
int sum=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",map[i]);//就是这个地方我wa了好多次啊!!!
/*for(int i=0;i<n;i++) 就是这样的输入有问题,中间可能输入 换行符的时候,也存入了数组中去了,所以输入的数组可能就和原数组不一样了
for(int j=0;j<m;j++)
scanf("%c",&map[i][j]);*/
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[i][j]=='W') //直接从有W的地方开始进行dfs
{
dfs(i,j);
sum++;
}
}
printf("%d\n",sum);
return 0;
}
这个题目让我也意识到了以前没有注意到的小细节,以后做题的时候就不能再犯了 ,对于连续字符串的输入的时候要特别注意,是不是把换行符加入进去了,改变了原字符串的结构,对于连续子都串的输入要特别注意!!!
下面这道题和上面这道题基本类似,就果断水过啦~ poj1562
Oil Deposits
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 11971 |
|
Accepted: 6474 |
Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
Source
Mid-Central USA 1997
下面是我的代码
#include <stdio.h>
#include <string.h>
char map[102][102];
int move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
int n,m;
void dfs(int x,int y)
{
map[x][y]='*';
for(int i=0;i<8;i++)
{
int dx=x+move[i][0];
int dy=y+move[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]=='@')
dfs(dx,dy);
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&m!=0&&n!=0)
{
memset(map,0,sizeof(map));
int sum=0;
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[i][j]=='@')
{
dfs(i,j);
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
dfs的基础题水过。