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. ASP.NET Core 2.1 源码学习之 Options[2]:IOptions 【转】

    原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-in-asp-net-core.html 在 上 ...

  2. winform 使用Anchor属性进行界面布局

    每个控件的定位方法: 一.使用Anchor: Anchor分为Left.Top.Right.Bottom四个属性. 它们的含义如下: Top——表示控件中与父窗体(或父控件)相关的顶部应该保持固定. ...

  3. PHP的几种遍历方法

    PHP常用的遍历方法有三种,foreach,for,list()/each()和while,这三种方法中效率最高的是使用foreach语句遍历数组 一.使用for语句循环遍历数组 值得大家注意的是使用 ...

  4. ubuntu服务器环境配置参考

    一.基本的Linux系统命令: ls 查看当前目录下的文件及文件夹 cd /var/www/html 转换目录到/var/www/html cd abc/ddd/ 转换目录到当前目录下的abc文件夹下 ...

  5. 【POJ 2406 Power Strings】

    Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...

  6. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  7. SpringMVC+MyBatis+Shiro 配置文件详解

    1.web.xml文件的配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. poj1039 Pipe(计算几何叉积求交点)

    F - Pipe Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  9. 渗透协作工具 dradis centos安装

    https://dradisframework.com/ce/documentation/install_centos.html yum install rubygems 安装的bundle在drad ...

  10. [Oracle] DataGuard switchover

    Oracle DataGuard switchover 2013/07/11 Tag.Data Guard,primary,standby,switchover 切换前primary site和sta ...