36-图像有用区(dfs, bfs)
- 描述
-
“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)的更多相关文章
- ACM 图像有用区域
图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 “ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以 ...
- nyoj 92 图像有用区域
点击打开链接 图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 "ACKing"同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [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 ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
随机推荐
- swagger 在apache CXF 中的使用——JAX-RS Swagger2Feature
The CXF Swagger2Feature allows you to generate Swagger 2.0 documents from JAX-RS service endpoints w ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)
数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...
- uuid 学习
#include <vector> #include <iostream> #include <boost/uuid/uuid.hpp> #include < ...
- [转]NYOJ-511-移动小球
大学生程序代写 http://acm.nyist.net/JudgeOnline/problem.php?pid=511 这道题很容易想到要构建一个循环链表来确定每个球的相对位置,就是操作比较繁琐,考 ...
- 20179203 《Linux内核原理与分析》第十二周作业
Return-to-libc 攻击实验 一.实验描述 缓冲区溢出的常用攻击方法是用 shellcode 的地址来覆盖漏洞程序的返回地址,使得漏洞程序去执行存放在栈中 shellcode.为了阻止这种类 ...
- 利用sort对数组快速排序
// sort内部使用快速排序,每次比较两个元素大小的时候如果没有参数,则直接判断字母表,如果有参数,则把正在比较的两个参数传入自定义方法并调用(正在比较的两个数会传给自定义方法的v1.v2),如果返 ...
- Java实现Bag类
Java实现Bag类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Scan ...
- PIX v2版本中Query 失败时, ERR段的构造
在ITI-9中描述PIX query事务的几个TestCase场景.其中有些是对于Query失败的描述. ERR 段包含Error location, Error code, Error code t ...
- 在Linux上利用core dump和GDB调试segfault
时常会遇到段错误(segfault),调试非常费劲,除了单元测试和基本测试外,有些时候是在在线环境下,没有基本开发和测试工具,这就需要调试的技能.以前介绍过使用strace进行系统调试和追踪<l ...