BZOJ 1102: [POI2007]山峰和山谷Grz【BFS】

1102: [POI2007]山峰和山谷Grz

【题目描述】
传送门

【解题报告】

这题很水,按照高度排序,BFS一下就可以了。

代码如下

#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Par pair<int,int>
using namespace std;
const int f[8][2]={{1,-1},{1,0},{1,1},{0,-1},{0,1},{-1,1},{-1,0},{-1,-1}};
int n,mp[1005][1005],Ans1,Ans2;
bool vis[1005][1005];
queue<Par> que;
struct xcw{
    int x,y,w;
    bool operator<(const xcw b)const{return w<b.w;}
}a[1005*1005];
int read(){
    int ret=0;char ch=getchar();bool f=1;
    for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');
    for(; isdigit(ch);ch=getchar()) ret=(ret<<3)+(ret<<1)+ch-48;
    return f?ret:-ret;
}
bool check(int x,int y){
    if(x<1||x>n||y<1||y>n) return 0;
    return !vis[x][y];
}
void BFS_UP(int x,int y){
    que.push(make_pair(x,y));vis[x][y]=1;
    while(!que.empty()){
        x=que.front().first,y=que.front().second;que.pop();
        for(int i=0;i<8;i++)
        if(check(x+f[i][0],y+f[i][1])&&mp[x][y]<=mp[x+f[i][0]][y+f[i][1]]){
            vis[x+f[i][0]][y+f[i][1]]=1;
            que.push(make_pair(x+f[i][0],y+f[i][1]));
        }
    }
}
void BFS_DOWN(int x,int y){
    que.push(make_pair(x,y));vis[x][y]=1;
    while(!que.empty()){
        x=que.front().first,y=que.front().second;que.pop();
        for(int i=0;i<8;i++)
        if(check(x+f[i][0],y+f[i][1])&&mp[x][y]>=mp[x+f[i][0]][y+f[i][1]]){
            vis[x+f[i][0]][y+f[i][1]]=1;
            que.push(make_pair(x+f[i][0],y+f[i][1]));
        }
    }
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("prob.in","r",stdin);
    freopen("prob.out","w",stdout);
    #endif
    n=read();
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++) mp[i][j]=read(),a[(i-1)*n+j]=(xcw){i,j,mp[i][j]};
    sort(a+1,a+1+n*n);
    for(int i=1;i<=n*n;i++)
    if(!vis[a[i].x][a[i].y]) BFS_UP(a[i].x,a[i].y),Ans1++;
    memset(vis,0,sizeof(vis));
    for(int i=n*n;i;i--)
    if(!vis[a[i].x][a[i].y]) BFS_DOWN(a[i].x,a[i].y),Ans2++;
    printf("%d %d\n",Ans2,Ans1);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值