zoukankan      html  css  js  c++  java
  • 1102-黑白图像

    描述

     

    输入一个n×n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数。如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块。如下图所示的图形有3个八连块。

    输入

    第1行输入一个正整数n(n≤700),此后输入n行,每行是由n个0或1组成的字符串。

    输出

    在输入黑白图像中,八连块的个数

    样例输入

    6

    100100

    001010

    000000

    110000

    111000

    010100

    样例输出

    3

    #include<iostream>
    #include<deque>
    #include<math.h>
    #include<string>
    #define PI 4.0*atan(1.0)
    #define MAX_32_INT
    #define MAX_64_INT
    #define max a>=b?(a):(b)
    #define min a<b?(a):(b)
    using namespace std;
    #define MAX 1001
    
    int flag[MAX][MAX];
    int temp[MAX][MAX];
    
    struct node
    {
        int x;
        int y;
    };
    
    deque <struct node *>S;
    
    int f(const void *a,const void *b)
    {
        return 1 ;
    }
    
    void bfs(int a,int b)
    {
        struct node *p;
        p=new struct node;
        p->x=a;
        p->y=b;
        temp[a][b]=0;
        S.push_back(p);
        while(S.size()>0)
        {
            struct node *p1;
            p1=new struct node;
            struct node *p2;
            p1=S.front();
            temp[p1->x][p1->y]=0;
            S.pop_front();
            if(!flag[p1->x-1][p1->y]&&temp[p1->x-1][p1->y])
            {    
                p2=new struct node;
                p2->x=p1->x-1;
                p2->y=p1->y;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x-1][p1->y+1]&&temp[p1->x-1][p1->y+1])
            {    
                p2=new struct node;
                p2->x=p1->x-1;
                p2->y=p1->y+1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x][p1->y+1]&&temp[p1->x][p1->y+1])
            {    
                p2=new struct node;
                p2->x=p1->x;
                p2->y=p1->y+1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x+1][p1->y+1]&&temp[p1->x+1][p1->y+1])
            {    
                p2=new struct node;
                p2->x=p1->x+1;
                p2->y=p1->y+1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x+1][p1->y]&&temp[p1->x+1][p1->y])
            {    
                p2=new struct node;
                p2->x=p1->x+1;
                p2->y=p1->y;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x+1][p1->y-1]&&temp[p1->x+1][p1->y-1])
            {    
                p2=new struct node;
                p2->x=p1->x+1;
                p2->y=p1->y-1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x][p1->y-1]&&temp[p1->x][p1->y-1])
            {    
                p2=new struct node;
                p2->x=p1->x;
                p2->y=p1->y-1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
            if(!flag[p1->x-1][p1->y-1]&&temp[p1->x-1][p1->y-1])
            {    
                p2=new struct node;
                p2->x=p1->x-1;
                p2->y=p1->y-1;
                flag[p2->x][p2->y]=1;
                temp[p2->x][p2->y]=0;
                S.push_back(p2);
            }
    
    
        }
    }
    
    int main()
    {
        int n,i,j;
        while(scanf("%d",&n)!=EOF)
        {
            S.clear();
            int count=0;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    scanf("%1d",&temp[i][j]);
                    flag[i][j]=0;
                }
            }
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(temp[i][j]&&(!flag[i][j]))
                    {
                        flag[i][j]=1;
                        bfs(i,j);
                        count++;
                        j++;
                    }
                }
            }
            cout<<count<<endl;
        }
        
        return 0;
    } 
    

      

  • 相关阅读:
    2021牛客寒假算法基础集训营4 B. 武辰延的字符串(二分/Hash/exkmp)
    2021牛客寒假算法基础集训营4 H. 吴楚月的表达式
    2021牛客寒假算法基础集训营4 J. 邬澄瑶的公约数(GCD/唯一分解定理)
    leetcode 995. K 连续位的最小翻转次数(差分)
    robot 源码解读2【run.py执行流程】
    robot 源码解读1【代码量】
    python计算代码的行数
    为什么要用yield
    任意网站添加目录
    mac 定时执行脚本
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3436910.html
Copyright © 2011-2022 走看看