图像有用区域
时间限制: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
-
-
//这道题应该不算难题,但是,有一点我还是不太明白,为什么用蓝色的就对,红色的就会错呢?
-
<span style="font-size:18px;">#include<stdio.h> #include<string.h> #include<queue> using namespace std; int map[2000][2000]; int row,col; int dir[4][2]={{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; queue<int> q; void bfs(int i,int j) { int x,y; <span style="color:#ff0000;">//int u; //u = i*col + j;</span> <span style="color:#333399;">q.push(i); q.push(j);</span> while(!q.empty()) { <span style="color:#ff0000;">//u = q.front();q.pop(); //x = u / col; //y = u % col;</span> <span style="color:#330099;background-color: rgb(192, 192, 192);">x = q.front();q.pop(); y = q.front();q.pop();</span> for(int k=0;k<4;k++) { int nx = x + dir[k][0]; int ny = y + dir[k][1]; if(nx>=0 && nx<=row+1 && ny>=0 && ny<=col+1) { if(map[nx][ny]!=0) { map[nx][ny] = 0; <span style="color:#330099;">q.push(nx); q.push(ny);</span> <span style="color:#ff0000;">//u = nx *col + ny; //q.push(u);</span> } } } } } int main() { int t; int i,j; scanf("%d",&t); while(t--) { for(i=0;i<1000;i++) { for(j=0;j<1500;j++) map[i][j] = 1; } scanf("%d%d",&col,&row); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) scanf("%d",&map[i][j]); } while(!q.empty()) q.pop(); bfs(0,0); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { if(j!=col) printf("%d ",map[i][j]); else printf("%d\n",map[i][j]); } } } return 0; }</span>
- 第一行输入测试数据的组数N(0<N<=6)