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. cocos2d-html5 中的性能优化

    游戏开发中,难免会遇到性能瓶颈.图片一多,渲染批次就会直线上升,任何动画都会变得闪动. OpenGL ES优化的问题,主要考虑两个方面:内存存储和运行速度. 2D游戏中的最占内存的就是图片资源,一张图 ...

  2. EF中使用Linq的Lambda比较字符串格式日期大小

    在使用EF时,想要比较字符串类型的日期时,参考以下: SQL语句: 1)select * from TableName where StartTime > ‘2015-04-08‘ 2)sele ...

  3. jsp中引入JavaScript的方法

    1:在页面中直接嵌入JavaScript <script language="javascript">..........</script> 2:链接外部J ...

  4. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

  5. TX2上安装spi和uart驱动

    替换/boot目录下的Image文件 重新烧写dtb文件.烧写方式参考. 文件下载

  6. Day3-Python基础3--默认参数和参数组

    一.默认参数 先看下下面的代码: def stu_register(name,age,country,course): print("----注册学生信息------") prin ...

  7. 嵌入式系统LINUX环境搭建

    Linux kernel Complier: http://supportopensource.iteye.com/blog/680483 sudo make mrproper         净化解 ...

  8. 机器学习:SVM(非线性数据分类:SVM中使用多项式特征和核函数SVC)

    一.基础理解 数据:线性数据.非线性数据: 线性数据:线性相关.非线性相关:(非线性相关的数据不一定是非线性数据) 1)SVM 解决非线性数据分类的方法 方法一: 多项式思维:扩充原本的数据,制造新的 ...

  9. df 命令-显示目前磁盘剩余的磁盘空间

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  10. canvas渐变

    代码: 1 /** 2 * Created by Administrator on 2016/1/29. 3 */ 4 function draw(id){ 5 var canvas = docume ...