题目背景

农夫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 ZA…Z, 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 201≤N≤20).

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[][];
int n,ans,num;
int flag[][];
int color[],c[];
struct answer{
int i1,i2,j1,j2;
}pcl_[];
void find(int x,int i,int j,int i1,int i2,int j1,int j2){
flag[i][j]=;
if(i<i2&&flag[i+][j]==&&a[i+][j]==x) find(x,i+,j,i1,i2,j1,j2);
if(j<j2&&flag[i][j+]==&&a[i][j+]==x) find(x,i,j+,i1,i2,j1,j2);
if(i>i1&&flag[i-][j]==&&a[i-][j]==x) find(x,i-,j,i1,i2,j1,j2);
if(j>j1&&flag[i][j-]==&&a[i][j-]==x) find(x,i,j-,i1,i2,j1,j2);
return;
}
int pcl(int i1,int i2,int j1,int j2){
memset(c,,sizeof(c));
memset(flag,,sizeof(flag));
memset(color,,sizeof(color));
int numm=;
for(int i=i1;i<=i2;i++)
for(int j=j1;j<=j2;j++)
if(flag[i][j]==){
if(color[a[i][j]]==) numm++,c[numm]=a[i][j];
if(numm>) return ;
color[a[i][j]]++;
find(a[i][j],i,j,i1,i2,j1,j2);
}
if((color[c[]]==&&color[c[]]>)||(color[c[]]==&&color[c[]]>)) return ;
else return ;
}
int check(int x){
for(int i=;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 ;
return ;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
char x;cin>>x;
a[i][j]=x-'A';
}
for(int i1=;i1<=n;i1++)
for(int i2=i1;i2<=n;i2++)
for(int j1=;j1<=n;j1++)
for(int j2=j1;j2<=n;j2++)
if(pcl(i1,i2,j1,j2)==) {
num++;
pcl_[num].i1=i1;
pcl_[num].i2=i2;
pcl_[num].j1=j1;
pcl_[num].j2=j2;
}
for(int i=;i<=num;i++)
if(check(i)==) ans++;
cout<<ans;
}
 

洛谷 P3671 [USACO17OPEN]Where's Bessie? 贝西在哪呢的更多相关文章

  1. 洛谷P3668 [USACO17OPEN]Modern Art 2 现代艺术2

    P3668 [USACO17OPEN]Modern Art 2 现代艺术2 题目背景 小TY的同学HF也想创作艺术 HF只有一块长条状的画布(画条),所以每一次涂色只能涂上连续几个单位的颜料,同样新的 ...

  2. 洛谷 P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银)

    P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银) 题目描述 Farmer John owns NN cows with spots and NN cows wi ...

  3. 洛谷 P3669 [USACO17OPEN]Paired Up 牛牛配对

    P3669 [USACO17OPEN]Paired Up 牛牛配对 题目描述 Farmer John finds that his cows are each easier to milk when ...

  4. 洛谷 [USACO17OPEN]Bovine Genomics G奶牛基因组(金) ———— 1道骗人的二分+trie树(其实是差分算法)

    题目 :Bovine Genomics G奶牛基因组 传送门: 洛谷P3667 题目描述 Farmer John owns NN cows with spots and NN cows without ...

  5. 洛谷 1938 [USACO09NOV]找工就业Job Hunt

    洛谷 1938  [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...

  6. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  7. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  8. 【洛谷P1352】没有上司的舞会

    [洛谷P1352]没有上司的舞会 x舷售 锚」翅θ 但是 拙臃 蓄ⅶ榔 暄条熨卫 翘ヴ馇 表现无愧于雪月工作室的核心管理 爸惚扎掬 颇瓶 芟缆肝 貌痉了 洵┭笫装 嗝◇裴腋 褓劂埭 ...

  9. 洛谷P2845-Switching on the Lights 开关灯

    Problem 洛谷P2845-Switching on the Lights 开关灯 Accept: 154    Submit: 499Time Limit: 1000 mSec    Memor ...

随机推荐

  1. HDU 2078 选课时间( 水题 )

    链接:传送门 思路:水题略 /************************************************************************* > File N ...

  2. hadoop中HDFS文件系统 nameNode出现的问题 nameNode无法打开

    1,修改core-site.xml文件,先改成localhost,将所有进程关闭stop-all.sh(或者是先关闭所有进程,然后再修改文件),然后重启,在修改core-site.xml文件成ip地址 ...

  3. 页面关闭或刷新时触发javascript的事件

    当页面在关闭或刷新时提示 window.onbeforeunload(function(){ //判断是关闭还是刷新 1.满足关闭,否则是刷新 if(event.clientX>document ...

  4. windows服务器剪贴板不能共用的解决办法

    远程桌面无法使用剪贴板共享纯文本的解决方法========================================以下操作须在远程桌面上操作,本地机没用的!================== ...

  5. mysql5.7官网直译SQL语句优化--select语句优化

    8.2 sql语句优化 大致内容如下: 8.2.1:SELECT语句的优化 8.2.2:优化子查询,派生表和试图引用 8.2.3:优化INFORMATION_SCHEMA查询 8.2.4:优化数据改变 ...

  6. (转)redis源代码分析 – event library

    每个cs程序尤其是高并发的网络服务端程序都有自己的网络异步事件处理库,redis不例外. 事件库仅仅包括ae.c.ae.h,还有3个不同的多路复用(本文仅描述epoll)的wrapper文件,事件库封 ...

  7. jdk动态代理(转)

    一旦这样绑定后,那么在进入代理对象方法调用的时候就会到HelloServiceProxy的invoke方法上,invoke方法有三个参数:第一个proxy是代理对象,第二个是当前调用那个方法,第三个是 ...

  8. 自己定义控件-MultipleTextView(自己主动换行、自己主动补齐宽度的排列多个TextView)

    一.功能: 1.传入一个 List<String> 数组,控件会自己主动加入TextView,一行显示不下会自己主动换行.而且把上一行末尾的空白通过拉伸而铺满. 2.配置灵活 <co ...

  9. POJ 1715

    同样是确定某位上的数,当确定某一位后,其后面的排列数是确定的,所以可以用除法和取余数的方法来确定这一位的值 #include <iostream> #include <cstdio& ...

  10. nyoj 585 取石子(六) 【Nim】

    取石子(六) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 近期TopCoder的PIAOYI和HRDV非常无聊,于是就想了一个游戏,游戏是这种:有n堆石子,两个人 ...