(寒假集训)Cow Art(bfs)
Cow Art
时间限制: 1 Sec 内存限制: 64 MB
提交: 13 解决: 10
[提交][状态][讨论版]
题目描述
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.
输入
* Lines 2..1+N: Each line contains a string with N characters,describing one row of a painting.
输出
样例输入
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)的更多相关文章
- CSU-ACM寒假集训选拔-入门题
CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...
- (寒假集训) Cow Jog(二分优化的最长上升子数列)
Cow Jog 时间限制: 1 Sec 内存限制: 64 MB提交: 24 解决: 5[提交][状态][讨论版] 题目描述 Farmer John's N cows (1 <= N < ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- 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 ...
- 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 ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Catch That Cow (bfs)
Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...
随机推荐
- python之路——网络编程
一.楔子 你现在已经学会了写python代码,假如你写了两个python文件a.py和b.py,分别去运行,你就会发现,这两个python的文件分别运行的很好.但是如果这两个程序之间想要传递一个数据, ...
- diskimage-builder-command
yum -y install python-virtualenv.noarch virtualenv ~/dib-virtualenv . ~/dib-virtualenv/bin/activate ...
- Qt(1)
Qt Qt开发图形界面软件,可以跨win.linux.mac平台.移动端,使用c++开发 Qt采用所见即所得的UI设计(UI设计和代码是联动的),GUI界面编辑信号和槽,由开发环境自动生成c++代码, ...
- 201621123034 《Java程序设计》第7周学习总结
作业07-Java GUI编程 1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模 ...
- weex 开发 (已放弃了)
关于weex 开发 本菜已放弃使用了,当初选择使用weex 是为了同时支持h5 和 android / ios 三端:想法很不错,深入之后 突然发现,开发起来并没有很轻松,因为weex 中有些方法, ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- 通过RHN网站给RHEL打补丁
[root@yum01 ~]# yum list-sec securityLoaded plugins: downloadonly, product-id, rhnplugin, security, ...
- canvas2D 基础知识 浅析
1.canvas HTML5给出的一个可以展示绘图内容的标签 使用领域: (推荐网站:threejs.org) 1. 游戏 2. 可视化数据 3. Banner广告 4. 多媒体 (1)在 c ...
- js Event对象
(事件阶段)Event Phases Event对象在event第一次触发的时候被创建出来,并且一直伴随着事件在DOM结构中流转的整个生命周期.event对象会被作为第一个参数传递给事件监听的回调函数 ...
- Topcoder SRM 604 div1题解
CTSC考完跑了过来日常TC--- Easy(250pts): 题目大意:有个机器人,一开始的位置在(0,0),第k个回合可以向四个方向移动3^k的距离(不能不动),问是否可以到达(x,y),数据满足 ...