泉水 | ||||||
| ||||||
Description | ||||||
Leyni是一个地址调查员,有一天在他调查的地方突然出现个泉眼。由于当地的地势不均匀,有高有低,他觉得如果这个泉眼不断的向外溶出水来,这意味着这里在不久的将来将会一个小湖。水往低处流,凡是比泉眼地势低或者等于的地方都会被水淹没,地势高的地方水不会越过。而且又因为泉水比较弱,当所有地势低的地方被淹没后,水位将不会上涨,一直定在跟泉眼一样的水位上。 | ||||||
Input | ||||||
有若干组数据,每组数据的第一行有四个整数n,m,p1,p2(0<n,m,p1,p2<=1000),n和m表示当前地图的长和宽,p1和p2表示当前地图的泉眼位置,即第p1行第p2列,随后的n行中,每行有m个数据。表示这每一个对应坐标的高度。 | ||||||
Output | ||||||
输出对应地图中会有多少个格子被水充满。 | ||||||
Sample Input | ||||||
3 5 2 3 3 4 1 5 1 2 3 3 4 7 4 1 4 1 1 | ||||||
Sample Output | ||||||
6 | ||||||
Author | ||||||
孔繁阳 |
题意:给出一个二维,每个元素都有不同的权,规定从一个给定点开始走,每次只能有四个方向走到小于他的权的地方,问这些地方连起来有多少块。
解题思路:DFS水题。每次走的时保证断其权小于给定权。最后全搜索求方块个数就行了。附上代码
#include <stdio.h>
#include <memory.h>
#define maxn 1010
int m,n,t;
int next[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int flag[maxn][maxn],power[maxn][maxn];
void DFS(int r,int s)
{
if(r<1||r>m||s<1||s>n) return;
if(flag[r][s]==1||power[r][s]>t) return;
flag[r][s]=1;
for(int i=0; i<4; i++)
DFS(r+next[i][0],s+next[i][1]);
}
int main()
{
//freopen("in.txt","r",stdin);
int p,q;
while(~scanf("%d%d%d%d",&m,&n,&p,&q))
{
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
scanf("%d",&power[i][j]);
t=power[p][q];
memset(flag,0,sizeof(flag));
DFS(p,q);
int sum=0;
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
if(flag[i][j]==1) sum++;
printf("%d\n",sum);
}
return 0;
}