洪水

小浣熊松松和朋友到野外露营,没想到遇上了π年一次的大洪水,好在松松是一只爱观察的小浣熊,他发现露营地的地形和洪水有如下性质:

①露营地可以被看做是一个N*M的矩形方阵,其中左上角坐标为(1,1),右下角坐标为(n,m),每个格子(i,j)都有一个高度h(i,j)。

②洪水送(r,c)开始,如果一个格子被洪水淹没,那这个格子四周比它低(或相同)的格子也会被淹没。

现在松松想请你帮忙算算,有多少个格子不会被淹没,便于他和朋友逃脱。

输入描述 Input Description

第一行包含两个整数n,m,表示矩形方阵右下角坐标。

以下n行,每行m个数,第i行第j个数表示格子(i,j)的高度。

最后一行包含两个整数r,c,表示最初被洪水淹没的格子。

输出描述 Output Description

输出仅一行,为永远不会被淹没的格子的数量。

样例输入 Sample Input

3 3

1 2 3

2 3 4

3 4 5

2 2

样例输出 Sample Output

5

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int map[1000][1000];
 5 int book[1000][1000];
 6 int startx,starty;
 7 int nextx[4]={0,1,0,-1};
 8 int nexty[4]={1,0,-1,0};
 9 int n,m;
10 void dfs(int x,int y){
11     int tx,ty;
12     for(int i=0;i<=3;i++){
13         tx=x+nextx[i];
14         ty=y+nexty[i];
15         if(tx<1||tx>n||ty>m||ty<1||book[tx][ty]!=0||map[tx][ty]>map[x][y]){
16             continue;
17         }
18         book[tx][ty]=1;
19         dfs(tx,ty);
20     }        
21 }
22 int main(){
23     cin>>n>>m;
24     for(int i=1;i<=n;i++){
25         for(int j=1;j<=m;j++){
26             cin>>map[i][j];
27         }
28     }
29     cin>>startx>>starty;
30     book[startx][starty]=1;
31     dfs(startx,starty);
32     int ans=0;
33     for(int i=1;i<=n;i++){
34         for(int j=1;j<=m;j++){
35             if(book[i][j]==0){
36                 ans++;
37             }
38         }
39     }
40     cout<<ans;
41 }
原文地址:https://www.cnblogs.com/1129-tangqiyuan/p/8479578.html