图像有用区域

题目:

描述

“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。代码很简单,很简洁。但不知为啥一直RE,最后发现dfs会溢出。也没多想了,这是dfs的通病。就直接改为bfs,同样的思路过去。ac


wa代码:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
using namespace std;
//#define LL __int64

#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif

int a[965][1445];

int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int w,h;

void dfs(int x,int y) {
    a[x][y]=0;
    for(int i=0; i<4; i++) {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(a[xx][yy]==0||xx<1||xx>h+1||yy<1||yy>w+1) {
            continue;
        } else
            dfs(xx,yy);
    }
}

int main() {
    int t;
    cin>>t;
    while(t--) {
        memset(a,1,sizeof(a));

        scanf("%d%d",&w,&h);
        for(int i=1; i<=h; i++) {
            for(int j=1; j<=w; j++) {
                scanf("%d",&a[i][j]);
            }
        }
        dfs(1,1);

        for(int i=1; i<=h; i++) {
            for(int j=1; j<w; j++) {
                printf("%d ",a[i][j]);
            }
            printf("%d\n",a[i][w]);
        }

    }
    return 0;
}



ac代码:


 
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
using namespace std;
//#define LL __int64

#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif

int a[965][1445];
int b[965][1445];
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int w,h;

struct point {
    int x,y;
};
queue <point> q;

void bfs(int x,int y) {
    point t;
    t.x=x;
    t.y=y;
    while(!q.empty()) q.pop();
    q.push(t);
    while(!q.empty()) {
        t=q.front();
        q.pop();
        for(int i=0; i<4; i++) {
            int xx=t.x+dir[i][0];
            int yy=t.y+dir[i][1];
            if(a[xx][yy]==0||xx<0||xx>h+1||yy<0||yy>w+1) {
                continue;
            }
            a[xx][yy]=0;
            point t2;
            t2.x=xx;
            t2.y=yy;
            q.push(t2);
        }

    }

}

int main() {
    int t;
    cin>>t;
    while(t--) {
        memset(a,1,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d%d",&w,&h);
        for(int i=1; i<=h; i++) {
            for(int j=1; j<=w; j++) {
                scanf("%d",&a[i][j]);
            }
        }
        bfs(0,0);
        for(int i=1; i<=h; i++) {
            for(int j=1; j<w; j++) {
                printf("%d ",a[i][j]);
            }
            printf("%d\n",a[i][w]);
        }

    }
    return 0;
}

/*

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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值