(寒假集训)Cow Art(bfs)

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+5;
int cnt,rt,n;
int vis[N][N];
int d[4][2]={1,0,0,1,-1,0,0,-1};
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]=1;
    while(!q.empty()){
        man t=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx=t.x+d[i][0];
            int yy=t.y+d[i][1];
            if(xx>=0&&xx<n&&yy>=0&&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]=1;
            }
        }
    }
}
int main(){
    scanf("%d",&n);
    int ans1=0,ans2=0;
    for(int i=0;i<n;i++)scanf("%s",str[i]);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
           if(!vis[i][j]){
              bfs(i,j);
              ans1++;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
           if(str[i][j]=='R')str[i][j]='G';
        }
    }
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
           if(!vis[i][j]){
              bfs(i,j);
              ans2++;
        }
    }
    printf("%d %d\n",ans1,ans2);
    return 0;
}

 


转载于:https://www.cnblogs.com/jianrenfang/p/6279675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值