图像有用区域 【BFS】

    xiaoxiao2021-03-25  68

    图像有用区域

    时间限制: 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 来源 [张云聪]原创 上传者

    张云聪

    坑的地方好多。

    一开始用的dfs 结果爆了0.0   后来有用bfs ,,,high和wide 。。快烦死我。。醉了。。

    还有一点,,就是外面还要加一圈 1 。。因为可能整个图都是要被保存下来的,。。 这样可以测试到图的最外层是不是 0 ;

    代码

    #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include<stack> #include<queue> #define inf 0x3f3f3f #define M 1440+10 using namespace std; typedef struct { int x,y; }data; int wide,high; int map[970][1444]; int to[4][2]={0,1,0,-1,1,0,-1,0}; bool check(int x,int y) { return x>=0&&x<=high+1&&y>=0&&y<=wide+1&&map[x][y]; } void getmap() { memset(map,1,sizeof(map));  //  全变为1  for(int i=1;i<=high;i++) { for(int j=1;j<=wide;j++) { scanf("%d",&map[i][j]); } } } void bfs(data st) { queue<data>num;   data now,next;   num.push(st);   while(!num.empty())   {   now=num.front();   num.pop();//wang     for(int i=0;i<4;i++)   {   next.x=now.x+to[i][0];   next.y=now.y+to[i][1];     if(check(next.x,next.y))   {   map[next.x][next.y]=0;   num.push(next); }   } } } void print() { for(int i=1;i<=high;i++) { for(int j=1;j<=wide;j++) { if(j>1) printf(" ");  printf("%d",map[i][j]); } putchar('\n'); }   } int main() {   int n;   scanf("%d",&n);   while(n--)   {   scanf("%d%d",&wide,&high);   getmap();   data c;   c.x=0;c.y=0;   bfs(c);   print();  } return 0;  } 

    转载请注明原文地址: https://ju.6miu.com/read-13750.html

    最新回复(0)