Blue Bridge Cup Fundamentals Practice - Taking Numbers

Problem Description
  The trapezoidal number is to take the number along the edge of the matrix. If there are countless options in the current direction or have been taken, then turn left 90 degrees. It starts at the upper left corner of the matrix and goes down.
input format
  The first line of input is two positive integers m, n not exceeding 200, representing the rows and columns of the matrix. The next m lines contain n integers each, representing this matrix.
output format
  The output has only one line, a total of mn, which is the result of taking the shape of the input matrix. Separate numbers with a space, and do not have extra spaces at the end of the line.
sample input
3 3
1 2 3
4 5 6
7 8 9
Sample output
1 4 7 8 9 6 3 2 5
sample input
3 2
1 2
3 4
5 6
Sample output
1 3 5 6 4 2

Idea: The order of taking the numbers in a loop is a downward, right, upper, and left loop, and each time you reach the end, you need to judge and then correct the direction, so I took the form of creating an additional circle of arrays, that is, in the original storage of the array. There is a layer of elements with a value of -1 around it, so that each time the number is fetched, the direction is changed when it encounters -1, so as to realize the fetching of the number in the form of a loop. For details, see the comments in the code.

Code:

public class test {
	public static void main(String[] args) {
                Scanner input = new Scanner(System.in);
		int m = input.nextInt();//行
		int n = input.nextInt();//列
		
		//In order to mark, I will add a circle around the original n×m array and assign it to -1
		final int x = m + 2;//rows of the array
		final int y = n + 2;//Columns of the array
		int[][] mn = new int[x][y];
		
		//Initialize the array, store its own array in the range from mn[1][1] to mn[m][n]
		for(int i = 0 ; i < x ; i++ ) {
			for(int j = 0 ; j < y ; j++	) {
				if( i == 0 || i == x - 1 ) {
					mn [i] [j] = -1;
				}
				else {
					if(j == 0 || j == y - 1)
						mn [i] [j] = -1;
					else {
						mn[i][j] = input.nextInt();
					}
				}
			}
		}
		
		int a = 1 ;
		int b = 1 ;
		int count = 1;//Initialize the tokenizer
		
		while(true) {
			//count=1 means fetch down, 2 means right, 3 is up, 4 is left
			// Move one in the corresponding direction after taking it, and assign the value that has been printed to -1
			//If a column is taken to the end, process the count and let the count change direction
			if(count % 4 == 1) {
				
				//Print
				while(mn[a][b] != -1) {
					System.out.print(mn[a][b] + " ");
					mn [a] [b] = -1;
					a++;
				}
				//When -1 is encountered, it has moved to -1 on the boundary, and the coordinates of --a and b are needed to process count
				//Because after fetching down, it is fetching to the right, so after processing count, you need to skip b++ to the right element
				if(mn[a][b] == -1) {
					count = dealCount(mn ,--a ,b++ );
					continue;
				}
				
			}
			
			if(count % 4 == 2) {
				
				while(mn[a][b] != -1) {
					System.out.print(mn[a][b] + " ");
					mn [a] [b] = -1;
					b++;
				}
				if(mn[a][b] == -1) {
					count = dealCount(mn ,a-- ,--b );//Jump to the above element
					continue;
				}
				
			}
			
			if(count % 4 == 3) {
				
				while(mn[a][b] != -1) {
					System.out.print(mn[a][b] + " ");
					mn [a] [b] = -1;
					a--;
				}
				if(mn[a][b] == -1) {
					count = dealCount(mn ,++a ,b-- );//Jump to the left element
					continue;
				}
				
			}
			
			if(count % 4 == 0) {
				
				while(mn[a][b] != -1) {
					System.out.print(mn[a][b] + " ");
					mn [a] [b] = -1;
					b--;
				}
				if(mn[a][b] == -1) {
					count = dealCount(mn ,a++ ,++b );//Jump to the element below
					continue;
				}
				
			}
			if(count == -2)break;//count returns -2 to get out of the loop
		}	
	}
	public static int dealCount(int[][] arr,int x ,int y) {
		int count = -2;
		//If there are no numbers that are not -1 around, the proof is complete, return -2 to identify
		if(arr[x + 1][y] != -1)
			count = 1;
		if(arr[x][y + 1] != -1)
			count = 2;
		if(arr[x - 1][y] != -1)
			count = 3;
		if(arr[x][y - 1] != -1)
			count = 0;
		return count;
		
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325591683&siteId=291194637
Recommended