Nyoj 92 useful areas of images

Source: Internet
Author: User
Useful areas of imagesTime Limit: 3000 MS | memory limit: 65535 kb difficulty: 4
Description

"ACKing" encountered a problem when he was working on an image processing project. He needed to extract images within the area of a black line in the image, now, please help him complete the first step and turn all the areas outside the black line into black.

Figure 1 Figure 2

It is known that there will be no crossover everywhere in the black line (2), and, except for the black line points, there is no pure black in the image (that is, the pixel is 0 points ).

 
Input
Number of groups of the first input test data N (0 <n <= 6)
The first row of each group of test data is two integers W, and the H table represents the width and height of the image (3 <= W <= 1440,3 <= H <= 960)
In the subsequent row H, each row has W positive integers, indicating the pixel value of the point. (Pixels are between 0 and 255. 0 indicates black and 255 indicates white)
Output
Output the pixel values of each point in the image after the black area is black in the form of a matrix.
Sample Input
15 5100 253 214 146 120123 0 0 0 054 0 33 47 0255 0 0 78 014 11 0 0 0
Sample output
0 0 0 0 00 0 0 0 00 0 33 47 00 0 0 78 00 0 0 0 0
Source
[Zhang yuncong] original
Uploaded
Zhang yuncong

Problem solving: It's amazing that BFS can do this kind of thing ............. Outside the world ........

 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 long11 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\n",mp[i][j]);53         }54     }55     return 0;56 }
View code

Why do we need to add a circle of 1? Hey ..... Because the search starts from the upper left corner. If it is all 0, it may not be on the right .. Then the search will fail, resulting in wa ..... The ring is added to ensure that the outside of the Zero ring is a continuous one.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.