nyoj 92 图像有用区域 【bfs】
来源:互联网 发布:珠海办公软件培训 编辑:程序博客网 时间:2024/04/28 05:49
图像有用区域
时间限制: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表示白色) - 输出
- 以矩阵形式输出把黑色框之外的区域变黑之后的图像中各点的像素值。
- 样例输入
15 5100 253 214 146 120123 0 0 0 054 0 33 47 0255 0 0 78 014 11 0 0 0
- 样例输出
0 0 0 0 00 0 0 0 00 0 33 47 00 0 0 78 00 0 0 0 0
分析:在原来的图上加一圈1.
代码:
#include <stdio.h>#include <string.h>#include <queue>#define W 1445#define H 965using namespace std;const int dx[] = {0, 0, 1, -1};const int dy[] = {1, -1, 0, 0};int map[H][W], w, h;struct node{int x, y;};int limit(int x, int y){return (x>=0&&x<=h+1&&y>=0&&y<=w+1); //这里的x,y,一定小于等于w+1,h+1;} void bfs(){int i;node st;st.x = st.y = 0;queue<node > q;q.push(st);while(!q.empty()){node temp = q.front();for(i = 0; i < 4; i ++){node cur = temp;cur.x+=dx[i]; cur.y+=dy[i];if(map[cur.x][cur.y] == 0) continue;if(!limit(cur.x, cur.y)) continue;//if(cur.x < 0||cur.y <0||cur.x > h+1||cur.y > w+1 || map[cur.x][cur.y] == 0) continue;map[cur.x][cur.y] = 0;q.push(cur);}q.pop();}}int main(){int t, i, j;scanf("%d", &t);while(t --){scanf("%d%d", &w, &h);for(i = 0; i <= w+1; i ++){map[0][i] = 1;map[h+1][i] = 1;}for(i = 0; i <= h+1; i++){map[i][0] = 1;map[i][w+1] = 1;}for(i = 1; i <= h; i ++)for(j = 1; j <= w; j++)scanf("%d", &map[i][j]);bfs();for(i = 1; i <= h; i++){printf("%d", map[i][1]);for(j = 2; j<= w; j++)printf(" %d", map[i][j]);printf("\n");}printf("\n");}return 0;}
0 0
- NYOJ-92 图像有用区域 BFS
- nyoj 92 图像有用区域 【bfs】
- NYOJ 92--图像有用区域【BFS && 水题】
- NYOJ 92 图像有用区域 【bfs】
- nyoj--92 图像有用区域(bfs)
- NYOJ 92 图像有用区域 (BFS)
- nyoj 92 图像有用区域 bfs
- NYOJ-92 图像有用区域(bfs)
- 【NYOJ-92- 图像有用区域-BFS】
- NYOJ 92 图像有用区域 bfs
- NYOJ 92 图像有用区域(BFS)
- nyoj 92 图像有用区域 【BFS】 【好题 】
- NYOJ 92 图像有用区域 (经典的 bfs)
- nyoj 图像有用区域(BFS)
- nyoj ACM:图像有用区域 (bfs)
- NYOJ 92 图像有用区域
- NYOJ 92 图像有用区域
- nyoj 92 图像有用区域
- gdb core文件调试
- scons简介
- QQ和微信差在哪儿?
- C++中的单例模式
- 学习网站
- nyoj 92 图像有用区域 【bfs】
- 数据库索引操作
- Spring Security3源码分析
- weblogic无法启动
- ios学习--NSDictionary
- mysql操作笔记
- 解决Android Library引用error: No resource identifier found for attribute
- 第五次作业
- mysql 的load data infile的用法