[关闭]
@jtahstu 2017-07-08T09:35:00.000000Z 字数 1287 阅读 2095

图像有用区域(广搜) - C++

算法


题目

url 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
  1. //
  2. // main.cpp
  3. // NYOJ92
  4. //
  5. // Created by jtusta on 2017/7/8.
  6. // Copyright © 2017年 jtahstu. All rights reserved.
  7. //
  8. #include <iostream>
  9. #include <queue>
  10. using namespace std;
  11. struct point
  12. {
  13. int x;
  14. int y;
  15. };
  16. int w, h;
  17. int map[970][1450];
  18. int dir[4][4] = {-1, 0, 0, 1, 1, 0, 0, -1};
  19. void BFS(int a, int b)
  20. {
  21. queue <point> q;
  22. point t1, t2;
  23. t1.x = a;
  24. t1.y = b;
  25. q.push(t1);
  26. while(!q.empty())
  27. {
  28. t1= q.front();
  29. q.pop();
  30. for(int i = 0; i < 4; ++i)
  31. {
  32. t2.x = t1.x + dir[i][0];
  33. t2.y = t1.y + dir[i][3];
  34. if(t2.x < 0 || t2.x > h+1 || t2.y < 0 || t2.y > w+1 || map[t2.x][t2.y] == 0)
  35. continue;
  36. map[t2.x][t2.y] = 0;
  37. q.push(t2);
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. int T;
  44. cin >> T;
  45. while(T--)
  46. {
  47. cin >> w >> h;
  48. for(int i = 0; i <= h; ++i)
  49. map[i][0] = map[i][w + 1] = 1;
  50. for(int j = 0; j <= w; ++j)
  51. map[0][j] = map[h + 1][j] = 1;
  52. for(int i = 1; i <= h; ++i)
  53. for(int j = 1; j <= w; ++j)
  54. cin >> map[i][j];
  55. BFS(0, 0);
  56. for(int i = 1; i <= h; ++i)
  57. for(int j = 1; j <= w; ++j)
  58. {
  59. if(j == w)
  60. cout << map[i][j] << endl;
  61. else
  62. cout << map[i][j] << ' ';
  63. }
  64. }
  65. return 0;
  66. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注