您的位置:首页 > 其它

NYOJ 92 图像有用区域 (BFS)

2016-06-07 20:25 183 查看
题目92

题目信息

运行结果

本题排行

讨论区


图像有用区域

时间限制: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


分析:

BFS,需要在图外加一圈1,从左上角开始广搜,把坐标入队即可。

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1500;
int maze[maxn][maxn];
int vis[maxn][maxn];
int t;
int w, h;
int dir[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
queue<int> que;

void bfs(int x, int y)
{
que.push(x);
que.push(y);
while (!que.empty()){
x = que.front();
que.pop();
y = que.front();
que.pop();
for (int i = 0; i < 4; i++){
int dx = x + dir[i][0];
int dy = y + dir[i][1];
if (dx >= 0 && dx <= h + 1 && dy >= 0 && dy <= w + 1 && maze[dx][dy]){
maze[dx][dy] = 0;
que.push(dx);
que.push(dy);
}
}
}
}

int main()
{
cin >> t;
while (t--){
memset(maze, 0, sizeof(maze));
cin >> w >> h;
for (int i = 1; i <= h; i++){
for (int j = 1; j <= w; j++){
cin >> maze[i][j];
}
}
for (int i = 0; i <= h + 1; i++){
maze[i][0] = 1;
maze[i][h + 1] = 1;
}
for (int j = 0; j <= w; j++){
maze[0][j] = 1;
maze[w + 1][j] = 1;
}
bfs(0, 0);
for (int i = 1; i <= h; i++){
for (int j = 1; j <= w; j++){
if (j != w)
cout << maze[i][j] << " ";
else
cout << maze[i][j] << endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: