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. 能ping通网络,也正常连接,就是打不开网页,无法访问网络

    netsh winsock reset命令,作用是重置 Winsock 目录.如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题,就需要用netsh winsock reset命令 ...

  2. time模块与random模块,六位含字母随机验证码

    # time模块# import time# time.time()#计算这一时刻时间戳 *******# time.sleep(1)#让cpu休眠一定时间 *******# time.clock() ...

  3. Windows 下开发.NET Core应用

    一.使用Visual Studio 2015开发1.1 依次安装Visual Studio 2015 Update 3.NET Core 1.0.0 - VS 2015 Tooling Preview ...

  4. 在Struts等框架中获取Spring容器的方式

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletContext src); ...

  5. Eclipse 日文乱码怎么解决Shift_JIS

    https://jingyan.baidu.com/article/870c6fc325a691b03fe4beac.html Eclipse设置编码的地方主要有三处,这三处的设置都会影响中文的显示. ...

  6. libcurl网络连接使用tcp/ip

    CURL *curl; CURLcode res; const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; / ...

  7. Codeforces Round #359 (Div. 2) B

    B. Little Robber Girl's Zoo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)

    Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)     最近在做控制算法实现的时候,对于其中参杂的各种差分.卷积很头疼,就在网上搜集了些资料,汇总于此,以做备 ...

  9. Java Web 路径问题

    可能在做文件上传或者 图片加载,资源加载 时候用到文件相对服务器地址 System.out.println(request.getRemoteUser()); //客户端用户System.out.pr ...

  10. Java Nio注意事项

    Selector  : public abstract class Selector extends Object SelectableChannel 对象的多路复用器. 可通过调用此类的 open ...