图像有用区域
时间限制: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;
}