zoukankan      html  css  js  c++  java
  • (寒假集训)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;
    }

  • 相关阅读:
    [jQuery学习系列六]6-jQuery实际操作小案例
    [Java拾遗一] XML的书写规范与解析.
    [数据库操作]Java中的JDBC的使用方法.
    [Java拾遗二]Tomact及Http 部分总结.
    [Java拾遗三]JavaWeb基础之Servlet
    [Java拾遗四]JavaWeb基础之Servlet_Request&&Response
    [Java拾遗五]使用Session防止表单重复提交
    [数据库连接池] Java数据库连接池--DBCP浅析.
    [开发工具]Java开发常用的在线工具
    [数据库连接池二]Java数据库连接池--C3P0和JDNI.
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6279675.html
Copyright © 2011-2022 走看看