螺旋矩陣是一個短陣,其中每個cell都填滿整數,且每個整數以螺旋的方式排列。
下圖為二個size = 8的螺旋矩陣。圖(a),數字由1開始,以逆時鐘的方向,螺旋狀地往中心排列。
而圖(b)則是一個順時鐘的螺旋矩陣,和圖(a)不一樣之處在於其數字的旋轉方向為順時鐘旋轉。
輸入說明:
輸入為一行字串,包含了兩個數值,每個數值以逗點隔開。
第一個數值為size,其值介於1到30間(i.e., 1 <= size <= 30)。
第二個數值為方向,1為順時鐘方向,2為逆時鐘方向。
輸出說明:
將螺旋矩陣輸出。每個數字的寬度為3,不足寬度之處補0(也就是說,數字1要印輸出成001,數字12要輸出成012…依此類推)。每個數字後要加一個逗點,數字間不要有空白。每一row的最後一個數字之後不要加逗點。
import java.util.*; import java.util.*; public class problem_two { public static void main(String args[]) { Scanner in = new Scanner(System.in); String input = in.nextLine(); String[] total = input.split(","); int Mr = Integer.parseInt(total[0]); // Mr*Mr int m = Integer.parseInt(total[1]); int[][] array = new int[Mr][Mr]; int x = Mr, y = Mr; int number = 1; int X_MIN = 0, X_MAX = Mr-1, Y_MIN = 0, Y_MAX = Mr-1; int count = 1; if(m == 1){ while(true) { count++; for(int i=Y_MIN;i<=Y_MAX;i++){ array[X_MIN][i] = number++; if(number > Mr*Mr){ //System.out.println("111"); break; } //System.out.println(number/* + "Y_MAX " + Y_MAX + "i " + i*/); } X_MIN++; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=X_MIN;i<=X_MAX;i++){ array[i][Y_MAX] = number++;//System.out.println(number/* + "X_MAX " + X_MAX + "i " + i*/); if(number > Mr*Mr){ //System.out.println("111"); break; } } Y_MAX--; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=Y_MAX;i>=Y_MIN;i--){ array[X_MAX][i] = number++;//System.out.println(number + "Y_MIN " + Y_MIN + "i " + i); if(number > Mr*Mr){ //System.out.println("111"); break; } } X_MAX--; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=X_MAX;i>=X_MIN;i--){ array[i][Y_MIN] = number++;//System.out.println(number + "X_MIN " + X_MIN + "i " + i); if(number > Mr*Mr){ //System.out.println("111"); break; } } Y_MIN++; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } } }else{ while(true) { for(int i=X_MIN;i<=X_MAX;i++){ array[i][Y_MIN] = number++;//System.out.println(number/* + "X_MAX " + X_MAX + "i " + i*/); if(number > Mr*Mr){ //System.out.println("111"); break; } } Y_MIN++; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=Y_MIN;i<=Y_MAX;i++){ array[X_MAX][i] = number++; if(number > Mr*Mr){ //System.out.println("111"); break; } //System.out.println(number/* + "Y_MAX " + Y_MAX + "i " + i*/); } X_MAX--; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=X_MAX;i>=X_MIN;i--){ array[i][Y_MAX] = number++;//System.out.println(number + "X_MIN " + X_MIN + "i " + i); if(number > Mr*Mr){ //System.out.println("111"); break; } } Y_MAX--; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } for(int i=Y_MAX;i>=Y_MIN;i--){ array[X_MIN][i] = number++;//System.out.println(number + "Y_MIN " + Y_MIN + "i " + i); if(number > Mr*Mr){ //System.out.println("111"); break; } } X_MIN++; //System.out.println(X_MIN + "," + X_MAX+ "," + Y_MIN+ "," + Y_MAX); if(number > Mr*Mr){ //System.out.println("111"); break; } } } for(int i = 0;i<Mr;i++){ for(int j=0;j<Mr;j++){ if(j != Mr-1){ System.out.printf("%03d,", array[i][j]); }else{ System.out.printf("%03d", array[i][j]); } } System.out.println(); } } }
沒有留言:
張貼留言