• (寒假集训)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
    ",ans1,ans2);
        return 0;
    }

  • 相关阅读:
    mac下配置openCV
    K最短路 A*算法
    KMP算法
    北航复试机试题
    1385重建二叉树
    二维数组中的查找
    简单的单向链表
    Getting Started with WebRTC [note]
    我的c漏洞
    PeerConnection
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6279675.html
Copyright © 2020-2023  润新知