Cutout java version (timeout, seeking easier method)

Jun garlic when doing image processing project, encountered a problem. He needs the removal of the picture, a picture frame black line, and now invite you to help him complete this step, the entire area outside the frame black to black, leaving only the color that is black line box.

Jun garlic may also picking multiple frames, these frames do not appear adjacent to the intersection, containment relationship, because the choice of frame too much, so the king could put garlic in which at least a portion of the frame to draw a side, so this wireframe is invalid.

Known black dot in FIG addition line, there is no pure black image (i.e., the point 00 pixels).

Rectangular relationship Description:

Wherein the following data also belong to adjacent.

1
0 0 0 1 1 1
2
0 1 0 1 1 1
3
0 0 0 1 1 1
4
1 1 0 0 0 1
5
1 1 1 0 1 0
6
1 1 1 0 0 0
around that is 00 (eight direction), there will be another rectangle 00.

Input format
first line of input test data sets the number N (0 <N \ le 10 ) N (0 <N≤10).

A first data line of each test two integers H, WH, W represent a height and width of the picture (3 \ le H, W \ le 500) (3≤H, W≤500).

HH subsequent rows, each row having WW positive integer, represents the pixel value at that point. (Pixel values ​​are between 00 to 255255, 00 representing black, white represents 255,255) using an integer between each row '\ t' apart.

Output format
output in a matrix form, the first region other than black black boxes, and then outputs the pixel value of each point in the image.

Data limits
of 30% to 30% of the data, a width of a rectangular frame 11, and the frame is complete.

For the 60% to 60% of the data, the width of the frame is not fixed rectangular, and the frame is complete.

To 100% 100% of the data, there may be a plurality of frames (frame will not be subject to ensure adjacent, intersecting, comprising relations), the width of the frame is not fixed, there may be missing a frame portion wherein the edge (can this frame is considered invalid wireframe).

Sample explained
for the first set of data:

This is a complete rectangle, it will retain the color of the line of the frame, retain the lower (3,2), (3,3) (3,2), (3,3) pixel value.

For the second group of data:

Only a rectangle, this rectangle is not a complete rectangle whole, all is invalid frame, so there is no pixel values ​​are reserved.

For the third set of data:

A plurality of rectangles, and the rectangles are not satisfied adjacent, intersecting relation included.

Sample input copy
. 3
. 4. 5
. 1 0 0 0. 1
. 1 0 0. 1. 1
. 1 0 0. 1. 1
. 1 0 0 0. 1
. 5. 6
. 1. 1. 1. 1. 1. 1
. 1 0 0. 1. 1. 1
. 1 0 0. 1. 1. 1
. 1 0 0 0. 1. 1
. 1. 1. 1. 1. 1. 1
10 10
. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1
. 1 0 0 0 0 0. 1 0. 1 0
. 1 0 0 0 0 0. 1 0. 1 0
. 1 0 0. 1 0 0. 1 0 0 0
. 1 0 0 0 0 0. 1. 1. 1. 1
. 1 0 0 0 0 0. 1. 1. 1. 1
. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1
. 1. 1. 1 0 0 0. 1 0 0 0
. 1. 1. 1 0. 1 0. 1 0. 1 0
. 1. 1. 1. 1 0 0 0 0 0 0
sample copy output
0 0 0 0 0
0 0. 1 0 0
0 0. 1 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0

import java.util.*;
	
public class Main {

		static int w;
		static int h;
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int num=0;
		int n=cin.nextInt();
		for(int i=0;i<n;i++) {
			h = cin.nextInt();
			w = cin.nextInt();
			int [][]shu = new int[h][w];
			for(int j=0;j<h;j++) {
				for(int k=0;k<w;k++) {
					shu[j][k] = cin.nextInt();
				}
			}
			for(int j=0;j<h;j++) {
				for(int k=0;k<w;k++) {
					if(shu[j][k]>0&&(j==0||k==0||j==h-1||k==w-1))
					bfs(shu,j,k);
				}
			}
			for(int j=0;j<h;j++) {
				for(int k=0;k<w;k++) {
					System.out.print(shu[j][k]);
				}
				System.out.println();
			}
			
		}
	}
	public static void bfs(int [][]shu,int x,int y) {
		int dx,dy;
		int []xx = new int[]{1,-1,0,0,1,1,-1,-1};
		int []yy = new int[]{0,0,1,-1,1,-1,1,-1}; 
				shu[x][y]=0;
		for(int i=0;i<8;i++) {
				dx=x+xx[i];
				dy=y+yy[i];
				if(dx<0||dx>=h||dy<0||dy>=w)
					continue;
				if(shu[dx][dy]>0)
					bfs(shu,dx,dy);
				
		}
		return ;
					
	}
}

Released four original articles · won praise 1 · views 93

Guess you like

Origin blog.csdn.net/weixin_45795519/article/details/104101532
Recommended