Advertisement
Dhaval2404

SpericalCubePrint

Aug 17th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. package com.image.main;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public class SpericalCubePrint{
  6.  
  7.     public static void main(String[] args) {
  8.         int i_length =3;
  9.                 int j_length =6;
  10.                
  11.                 int[][] array = new int[i_length][j_length];
  12.                 int count = 11;
  13.                 for (int i = 0; i < i_length; i++) {
  14.                         for (int j = 0; j < j_length; j++) {
  15.                                 array[i][j] = count;
  16.                                 count++;
  17.                         }
  18.                 }
  19.  
  20.                 for (int i = 0; i < i_length; i++) {
  21.                         for (int j = 0; j < j_length; j++) {
  22.                                 System.out.print(array[i][j] + " ");
  23.                         }
  24.                         System.out.println();
  25.                 }
  26.                
  27.                 int counter = 0;
  28.                 int i = 0, j = 0;
  29.                 int n = 0;
  30.                 int c = 1;
  31.                 int d = 1;
  32.                
  33.                 LinkedList<String> ar= new LinkedList<String>();
  34.                 while (counter < (i_length) * (j_length)) {
  35.                         switch (n % 4) {
  36.                         case 0:
  37.                            //     System.out.print("1:Array["+i+"]["+j+"]>>");
  38.                             System.out.print(array[i][j]+" ");
  39.                                    ar.add(i+""+j);
  40.                                 if (j >= (j_length - 2)) {
  41.                                         n++;
  42.                                 }
  43.                                 j++;
  44.                                 break;
  45.                         case 1:
  46.                             //    System.out.print("2:Array["+i+"]["+j+"]>>");
  47.                              System.out.print(array[i][j]+" ");
  48.                                    ar.add(i+""+j);
  49.                                 if (i >= (i_length - 2)) {
  50.                                         n++;
  51.                                 }  
  52.                                 i++;
  53.                                 break;
  54.                         case 2:
  55.                            //     System.out.print("3:Array["+i+"]["+j+"]>>");
  56.                                 System.out.print(array[i][j]+" ");
  57.                                 ar.add(i+""+j);
  58.                                 if (j <= c) {
  59.                                         n++;
  60.                                         c++;
  61.                                 }
  62.                                 j--;
  63.                                 break;
  64.                         case 3:
  65.                         //        System.out.print("4:Array["+i+"]["+j+"]>>");
  66.                                 System.out.print(array[i][j]+" ");
  67.                                 ar.add(i+""+j);
  68.                                 if (i <= d) {
  69.                                         n++;
  70.                                         d++;
  71.                                 }
  72.                                 i--;
  73.                                 break;
  74.                         }
  75.                        String ij = ar.getLast();
  76.                         if(ij!=null){
  77.                                 int i_last = Integer.parseInt(ij.substring(0, 1));
  78.                                 int j_last = Integer.parseInt(ij.substring(1, 2));
  79.                                 if(ar.contains(i+""+j)){
  80.                                         ar.add(i+""+j);
  81.                                         if(j_last==j && i<i_last){
  82.                                              i++;
  83.                                              j++;
  84.                                         } else if(i_last==i && j_last<j){
  85.                                              i++;
  86.                                              j--;
  87.                                         } else if(j_last==j && i>i_last){
  88.                                              i--;
  89.                                              j--;
  90.                                         } else if(i_last==i && j_last>j){
  91.                                              i--;  
  92.                                              j++;
  93.                                         }
  94.                                 }
  95.                         }
  96.                         counter++;
  97.                 }
  98.                 System.out.println();
  99.                 System.out.println("Done");
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement