zoukankan      html  css  js  c++  java
  • luogu P1162 填涂颜色

    P1162 填涂颜色

    题目描述

    由数字0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向。现要求把闭合圈内的所有空间都填写成2.例如:6X6的方阵(n=6),涂色前和涂色后的方阵如下:

    0 0 0 0 0 0 0 0 0 0 0 0

    0 0 1 1 1 1 0 0 1 1 1 1

    0 1 1 0 0 1 0 1 1 2 2 1

    1 1 0 0 0 1 1 1 2 2 2 1

    1 0 0 0 0 1 1 2 2 2 2 1

    1 1 1 1 1 1 1 1 1 1 1 1

    输入输出格式

    输入格式:

    每组测试数据第一行一个整数:n。其中n(1<=n<=30)

    接下来n行,由0和1组成的nXn的方阵。

    方阵内只有一个闭合圈,圈内至少有一个0。

    //感谢黄小U饮品指出本题数据和数据格式不一样. 已修改(输入格式)

    输出格式:

    已经填好数字2的完整方阵。

    输入输出样例

    输入样例#1:
    6
    0 0 0 0 0 0
    0 0 1 1 1 1
    0 1 1 0 0 1
    1 1 0 0 0 1
    1 0 0 0 0 1
    1 1 1 1 1 1
    
    输出样例#1:
    0 0 0 0 0 0
    0 0 1 1 1 1
    0 1 1 2 2 1
    1 1 2 2 2 1
    1 2 2 2 2 1
    1 1 1 1 1 1
    

    说明

    1<=n<=30

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    
    using namespace std;
    const int N=31;
    const int xd[]={0,1,0,-1};
    const int yd[]={-1,0,1,0};
    
    struct node{
        int x,y;
    }now,nxt,pus;
    int a[N][N];
    queue<node>q;
    int n;
    
    inline int read()
    {
        int x=0;
        char c=getchar();
        while(c<'0'||c>'9')c=getchar();
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
        return x;
    }
    
    inline void bfs(int x,int y)
    {
        now.x=x;
        now.y=y;
        q.push(now);
        while(!q.empty())
        {
            nxt=q.front();
            q.pop();
            a[nxt.x][nxt.y]=3;
            for(int i=0;i<4;i++)
            {
                int xx=nxt.x+xd[i];
                int yy=nxt.y+yd[i];
                if(!a[xx][yy]&&xx>=0&&xx<=n&&y>=0&&y<=n)
                {
                    pus.x=xx;
                    pus.y=yy;
                    a[xx][yy]=3;
                    q.push(pus);
                }
            }
        }    
    }
    
    int main()
    {
        n=read();
        
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                a[i][j]=read();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(!a[i][j]&&(i==1||i==n||j==1||j==n))
                    bfs(i,j);
        
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]==3)
                    printf("0 ");
                else 
                    if(a[i][j]==1)
                        printf("1 ");
                    else 
                        printf("2 ");
            }
            printf("
    ");        
        }    
        return 0;
    }
    /*
    6
    0 0 0 0 0 0
    0 0 1 1 1 1
    0 1 1 0 0 1
    1 1 0 0 0 1
    1 0 0 0 0 1
    1 1 1 1 1 1
    5
    0 0 0 0 0
    0 0 0 0 0
    1 1 1 1 1
    1 0 0 0 1 
    1 0 0 0 1
    */
  • 相关阅读:
    September 17th 2016 Week 38th Saturday
    【2016-09-16】UbuntuServer14.04或更高版本安装问题记录
    September 16th 2016 Week 38th Friday
    September 11th 2016 Week 38th Sunday
    September 12th 2016 Week 38th Monday
    September 10th 2016 Week 37th Saturday
    September 9th 2016 Week 37th Friday
    c++暂停
    八皇后问题
    ( 转转)Android初级开发第九讲--Intent最全用法(打开文件跳转页面等)
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7073377.html
Copyright © 2011-2022 走看看