luogu P1162 填涂颜色

https://www.luogu.org/problem/show?pid=1162

 

//其实很简单的吧
//就是最外圈加一圈0 ,然后把外圈里面的0都遍历了
//剩下的0 就变成2 就行了
#include<bits/stdc++.h>
using namespace std;
typedef pair <int,int> pii;
int n ,s[50][50];
bool vis[50][50];
int fx[]={-1,0,0,1};
int fy[]={0,-1,1,0};

bool check(int x,int y)
{
    if(!vis[x][y] && s[x][y] == 0 && x>=0 && x<=n+1 && y>=0 && y<=n+1)
        return 1;
    return 0;
}

void bfs(int x,int y)
{
    queue<pii > que;
    que.push({x,y});
    //cout<<"yes1"<<endl;
    while (que.size() )
    {
        pii now = que.front(); que.pop();
        if( vis[now.first][now.second] )
            continue;
        vis[now.first][now.second] = 1;//注意该点已经访问过   这里很容易忘记的吧
        //cout<<"yes2"<<endl;
        for(int i=0;i<4;i++)
        {
            //cout<<"yes3"<<endl;
            int dx= now.first+fx[i];
            int dy= now.second+fy[i];

            if(check(dx,dy))
            {
                //cout<<"yes3"<<endl;
                que.push({dx,dy});
            }
        }
    }
}

int main ()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&s[i][j]);
    //cout<<endl;
    //因为外圈已经本身就是0了
    //从第0行到第n+1行
    bfs(0,0);


    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if( vis[i][j] )
                cout<<0<<" ";
            else if(s[i][j] == 1)
                cout<<1<<" ";
            else
                cout<<2<<" ";
        }
        cout<<endl;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值