nyoj 92 图像有用区域 【BFS】 【好题 】
2015-06-20 12:18
225 查看
难度:4
描述
“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。
![](http://acm.nyist.net/JudgeOnline/admin/kind/attached/20101110121709_32589.jpg)
![](http://acm.nyist.net/JudgeOnline/admin/kind/attached/20101110122055_50520.jpg)
图1 图2
已知黑线各处不会出现交叉(如图2),并且,除了黑线上的点外,图像中没有纯黑色(即像素为0的点)。
输入第一行输入测试数据的组数N(0<N<=6)
每组测试数据的第一行是两个个整数W,H分表表示图片的宽度和高度(3<=W<=1440,3<=H<=960)
随后的H行,每行有W个正整数,表示该点的像素值。(像素值都在0到255之间,0表示黑色,255表示白色)
输出以矩阵形式输出把黑色框之外的区域变黑之后的图像中各点的像素值。样例输入
样例输出
[/code]
[/code]
[/code]
[/code]
[/code]
[/code]
[/code]
图像有用区域
时间限制:3000 ms | 内存限制:65535 KB难度:4
描述
“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。
![](http://acm.nyist.net/JudgeOnline/admin/kind/attached/20101110121709_32589.jpg)
![](http://acm.nyist.net/JudgeOnline/admin/kind/attached/20101110122055_50520.jpg)
图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
题目意思:在W*H矩阵有一个用0构成的圈,你需要把圈外的所有数字变成0,圈内不变。然后输出即可。 ac代码在最后面
咋看简单搜索嘛,果断DFS结果RTE 无语。。。 然后BFS各种错,下面是我AC过程中的错误,希望对你我都有帮助。
DFS RTE代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int map[964][1444]; int H, W; int move[8][2] = {0,1, 0,-1, 1,0, -1,0}; void getmap() { int i, j; for(i = 1; i <= H; i++) { for(j = 1; j <= W; j++) scanf("%d", &map[i][j]); } } bool judge(int x, int y) { return x >= 1 && x <= H && y >= 1 && y <= W && map[x][y]; } void DFS(int x, int y) { map[x][y] = 0; for(int k = 0; k < 4; k++) { int next_x = x + move[k][0]; int next_y = y + move[k][1]; if(judge(next_x, next_y)) { DFS(next_x, next_y); } } } void output() { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(j > 1) printf(" "); printf("%d", map[i][j]); } printf("\n"); } } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &W, &H); getmap(); DFS(1, 1); output(); } return 0; }
[/code]
没办法BFS吧: 结果又错了。
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; int map[964][1444]; int H, W; int move[8][2] = {0,1, 0,-1, 1,0, -1,0, 1,-1, -1,1, 1,1, -1,-1}; struct Node { int x, y; }; void getmap() { int i, j; for(i = 1; i <= H; i++) { for(j = 1; j <= W; j++) scanf("%d", &map[i][j]); } } bool judge(int x, int y) { return x >= 1 && x <= H && y >= 1 && y <= W && map[x][y]; } void BFS(int x, int y) { Node now, next; queue<Node> Q; now.x = x; now.y = y; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); for(int k = 0; k < 8; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; if(judge(next.x, next.y)) { map[next.x][next.y] = 0; Q.push(next); } } } } void output() { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(j > 1) printf(" "); printf("%d", map[i][j]); } printf("\n"); } } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &W, &H); getmap(); BFS(1, 1); output(); } return 0; }
仔细想想,方向搜多了比如这样的数据:
3 4 1 1 0 1 1 0 2 0 1 1 0 1
[/code]
正确答案:
0 0 0 0 0 0 2 0 0 0 0 0
[/code]
而我的代码输出是:
0 0 0 0 0 0 0 0 0 0 0 0
[/code]
改过之后继续。
结果又错了:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; int map[964][1444]; int H, W; int move[4][2] = {0,1, 0,-1, 1,0, -1,0}; struct Node { int x, y; }; void getmap() { int i, j; for(i = 1; i <= H; i++) { for(j = 1; j <= W; j++) scanf("%d", &map[i][j]); } } bool judge(int x, int y) { return x >= 1 && x <= H && y >= 1 && y <= W && map[x][y]; } void BFS(int x, int y) { Node now, next; queue<Node> Q; now.x = x; now.y = y; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); for(int k = 0; k < 4; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; if(judge(next.x, next.y)) { map[next.x][next.y] = 0; Q.push(next); } } } } void output() { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(j > 1) printf(" "); printf("%d", map[i][j]); } printf("\n"); } } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &W, &H); getmap(); BFS(1, 1); output(); } return 0; }
[/code]
真心醉了,好好想了想,发现有这样的数据:
3 3 0 0 1 0 1 0 0 0 0
正确答案:
0 0 0 0 1 0 0 0 0
我的代码输出:
0 0 1 0 1 0 0 0 0
[/code]
这也就是说如果直接BFS的话,还要考虑到边界为0的情况。因此我们应该在外面加一圈非0数,然后BFS。改了继续。
终于过了:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; int map[964][1444]; int H, W; int move[4][2] = {0,1, 0,-1, 1,0, -1,0}; struct Node { int x, y; }; void getmap() { int i, j; for(i = 0; i <= H+1; i++) { for(j = 0; j <= W+1; j++) map[i][j] = 1; } for(i = 1; i <= H; i++) { for(j = 1; j <= W; j++) scanf("%d", &map[i][j]); } } bool judge(int x, int y) { return x >= 0 && x <= H+1 && y >= 0 && y <= W+1 && map[x][y]; } void BFS(int x, int y) { Node now, next; queue<Node> Q; now.x = x; now.y = y; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); for(int k = 0; k < 4; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; if(judge(next.x, next.y)) { map[next.x][next.y] = 0; Q.push(next); } } } } void output() { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(j > 1) printf(" "); printf("%d", map[i][j]); } printf("\n"); } } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &W, &H); getmap(); BFS(0, 0); output(); } return 0; }
[/code]
相关文章推荐
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第八课: Cocos2D-X引擎框架3
- iOS中获取通讯录的方法
- 最小生成树算法和最短路径算法的区别是什么?
- 安卓学习笔记之activity生命周期
- unix环境高级编程——进程状态
- 记录网址
- Android开发_获取手机屏幕的尺寸
- Java并发编程-29-非阻塞式线程安全列表-ConcurrentLinkedDeque
- Android -- Looper.prepare()和Looper.loop() —深度版
- [POJ 1330] Nearest Common Ancestors (朴素方法)
- linux ssh rsync
- Activity的生命周期
- c语言第一周总结-Basic
- Java基础 管道流 RandomAccessFile DataStream ByteArrayStream 字符编码
- 机器学习:更多的数据总是优于更好的算法吗?
- C#中TextBox实现输入提示功能的方法
- 类个对象
- 连awk都不熟悉还搞什么Linux后台开发啊-----顺便分享一下与awk有关的笔试、面试经历
- java泛型中extends 和 super的区别
- 日语学习之沪江N3基础 20150620 -1