92-图像有用区域
- 内存限制:64MB 时间限制:3000ms 特判: No
- 通过数:19 提交数:90 难度: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
题意大概就是矩阵里被0包围的数字不改变,其他的都变为0,所以得用BFS。
有个坑人的地方,先输入的是列,再输入的是行。
先把矩阵外面包一圈1,再从(0,0)开始搜索,这样简单点,直接上代码
代码已AC
扫描二维码关注公众号,回复:
5982927 查看本文章
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int map[1000][1500];//存矩阵
int row, col;
int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; //上下左右
queue<int>q;
void BFS(int i, int j)
{
q.push(i);
q.push(j);
while(!q.empty())
{
int x1=q.front();
q.pop();
int y1=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=x1+dir[i][0];
int y=y1+dir[i][1];
if(x<0||y<0||x>row+1||y>col+1||map[x][y]==0)
continue;
map[x][y]=0;
q.push(x);
q.push(y);
}
}
}
int main()
{
int ncase;
scanf("%d", &ncase);
while(ncase--)
{
while(!q.empty()) //每次先清空队列
q.pop();
scanf("%d%d", &col, &row);
for(int i = 0; i <= col + 1; ++i) //矩阵行外加一圈1
{
map[0][i] = 1;
map[row + 1][i] = 1;
}
for(int i = 0; i <= row + 1; ++i) //矩阵列外加一圈1
{
map[i][0] = 1;
map[i][col + 1] = 1;
}
for(int i = 1; i <= row; ++i)
for(int j = 1; j <= col; ++j)
scanf("%d", &map[i][j]);
BFS(0, 0);//从左上角开始搜索
for(int i = 1; i <= row; ++i)
for(int j = 1; j <= col; ++j)
{
if(j != col)
printf("%d ", map[i][j]);
else
printf("%d\n", map[i][j]);
}
}
return 0;
}