Cow Art

时间限制: 1 Sec  内存限制: 64 MB
提交: 13  解决: 10
[提交][状态][讨论版]

题目描述

A
little known fact about cows is the fact that they are red-green
colorblind, meaning that red and green look identical to them.  This
makes it especially difficult to design artwork that is appealing to
cows as well as humans.

Consider a square painting that is
described by an N x N grid of characters (1 <= N <= 100), each one
either R (red), G (green), or B (blue).  A painting is interesting if
it has many colored "regions" that can be distinguished from
each-other.  Two characters belong to the same region if they are
directly adjacent (east, west, north, or south), and if they are
indistinguishable in color.  For example, the painting
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR
has 4 regions (2 red, 1 blue, and 1 green) if viewed by a human, but only 3 regions (2 red-green, 1 blue) if viewed by a cow.

Given a painting as input, please help compute the number of regions in the painting when viewed by a human and by a cow.

输入

* Line 1: The integer N.
* Lines 2..1+N: Each line contains a string with N characters,describing one row of a painting.

输出

* Line 1: Two space-separated integers, telling the number of regions in the painting when viewed by a human and by a cow.

样例输入

5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

样例输出

4 3
【分析】水题,两次BFS。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define MAXN 111111
#define MAXM 222222
#define INF 1000000000
using namespace std;
const int N=1e2+;
int cnt,rt,n;
int vis[N][N];
int d[][]={,,,,-,,,-};
char str[N][N];
struct man{
int x,y;
};
void bfs(int x,int y){
man s;s.x=x;s.y=y;
queue<man>q;
q.push(s);
vis[x][y]=;
while(!q.empty()){
man t=q.front();
q.pop();
for(int i=;i<;i++){
int xx=t.x+d[i][];
int yy=t.y+d[i][];
if(xx>=&&xx<n&&yy>=&&yy<=n&&!vis[xx][yy]&&str[xx][yy]==str[t.x][t.y]){
man k;k.x=xx;k.y=yy;
q.push(k);
vis[xx][yy]=;
}
}
}
}
int main(){
scanf("%d",&n);
int ans1=,ans2=;
for(int i=;i<n;i++)scanf("%s",str[i]);
for(int i=;i<n;i++){
for(int j=;j<n;j++)
if(!vis[i][j]){
bfs(i,j);
ans1++;
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(str[i][j]=='R')str[i][j]='G';
}
}
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
for(int j=;j<n;j++)
if(!vis[i][j]){
bfs(i,j);
ans2++;
}
}
printf("%d %d\n",ans1,ans2);
return ;
}
												

最新文章

  1. Mac OSX+VirtualBox+Vagrant+CentOS初体验
  2. Redis集群(三):主从配置一
  3. owncloud7.0.2.1升级8.0.3
  4. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法
  5. 安装node.js+express for win7的Web开发环境配置
  6. linux rpm 安装包 信息查询
  7. ELF Spec
  8. 学习smali
  9. ios category
  10. 编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish 和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
  11. windows8 8.1 安装完 ubuntu无法挂载 ntfs分区 解决方法
  12. codeforces --- 279C Ladder
  13. [深入React] 1. 开发环境搭建
  14. 大数据笔记01:大数据之Hadoop简介
  15. (转) Functions
  16. C# 与MySQL
  17. 怎样的 Hash 算法能对抗硬件破解
  18. Jquery操作Table
  19. css详细笔记
  20. 当页面是动态时 如果后台存储id可以通过查询后台方式获取对象;当后台没有存储时候 只有通过前端标记了 例如标记数量为10 我们根据传递过来的10循环取值

热门文章

  1. phpStorm9.0 +xampp+chrome php调试环境配置!
  2. 关于windows10设置环境变量的问题
  3. ADB连接手机遇到的问题:list of devices attached
  4. Robot Framwork +Selenium2环境搭建
  5. node+express+nginx搭建站点
  6. jQuery静态分页功能
  7. 使用hadoop统计多个文本中每个单词数目
  8. [CF482B]Interesting Array
  9. BZOJ3456 城市规划 【分治NTT】
  10. webpack watch模式产生*.hot-update.json文件