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

(寒假集训)Cow Art(bfs)的更多相关文章

  1. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  2. (寒假集训) Cow Jog(二分优化的最长上升子数列)

    Cow Jog 时间限制: 1 Sec  内存限制: 64 MB提交: 24  解决: 5[提交][状态][讨论版] 题目描述 Farmer John's N cows (1 <= N < ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  5. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  6. hdoj 2717 Catch That Cow【bfs】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. Catch That Cow(BFS)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  9. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

随机推荐

  1. 【转载】Linux下安装LoadRunner LoadGenerator

    原文地址:[转载]Linux下安装LoadRunner LoadGenerator作者:邱建忠tester LR的负载机安装在linux的理由: 1.windows xp,双核+4G内存,基本上每个v ...

  2. 测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低)

    测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低) 答案: .A .C .C .A .A .D

  3. 解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误

    安装MySQL8.0之后,使用Navicat连接数据库,报1251错误. 上网搜索解决方案,网上说出现这种情况的原因是:mysql8 之前的版本中加密规则是mysql_native_password, ...

  4. CandyCrush 糖果传奇

    1.unity自带触发事件 unity的每一个Collider对象都有类似OnMouseDown.OnMouseOver等事件.此事件是存在于MonoBehaviour脚本里的,而MonoBehavi ...

  5. URAL 1934 spfa算法

    D - Black Spot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  6. oracle定时job粗解

    其中一篇随笔我写了oracle的存储过程大概的介绍,存储过程除了自身有in的param,来进行程序调用处理之外,还可以通过定时任务的方式调用来执行. 应用场景: 数据同步:有两个显示菜单,“信息编辑” ...

  7. 软考——(1)J2SE

    我们先从Java说起,简单的说,Java是一种面向对象的程序设计语言,可跨平台使用. 与之前学习的程序设计语言相比,最值得一提的就是Java的两种核心机制:Java虚拟机和垃圾回收机制. 1)虚拟机 ...

  8. Linq 聚合函数

    var WReserve = (from m in _db.W_RESERVE select m).ToList().LastOrDefault(); 必须ToList() 之后才能 last fir ...

  9. Nginx负载均衡的实现(初级)

    不用nginx.conf,新建一个 fzjh.conf (名称自定义) 内容如下: user nobody; # 声明用户为nobody worker_processes 4; # 开启的nginx ...

  10. [bzoj3065] 带插入区间第k小值 [重量平衡树套线段树]

    题面 传送门 思路 发现强制在线了...... 本来可以树套树解决的问题,现在外层不能使用线段树了,拿什么替代呢? 我们需要一种支持单点插入.下套数据结构.数据结构上传合并复杂度最多单log,不能旋转 ...