NYOJ 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
来源
[张云聪]原创
上传者
张云聪

解题:BFS还能干这种事情啊 真是神奇。。。。。。。。。。。。。井外的世界。。。。。。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #define LL long long
11 using namespace std;
12 int rows,cols,mp[1000][1500];
13 queue<int>q;
14 void bfs(int x,int y) {
15     const int dir[4][2] = {0,-1,0,1,-1,0,1,0};
16     int i,j,tx,ty,u,v;
17     q.push(x);
18     q.push(y);
19     while(!q.empty()) {
20         tx = q.front();
21         q.pop();
22         ty = q.front();
23         q.pop();
24         for(i = 0; i < 4; i++) {
25             u = tx+dir[i][0];
26             v = ty+dir[i][1];
27             if(!mp[u][v]) continue;
28             if(u < 0 || v < 0 || u >= 999 || v >= 1499) continue;
29             mp[u][v] = 0;
30             q.push(u);
31             q.push(v);
32         }
33     }
34 }
35 int main() {
36     int ks,i,j;
37     scanf("%d",&ks);
38     while(ks--) {
39         scanf("%d %d",&cols,&rows);
40         for(i = 0; i < 1000; i++)
41             for(j = 0; j < 1500; j++)
42                 mp[i][j] = 1;
43         for(i = 1; i <= rows; i++) {
44             for(j = 1; j <= cols; j++)
45                 scanf("%d",mp[i]+j);
46         }
47         while(!q.empty()) q.pop();
48         bfs(0,0);
49         for(i = 1; i <= rows; i++) {
50             for(j = 1; j <= cols-1; j++)
51                 printf("%d ",mp[i][j]);
52             printf("%d
",mp[i][j]);
53         }
54     }
55     return 0;
56 }
View Code

为什么要加圈1呢?嘻嘻。。。。。因为从左上角开始搜,如果全是0啊,而右边可能不是啊。。那么就会出现搜不下去的情况,导致WA。。。。。加圈是为了保0圈外面是连续的一片

 
原文地址:https://www.cnblogs.com/crackpotisback/p/3854648.html