图像有用区域
时间限制:3000 ms | 内存限制:65535 KB
难度:4
描述
“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。
已知黑线各处不会出现交叉(如图2),并且,除了黑线上的点外,图像中没有纯黑色(即像素为0的点)。
输入
第一行输入测试数据的组数N(0< N<=6)
每组测试数据的第一行是两个个整数W,H分表表示图片的宽度和高度(3<=W<=1440,3<=H<=960)
随后的H行,每行有W个正整数,表示该点的像素值。(像素值都在0到255之间,0表示黑色,255表示白色)
输出
以矩阵形式输出把黑色框之外的区域变黑之后的图像中各点的像素值。
样例输入
1
5 5
100 253 214 146 120
123 0 0 0 0
54 0 33 47 0
255 0 0 78 0
14 11 0 0 0
样例输出
0 0 0 0 0
0 0 0 0 0
0 0 33 47 0
0 0 0 78 0
0 0 0 0 0
在原图的外围加一圈1然后从(0,0)点开始,把可以到达的点都变成0就行了,原本是0的点自然不能到达
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<ctype.h>
#include<queue>
using namespace std;
int n,m,go[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int e[1500][1500];
bool vis[1500][1500];
struct node
{
int x,y;
node(int tx,int ty)
{
x=tx,y=ty;
}
};
bool check(int x,int y)
{
if(x<0||x>n+1||y<0||y>m+1||vis[x][y]||e[x][y]==0)
return false;
return true;
}
void bfs(int x,int y)
{
memset(vis,false,sizeof(vis));
queue<node>q;
q.push(node(x,y));
vis[x][y]=true;
while(!q.empty())
{
node st=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int tx=st.x+go[i][0];
int ty=st.y+go[i][1];
if(!check(tx,ty))continue;
vis[tx][ty]=true;
e[tx][ty]=0;
q.push(node(tx,ty));
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
for(int i=0; i<=n; i++)
e[i][0]=e[i][m+1]=1;
for(int i=0; i<=m; i++)
e[0][i]=1,e[n+1][i]=1;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&e[i][j]);
}
}
bfs(0,0);
for(int i=1;i<=n;i++)
{
printf("%d",e[i][1]);
for(int j=2;j<=m;j++)
printf(" %d",e[i][j]);
puts("");
}
}
return 0;
}