图像有用区域
时间限制: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
/*
思路:在地图之外加上一圈非黑色(非0)的 外层,从0,0点开始bfs遇到不是0的,将地图上的点变为0,再压进队列。
非0的不压进队列。
把行和列搞反了,一直wa。。
*/
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef struct node
{
int x,y;
}point;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int row,col;
int maze[970][1450];
void init()
{
//memset(maze,0,sizeof(maze));
cin>>col>>row;
for(int i=0;i<=row+1;i++)
maze[i][0]=maze[i][col+1]=1;
for(int i=0;i<=col+1;i++)
maze[0][i]=maze[row+1][i]=1;
for(int i=1;i<=row;i++)
for(int j=1;j<=col;j++)
cin>>maze[i][j];
}
void print()
{
for(int i = 1; i <= row; ++i)
for(int j = 1; j <= col; ++j)
{
if(j == col)
cout << maze[i][j] << endl;
else
cout << maze[i][j] << ' ';
}
}
void bfs()
{
queue<point> qu;
point temp ={0,0};
qu.push(temp);
while(!qu.empty())
{
point v= qu.front();
qu.pop();
int x= v.x,y=v.y;
for(int i=0;i<4;i++)
{
int mx=x+dir[i][0];
int my=y+dir[i][1];
if( mx<0 || my<0 || mx>row+1 || my>col+1 || maze[mx][my]==0) continue;
point to ={mx,my};
maze[mx][my]=0;
qu.push(to);
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
init();
bfs();
print();
}
return 0;
}