您的位置:首页 > 其它

NYOJ-92 图像有用区域 BFS

2014-04-07 17:53 134 查看

图像有用区域

时间限制:3000 ms  |  内存限制:65535 KB 难度:4
描述

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

     

                图1                                                        图2 

已知黑线各处不会出现交叉(如图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

广搜,不过要在图像周围围一圏不为黑的像素

01.
#include
<iostream>
02.
#include
<queue>
03.
using
 
namespace
 
std;
04.
 
05.
int
 
image[1000][1500],width,height;
06.
int
 
dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
07.
 
08.
void
 
bfs()
09.
{
10.
queue<
int
>
q;
11.
int
 
x,y,xt,yt;
12.
q.push(0);
13.
q.push(0);
14.
while
(!q.empty())
15.
{
16.
x=q.front();
17.
q.pop();
18.
y=q.front();
19.
q.pop();
20.
for
(
int
 
i=0;i<4;i++)
21.
{
22.
xt=x+dir[i][0];
23.
yt=y+dir[i][1];
24.
if
(xt<0||xt>height+1||yt<0||yt>width+1)
continue
;
25.
if
(image[xt][yt]==0)
continue
;
26.
image[xt][yt]=0;
27.
q.push(xt);
28.
q.push(yt);
29.
}
30.
}
31.
}
32.
 
33.
int
 
main()
34.
{
35.
int
 
N;
36.
cin>>N;
37.
while
(N--)
38.
{
39.
cin>>width>>height;
40.
for
(
int
 
i=0;i<=width+1;i++)
41.
{
42.
image[0][i]=1;
43.
image[height+1][i]=1;
44.
}
45.
for
(
int
 
i=0;i<=height+1;i++)
46.
{
47.
image[i][0]=1;
48.
image[i][width+1]=1;
49.
}
50.
for
(
int
 
i=1;i<=height;i++)
51.
for
(
int
 
j=1;j<=width;j++)
52.
cin>>image[i][j];
53.
bfs();
54.
for
(
int
 
i=1;i<=height;i++)
55.
for
(
int
 
j=1;j<=width;j++)
56.
cout<<image[i][j]<<(j==width?
'\n'
:
'
'
);
57.
}
58.
return
 
0;
59.
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: