题解:

简单灌水

从最高的开始

代码:

#include<bits/stdc++.h> 
const int inf=0x7fffffff,N=705;
typedef long long ll;
using namespace std;
int n,m,cnt,ans,xx[8]={0,0,1,1,1,-1,-1,-1},yy[8]={1,-1,1,0,-1,1,0,-1},mp[N][N],mark[N][N];
struct data{int x,y,t;}a[500005];
bool operator<(data a,data b){return a.t>b.t;}
void dfs(int x,int y)
{
    mark[x][y]=1;
    for (int i=0;i<8;i++)
     { 
        int nowx=x+xx[i],nowy=y+yy[i];
        if (nowx<1||nowy<1||nowx>n||nowy>m)continue;
        if (mp[nowx][nowy]<=mp[x][y]&&!mark[nowx][nowy])dfs(nowx,nowy);
     } 
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
     for (int j=1;j<=m;j++)
      {
        cnt++;
        scanf("%d",&mp[i][j]);
        a[cnt].x=i;a[cnt].y=j;a[cnt].t=mp[i][j];
      }
    sort(a+1,a+cnt+1);
    for (int i=1;i<=cnt;i++)
     if (!mark[a[i].x][a[i].y]){dfs(a[i].x,a[i].y);ans++;}
    printf("%d",ans);
    return 0;
}

 

posted on 2018-02-24 09:58  宣毅鸣  阅读(74)  评论(0编辑  收藏  举报