(JAVA) Z字形扫描 - 201412-2

问题描述

试题编号: 201412-2

试题名称: Z字形扫描

时间限制: 2.0s

内存限制: 256.0MB

问题描述:

问题描述

  在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan)。给定一个n×n的矩阵,Z字形扫描的过程如下图所示:

                                                                                        《(JAVA) Z字形扫描 - 201412-2》

  对于下面的4×4的矩阵,

  1 5 3 9

  3 7 5 6

  9 4 6 4

  7 3 1 3

  对其进行Z字形扫描后得到长度为16的序列:

  1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3

  请实现一个Z字形扫描的程序,给定一个n×n的矩阵,输出对这个矩阵进行Z字形扫描的结果。

输入格式

  输入的第一行包含一个整数n,表示矩阵的大小。

  输入的第二行到第n+1行每行包含n个正整数,由空格分隔,表示给定的矩阵。

输出格式

  输出一行,包含n×n个整数,由空格分隔,表示输入的矩阵经过Z字形扫描后的结果。

样例输入

4

1 5 3 9

3 7 5 6

9 4 6 4

7 3 1 3

样例输出

1 5 3 9 7 3 9 5 4 7 3 6 6 4 1 3

评测用例规模与约定

  1≤n≤500,矩阵元素为不超过1000的正整数。

代码:100分答案

     做法一:(第二次做答案)
     思路:一共四个方向(右,下,左下,右上),列出对这四个方向的后续步骤的处理。
              如,右的下一步有(1,到达右上角。2,左下。3,右上。4,到达右下角)


import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] arr = new int[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				arr[i][j] = sc.nextInt();
			}
		}
		
		System.out.print(arr[0][0]+" ");
 
		if(n > 1){
			// 0右   1下  2左下  3右上 
			int direction  = 0;
			int i = 0; //行
			int j = 0; //列
			while(direction != -1){
				switch(direction){
					case 0:
						//向右
						j++;
						System.out.print(arr[i][j]+" ");
						if(i == 0 && j == n-1){
							//右上角 - 第一行的最后一个
							direction = 1;
							if(n%2 == 0){
								// 特殊情况:偶矩阵 左下	
								direction = 2;
							}
						}
						else if(i == 0 && j != n-1){
							//左下
							direction = 2;
						}else if(i== n-1 && j>0 && j<n-1){
							//右上
							direction = 3;
						}else if(i == n-1 && j == n-1){
							//右下角 - 最后一行的最后一个
							direction = -1;
						}
						break;
					case 1:
						//向下
						i++;
						System.out.print(arr[i][j]+" ");
						if(j==0){
							//左边 - 左下角
							direction = 3;						
						}else if(j==n-1){
							//最右边
							direction = 2;
						} 
						break;
					case 2:
						//左下
						i++;
						j--;
						System.out.print(arr[i][j]+" ");
						if(i > 0 && i < n-1 && j > 0&& j < n-1){
							//继续左下
							direction = 2;
						}else if(j < n-1 && i==n-1){
							//左下角 - 向右
							direction = 0;
						}else if(j==0 && i>0 && i < n-1){
							//向下
							direction = 1;
						} 
						break;
					case 3:
						//右上
						i--;
						j++;
						System.out.print(arr[i][j]+" ");
						if(i > 0 && i < n-1 && j > 0&& j < n-1){
							//继续右上
							direction = 3;
						}else if( i < n-1 && j==n-1){
							//右上角  右边- 向下
							direction = 1;
						}else if(i==0 && j<n-1 && j>0){
							//向右
							direction = 0;
						} 
						
						break;				
	 
				}
	 
			}			
		}
 
	}
}

———————–  分界线 ———————————– 
  
做法二:(第一次做答案)
       思路:先输出最开始的两个数字。
                 输出的两个主要走向为:左下 和 右上,再根据具体情况,判断左下的下一步,右上的下一步。
                 以左下角为中介点,左下角之前,“左下”的下一步是“向下”;左下角之后,“左下”的下一步是“向右”;偶数到了左下角,方向向右,奇数到了左下角右上
 以右上角为中    介点,右上角之前,“右上”的下一步是“向右”;
右上角之后,“右上”的下一步是“向下”;偶数到了右上角,方向左下,奇数到了右上角向下

import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
 
		int[][] arr = new int[n][n];
		for(int i =0;i<n;i++){
			for(int j=0;j<n;j++){
				arr[i][j] = sc.nextInt();
			}
		}
		
		int left = 0;
		int right = 0;
 		System.out.print(arr[left][right]+" ");
 		if(n>1){ 
 			right ++;
			System.out.print(arr[left][right]+" "); 
 		}
		for(int i = 0;i < arr.length;i++){	
			//方向 : 左下
			if(left < right && right >0){
				//左比右小,左++,右--
 
				left++;
				right--;
				//左下的  下一步:向下 还是向右
				if(right ==0 && left < (n-1)){
					//向下
					System.out.print(arr[left][right]+" "+arr[++left][right]+" ");
				}else if(left==(n-1) && right!=0){
					//向右
					System.out.print(arr[left][right]+" "+arr[left][++right]+" ");
				} else{
					  System.out.print(arr[left][right]+" ");
						  while(right!=0 && left>0 &&left <(n-1)){
							  System.out.print(arr[++left][--right]+" ");
						  }
							//没到左下角,向下
							if(right ==0 && left <(n-1)&&left>0){
								System.out.print(arr[++left][right]+" ");
							}
							
							//到左下角  ,偶数向右,跳过此处,奇数右上
							if(n%2 ==0 && right >=0 && right<(n-1) && left == (n-1)){
								System.out.print(arr[left][++right]+" ");
							}	
							if(n%2 !=0 && right >0 && right<(n-1) && left == (n-1)){
								System.out.print(arr[left][++right]+" ");
							}
				 }

			} 
			
			//方向:右上
			if(right < left && left >0){
				//右比左小,右++,左--
				left--;
				right++;
				//右上的下一步:向右 或向下
				if(left ==0 && right < (n-1)){
					//向右
					System.out.print(arr[left][right]+" "+arr[left][++right]+" ");
				}else if( right ==(n-1) && left != 0 ){
					//向下
					System.out.print(arr[left][right]+" "+arr[++left][right]+" ");
				}else{ 
					 System.out.print(arr[left][right]+" ");
					 while(left!=0 && right>0 &&right<(n-1)){
						 System.out.print(arr[--left][++right]+" "); 
					 }
						//右上角之前,向右
						if(left ==0 && right < (n-1)){
							//向右
						 System.out.print(arr[left][++right]+" ");
						}
						
						//右上角之后,偶数左下,奇数向下
						if(n%2 == 0 && right ==(n-1) && left > 0 && left<(n-1) ){
							//向下
							System.out.print(arr[++left][right]+" ");
						}
						if( n%2 !=0 && right ==(n-1) && left >= 0 && left<(n-1) ){
							//向下
							System.out.print(arr[++left][right]+" ");
						}
					 
			 	  }
				 
			}
 
		}
 
	}

}


    原文作者:Z字形编排问题
    原文地址: https://blog.csdn.net/u010707315/article/details/72801440
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞