1. 程式人生 > >洛谷 P3671 [USACO17OPEN]Where's Bessie? 貝西在哪呢

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

small int include best tar targe const ace ios

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 \times 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;
}

 

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