洛谷:P1162 填涂颜色(bfs)

洛谷:P1162 填涂颜色(bfs)

https://www.luogu.com.cn/problem/P1162

题目:

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

输入格式:

每组测试数据第一行一个整数n(1≤n≤30)

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

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

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

输入样例:

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

输出样例:

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

分析:

本题看似是一个判断闭合环的dfs,但是菜鸡的我并没有想出用dfs判断出闭合1环后如何快速检索环内的元素。

所以就换了一个思路,首先可以发现map中的0分为两类,一类是1环内的,另一类是可以联通到边界的,所以我只需要判断出与边界联通的0,让其它的0变为2就可以了。所以我用另外一个数组把与边界联通的0全部改为1,然后这其中0的索引就是原map要输出2的索引。

这个思路明了多了,现在就是判断与边界联通的0了,用for遍历map的外边界,检索到0就bfs(因为结束条件不明确)并把联通0改为1;如果边界没有0,那就更简单了,直接把原map是0的地方改为2即可。

代码有点冗长,但是感觉思路很好。

代码:

#include<iostream>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdio>
#include<queue>

using namespace std;

class node
{
    
    
public:
    int x;
    int y;
};

int ma[40][40];
int ansma[40][40];
int n;
int changex[4] = {
    
    -1, 0, 1, 0};
int changey[4] = {
    
    0, 1, 0, -1};
bool ju = false;
queue<node> q;
node no;

void bfs()
{
    
    
    while(!q.empty())
    {
    
    
        node temp = q.front();
        q.pop();
        for(int i = 0;i < 4;i++)
        {
    
    
            int newx = temp.x + changex[i];
            int newy = temp.y + changey[i];
            if(newx >=0 && newx < n && newy >= 0 && newy < n && ma[newx][newy] == 0)
            {
    
    
                temp.x = newx;
                temp.y = newy;
                q.push(temp);
                ma[newx][newy] = 1;
            }
        }
    }
}


int main()
{
    
    
    cin>>n;

    for(int i = 0;i < n;i++)
    {
    
    
        for(int j = 0;j < n;j++)
        {
    
    
            cin>>ma[i][j];
            ansma[i][j] = ma[i][j];
        }
    }

    for(int i = 0;i < n;i++)
    {
    
    
        for(int j = 0;j < n;j++)
        {
    
    
            if(i != 0 && i != n - 1)
            {
    
    
                if(ma[i][0] == 0)
                {
    
    
                    ju = true;
                    no.x = i;
                    no.y = 0;
                    q.push(no);
                    ma[i][0] = 1;
                    bfs();
                }
                else if(ma[i][n - 1] == 0)
                {
    
    
                    ju = true;
                    no.x = i;
                    no.y = n - 1;
                    q.push(no);
                    ma[i][n - 1] = 1;
                    bfs();
                }
                break;
            }
            else
            {
    
    
                if(ma[i][j] == 0)
                {
    
    
                    ju = true;
                    no.x = i;
                    no.y = j;
                    q.push(no);
                    ma[i][j] = 1;
                    bfs();
                }
            }
        }
    }

    if(ju)
    {
    
    
        for(int i = 0;i < n;i++)
        {
    
    
            for(int j = 0;j < n;j++)
            {
    
       
                if(ma[i][j] == 0)
                {
    
    
                    cout<<2<<" ";
                }
                else
                {
    
    
                    cout<<ansma[i][j]<<" ";
                }
            }
            cout<<endl;
        }
    }
    else
    {
    
    
        for(int i = 0;i < n;i++)
        {
    
    
            for(int j = 0;j < n;j++)
            {
    
    
                if(ansma[i][j] == 0)
                {
    
    
                    cout<<2<<" ";
                }
                else
                {
    
    
                    cout<<ansma[i][j]<<" ";
                }
            }
            cout<<endl;
        }
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46052886/article/details/113948255
今日推荐