http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=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
来源
[张云聪]原创
上传者
张云聪
开始用的dfs,爆栈了:
#include <bits/stdc++.h>
using namespace std;
int mp[1500][1500];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int h, w; void dfs(int x, int y){
mp[x][y] = 0;
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx >= 0 && xx < w && yy >= 0 && yy < h)
if(mp[xx][yy]){
dfs(xx, yy);
}
}
} int main(){
int n;
cin >> n;
while(n--){
cin >> h >> w;
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
cin >> mp[i][j];
}
}
for(int i = 0; i < w; i++){ //由外层边界dfs
for(int j = 0; j < h; j++){
if((i == 0 || j == 0 || i == w - 1 || j == h - 1) && mp[i][j]){
dfs(i, j);
}
}
}
for(int i = 0; i < w; i++){
cout << mp[i][0];
for(int j = 1; j < h; j++){
cout << " " << mp[i][j];
}
cout << endl;
}
} return 0;
}

  后面用了广搜ac:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct node{
int x, y;
};
int mp[1000][1500];
queue <node> myq;
int n, m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0}; void bfs(int x, int y){
node a;
a.x = x, a.y = y;
myq.push(a);
mp[a.x][a.y] = 0;
while(!myq.empty()){
node b;
b = myq.front();
myq.pop();
int myx = b.x, myy = b.y;
node c;
for(int i = 0; i < 4; i++){
int xx = myx + dx[i];
int yy = myy + dy[i];
if(xx >= 0 && xx <= n + 1 && yy >= 0 && yy <= m + 1){
if(mp[xx][yy]){
mp[xx][yy] = 0;
c.x = xx;
c.y = yy;
myq.push(c);
}
}
}
}
} int main(){
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--){
cin >> m >> n;
memset(mp, 1, sizeof(mp));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> mp[i][j];
}
}
bfs(0, 0);
for(int i = 1; i <= n ; i++){
cout << mp[i][1];
for(int j = 2; j <= m; j++){
cout << " " << mp[i][j];
}
cout << endl;
}
}
return 0;
}

  

36-图像有用区(dfs, bfs)的更多相关文章

  1. ACM 图像有用区域

    图像有用区域 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 “ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以 ...

  2. nyoj 92 图像有用区域

    点击打开链接 图像有用区域 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 "ACKing"同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取 ...

  3. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  4. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  5. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  6. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  7. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  8. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  9. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

随机推荐

  1. LeetCode OJ:Largest Number(最大数字)

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  2. 通过 objc_setAssociatedObject alert 和 button关联 及传值

    原文地址 http://blog.csdn.net/lengshengren/article/details/16886915 //唯一静态变量key static const char associ ...

  3. stl_algo.h

    stl_algo.h // Filename: <stl_algo.h> // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  4. Ubuntu 16.10 Apache PHP Server

    /******************************************************************************************* * Ubunt ...

  5. [BZOJ5249][多省联测2018]IIIDX

    bzoj luogu sol 首先可以把依赖关系转成一个森林.自下而上维护出每个点的\(size\),表示这关解锁以后一共有多少关. 考虑没有重复数字的情况. 直接从小往大贪心把每个数赋给当前已解锁的 ...

  6. Linux 终端 忽略大小写

    忘了在哪里看到的了,记录一下. 在-/.inputrc中加入一行 set completion-ignore-case on 搞定! 这样在终端输入.补全时就忽略大小写了.当然,Linux本身还是区分 ...

  7. 解决mac下sublime中文乱码

    Mac OS X 属于 Apple 独家演绎的 Unix 分支版本,默认使用 UTF-8 编码,当使用不同开发平台的小伙伴们,共同维护一份代码的时候,尤其现在很多人都还在用 Windows 系统的时候 ...

  8. Python 算法之冒泡排序

    冒泡排序 冒泡排序算法的原理如下:(从后往前) 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是 ...

  9. lnmp下安装curl openssl扩展

    openssl http://www.mamicode.com/info-detail-1957696.html curl https://blog.csdn.net/qq_34372929/arti ...

  10. -3dB的理解

    -3dB到底是什么?集成运放-3dB带宽又是什么? 以无源高通电路为例,介绍-3dB的意义. 输出与输入只比: Au=Uo/Ui=R/(R+1/j*2*PI*f*C)=1/(1+1/j*2*PI*f* ...