图像有用区域(BFS)

图像有用区域
时间限制:3000 ms | 内存限制:65535 KB
难度:4

描述

“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。



                图1                                                        图2 

已知黑线各处不会出现交叉(如图2),并且,除了黑线上的点外,图像中没有纯黑色(即像素为0的点)。

输入
第一行输入测试数据的组数N(0

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>

using namespace std;

struct LNode
{
    int x,y;
};
int dirx[4] = {-1,0,1,0};
int diry[4] = {0,1,0,-1};
int graph[1447][1447];//地图
int h,w;//长和宽
int bfs(int x,int y)
{
    LNode node;
    node.x = x ;
    node.y = y;
    queue<LNode> Q;
    Q.push(node);
    while(!Q.empty())
    {
        LNode now = Q.front();
        Q.pop();
        for(int i = 0;i<4;i++)
        {
            int x = now.x + dirx[i];
            int y = now.y + diry[i];
            if(x < 0 || x  > h+1  || y < 0 || y > w+1) continue;
            if(graph[x][y])
            {
                graph[x][y] = 0;
                LNode next;
                next.x = x;
                next.y = y;
                Q.push(next);
            }
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int ncase;
    cin>>ncase;
    while(ncase--)
    {
        cin>>w>>h;
        for(int i =0;i<= h+1;i++)
            for(int j = 0;j<= w+1;j++)
                graph[i][j] = 1;
        for(int i = 1;i<=h;i++)
            for(int j = 1;j<=w;j++)
            cin>>graph[i][j];
        bfs(0,0);
        for(int i =1;i<=h;i++)
        {
            cout<<graph[i][1];
            for(int j = 2;j<=w ;j++)
            {
                cout<<" "<<graph[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值