Advertisement
Go-Ice

NTHU Online Judge #10236

Nov 15th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. /**
  2.  * Name: NTHU Online Judge #10236 - Lab06
  3.  * @author LinChuWen
  4.  * Date: 2014.11.16
  5.  * NCHU EE, Taichung, Taiwan
  6.  *
  7.  * Problem: http://acm.cs.nthu.edu.tw/problem.php?pid=10236
  8.  * Description: Given a number N, we can create an N by N matrix as below:
  9.  *           1   2   3   4   5
  10.  *          16  17  18  19   6
  11.  *          15  24  25  20   7
  12.  *          14  23  22  21   8
  13.  *          13  12  11  10   9
  14.  *      In this case, N = 5, and the elements are in order from 1 to N^2 as a clockwise spiral pattern.
  15.  *      Note that we always start from the upper left corner.
  16.  */
  17. import java.util.Scanner;
  18.  
  19. public class NTHU_10236_Lab06 {
  20.     private static Scanner input = new Scanner(System.in);
  21.     private static int width;  
  22.    
  23.     public static void main(String[] args){
  24.         System.out.println("Creating an N by N matrix......");
  25.         System.out.print("Please input an integer N: ");
  26.         while(input.hasNext()){
  27.             width = input.nextInt();
  28.             int[][] Matrix = new int[width][width];
  29.            
  30.             if(width%2==0)
  31.                 printMatrix(flipMatrix(buildMatrix(Matrix)));
  32.             else printMatrix(buildMatrix(Matrix));
  33.         } //while end
  34.     } //main end
  35.    
  36.     private static int[][] buildMatrix( int[][] buildMatrixInput){
  37.         int fillTimes=width;
  38.         int cntFillColumn=0,cntFillRow=0;
  39.         int indexRow=0,indexColumn=0;
  40.         int startRow=0,startColumn=0;
  41.         int cntFlipTimes=0;
  42.         boolean goFlip=false;
  43.        
  44.         for(int countUp=1;countUp<=width*width;countUp++){
  45.            
  46.             if(goFlip){
  47.                 buildMatrixInput=flipMatrix(buildMatrixInput);
  48.                 cntFlipTimes++;
  49.                 fillTimes--;               
  50.                 goFlip=false;      
  51.                
  52.                 if(cntFlipTimes%2==0)
  53.                     startRow++;
  54.                 else startColumn++;
  55.                
  56.                 indexRow=startRow;
  57.                 indexColumn=startColumn;
  58.                 cntFillColumn=0;cntFillRow=0;
  59.             } //if(goFlip) end
  60.            
  61.             //fill content into Matrix
  62.             if(cntFillColumn<fillTimes){
  63.                 buildMatrixInput[indexRow][indexColumn]=countUp;
  64.                 indexColumn++;cntFillColumn++;
  65.             }else{
  66.                 indexRow++;cntFillRow++;
  67.                 buildMatrixInput[indexRow][indexColumn-1]=countUp;
  68.                
  69.                 if(cntFillRow==fillTimes-1)
  70.                     goFlip=true;
  71.             } //fill Matrix end
  72.            
  73.         } //big for end
  74.        
  75.         return buildMatrixInput;
  76.     } //buildMatrix() end
  77.    
  78.     private static int[][] flipMatrix( int[][] flipMatrixInput ){
  79.         int[][] arrayFlip = new int[width][width];
  80.         int cntDown_r,cntDown_c;
  81.         cntDown_r=width-1;
  82.         for(int cntUp_r=0;cntUp_r<width;cntUp_r++){
  83.             cntDown_c=width-1;
  84.             for(int cntUp_c=0;cntUp_c<width;cntUp_c++){
  85.                 arrayFlip[cntDown_r][cntDown_c] = flipMatrixInput[cntUp_r][cntUp_c];
  86.                 cntDown_c--;
  87.             } //small for end
  88.             cntDown_r--;
  89.         } //big for end
  90.         return arrayFlip;
  91.     } //flipMatrix() end
  92.    
  93.     private static void printMatrix( int[][] printMatrixInput){
  94.         int cnt1,cnt2;
  95.         for(cnt1=0;cnt1<width;cnt1++){
  96.             for(cnt2=0;cnt2<width;cnt2++){
  97.                 System.out.printf("%3d ", printMatrixInput[cnt1][cnt2]);
  98.             } //small for end
  99.             System.out.printf("\n");
  100.         } //big for end
  101.     } //printMatrix() end
  102.    
  103. } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement