• (BFS) bzoj 1102


    1102: [POI2007]山峰和山谷Grz

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 436  Solved: 227
    [Submit][Status]

    Description

    FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量。
    给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的。
    若两个格子有公共顶点,那么他们就是相邻的格子。(所以与(i,j)相邻的格子有(i−1, j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1))。
    我们定义一个格子的集合S为山峰(山谷)当且仅当:
    1.S的所有格子都有相同的高度。
    2.S的所有格子都联通
    3.对于s属于S,与s相邻的s’不属于S。都有ws > ws’(山峰),或者ws < ws’(山谷)。
    你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。

    Input

    第一行包含一个正整数n,表示地图的大小(1<=n<=1000)。接下来一个n*n的矩阵,表示地图上每个格子的高度。(0<=w<=1000000000)

    Output

    应包含两个数,分别表示山峰和山谷的数量。

    Sample Input

    输入样例1
    5
    8 8 8 7 7
    7 7 8 8 7
    7 7 7 7 7
    7 8 8 7 8
    7 8 8 8 8
    输入样例2
    5
    5 7 8 3 1
    5 5 7 6 6
    6 6 6 2 8
    5 7 2 5 8
    7 1 0 1 7

    Sample Output

    输出样例1
    2 1
    输出样例2
    3 3
     
    直接BFS啊。。。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<vector>
    using namespace std;
    int n,mp[1005][1005];
    bool vis[1005][1005];
    int dx[8]={0,0,1,-1,-1,-1,1,1};
    int dy[8]={1,-1,0,0,1,-1,-1,1};
    int ans1,ans2;
    bool check(int xx,int yy)
    {
        if(xx<1||yy<1||xx>n||yy>n)
            return false;
        return true;
    }
    void bfs(int s,int t)
    {
        int cnt1=0,cnt2=0;
        vis[s][t]=1;
        queue<int> q;
        q.push(s),q.push(t);
        while(!q.empty())
        {
            int x,y,xx,yy;
            x=q.front(),q.pop();
            y=q.front(),q.pop();
            for(int i=0;i<8;i++)
            {
                xx=x+dx[i],yy=y+dy[i];
                if(!check(xx,yy))
                    continue;
                if(mp[xx][yy]==mp[x][y]&&!vis[xx][yy])
                {
                    q.push(xx);
                    q.push(yy);
                    vis[xx][yy]=1;
                }
                else
                {
                    cnt1+=mp[xx][yy]>mp[x][y];
                    cnt2+=mp[xx][yy]<mp[x][y];
                }
            }
        }
        ans1+=!cnt1&&cnt2;
        ans2+=cnt1&&!cnt2;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                scanf("%d",&mp[i][j]);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(!vis[i][j])
                {
                    bfs(i,j);
                }
            }
        }
        if(!ans1&&!ans2)
            ans1=ans2=1;
        printf("%d %d
    ",ans1,ans2);
        return 0;
    }
    

      

  • 相关阅读:
    四、java IO--使用字节流拷贝文件
    三、java IO--使用字节流写入文件
    二、java IO--使用字节流读取文件
    一、java--IO概念
    xml解析/读取--dom4j
    java程序执行顺序
    Flask学习——cookie操作
    Linux13 shell函数、数组及awk、awk中的数组
    Linux12 RPM 和yum的使用
    Linux11 IP网段划分及主机接入网络
  • 原文地址:https://www.cnblogs.com/water-full/p/4524370.html
Copyright © 2020-2023  润新知