• 洛谷 P3671 [USACO17OPEN]Where's Bessie? 贝西在哪呢


    题目背景

    农夫John正在测试一个他新发明的全自动寻找奶牛无人机,它能够照一张农场的图片然后自动找出奶牛的位置。

    不幸的是,这个相机并不包含一个优秀的寻找奶牛函数,所以农夫John需要你来写一个。

    农场的俯瞰图被定义为一个n * n的字符矩阵。矩阵由大写字母A到Z组成,每个字母表示一种可行

    的颜色。农夫John发现一个可能是奶牛的位置(以下简称PCL)的最好定义如下:

    一个PCL是一个矩阵(可能是整张图),矩阵的边与图像的边缘平行,且不能被其他PCL所包含(因此PCL内部不可能有PCL)

    更多的,一个PCL必须满足以下特性:

    1、矩阵有且只能有2种颜色构成。

    2、这两种颜色一种构成一个连通块,另一种形成两个或两个以上的连通块。

    举个例子:

    AAAAA ABABA AAABB 这个矩阵就是一个PCL,其中颜色A构成一个连通块,B构成两个连通块,描述了一只可能以A为底色,B为花纹的奶牛。

    在这里连通块被定义为:从其中的任何一个点,你能仅通过上下左右移动,到达另外任何一个点

    (即上下左右相邻)

    给定农场的照片,请你计算图中有几个PCL。

    输入格式:

    第一行包含一个正整数N,表示矩阵的边长。

    接下来的N行每行N个字符,描述了这个矩阵的颜色。

    输出格式:

    输出PCL的个数

    说明:

    在这个样例里,两个PCL分别是:

    (如下)

    题目描述

    Always known for being quite tech-savy, Farmer John is testing out his new automated drone-mounted cow locator camera, which supposedly can take a picture of his field and automatically figure out the location of cows. Unfortunately, the camera does not include a very good algorithm for finding cows, so FJ needs your help developing a better one.

    The overhead image of his farm taken by the camera is described by an N imes NN×N grid of characters, each in the range A ldots ZAZ, representing one of 26 possible colors. Farmer John figures the best way to define a potential cow location (PCL) is as follows: A PCL is a rectangular sub-grid (possibly the entire image) with sides parallel to the image sides, not contained within any other PCL (so no smaller subset of a PCL is also a PCL). Furthermore, a PCL must satisfy the following property: focusing on just the contents of the rectangle and ignoring the rest of the image, exactly two colors must be present, one forming a contiguous region and one forming two or more contiguous regions.

    AAAAA
    ABABA
    AAABB

    For example, a rectangle with contents

    would constitute a PCL, since the A's form a single contiguous region and the B's form more than one contiguous region. The interpretation is a cow of color A with spots of color B.

    A region is "contiguous" if you can traverse the entire region by moving repeatedly from one cell in the region to another cell in the region taking steps up, down, left, or right.

    Given the image returned by FJ's camera, please count the number of PCLs.

    输入输出格式

    输入格式:

     

    The first line of input contains NN, the size of the grid (1 leq N leq 201N20).

    The next NN lines describe the image, each consisting of NN characters.

     

    输出格式:

     

    Print a count of the number of PCLs in the image.

     

    输入输出样例

    输入样例#1: 复制
    4
    ABBC
    BBBC
    AABB
    ABBC
    输出样例#1: 复制
    2

    说明

    In this example, the two PCLs are the rectangles with contents

    
    ABB
    BBB
    AAB
    ABB
    
    and
    
    BC
    BC
    BB
    BC
    ``
    思路:搜索。
    因为数据范围很小,所以可以枚举每一个矩形,然后进行判断。
    最后时,判断重合,统计答案即可。
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int a[25][25];
    int n,ans,num;
    int flag[25][25];
    int color[26],c[10000];
    struct answer{
        int i1,i2,j1,j2;
    }pcl_[50000];
    void find(int x,int i,int j,int i1,int i2,int j1,int j2){
        flag[i][j]=1;
        if(i<i2&&flag[i+1][j]==0&&a[i+1][j]==x) find(x,i+1,j,i1,i2,j1,j2);
        if(j<j2&&flag[i][j+1]==0&&a[i][j+1]==x)    find(x,i,j+1,i1,i2,j1,j2);
        if(i>i1&&flag[i-1][j]==0&&a[i-1][j]==x)    find(x,i-1,j,i1,i2,j1,j2);
        if(j>j1&&flag[i][j-1]==0&&a[i][j-1]==x)    find(x,i,j-1,i1,i2,j1,j2);
        return;
    }
    int pcl(int i1,int i2,int j1,int j2){
        memset(c,0,sizeof(c));
        memset(flag,0,sizeof(flag));
        memset(color,0,sizeof(color));
        int numm=0;
        for(int i=i1;i<=i2;i++)
            for(int j=j1;j<=j2;j++)
                if(flag[i][j]==0){
                    if(color[a[i][j]]==0) numm++,c[numm]=a[i][j];
                    if(numm>2)    return 0;
                    color[a[i][j]]++;
                    find(a[i][j],i,j,i1,i2,j1,j2);
                }   
        if((color[c[1]]==1&&color[c[2]]>1)||(color[c[2]]==1&&color[c[1]]>1))    return 1;
        else    return 0;
    }
    int check(int x){
        for(int i=1;i<=num;i++)
            if(i!=x&&pcl_[x].i1>=pcl_[i].i1&&pcl_[x].i2<=pcl_[i].i2&&pcl_[x].j1>=pcl_[i].j1&&pcl_[x].j2<=pcl_[i].j2) 
                return 0;
        return 1;
    }
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                char x;cin>>x;
                a[i][j]=x-'A';
            }
        for(int i1=1;i1<=n;i1++)
            for(int i2=i1;i2<=n;i2++)
                for(int j1=1;j1<=n;j1++)
                    for(int j2=j1;j2<=n;j2++)
                        if(pcl(i1,i2,j1,j2)==1) {
                            num++;
                            pcl_[num].i1=i1;
                            pcl_[num].i2=i2;
                            pcl_[num].j1=j1;
                            pcl_[num].j2=j2;
                        }
        for(int i=1;i<=num;i++)
            if(check(i)==1)    ans++;
        cout<<ans;
    }
     
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    POJ 1981 最大点覆盖问题(极角排序)
    POJ 1286 Pólya定理
    POJ 1830 高斯消元
    HDU 3364 高斯消元
    Educational Codeforces Round 42D. Merge Equals(STL)
    ZOJ 3955:Saddle Point(思维)
    POJ 3301:Texas Trip(计算几何+三分)
    SCUT 125 :笔芯回文(DP)
    ZOJ 3953:Intervals(优先队列+思维)
    Codeforces Gym101097I:Sticks (思维)
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8213383.html
Copyright © 2020-2023  润新知