Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package test1;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public final class Main {
  6.     static Pair[][] map = new Pair[7][5];
  7.     static {
  8.         map[0][0] = new Pair(0,-1);  map[0][1] = new Pair(0,-1);  map[0][2] = new Pair(1,-1);  map[0][3] = new Pair(1,-1);  map[0][4] = new Pair(2,-1);
  9.         map[1][0] = new Pair(0,-1);  map[1][1] = new Pair(0,-1);  map[1][2] = new Pair(1,-1);  map[1][3] = new Pair(0,-1);  map[1][4] = new Pair(0,-1);
  10.         map[2][0] = new Pair(2,-1);  map[2][1] = new Pair(1,-1);  map[2][2] = new Pair(1,-1);  map[2][3] = new Pair(2,-1);  map[2][4] = new Pair(0,-1);
  11.         map[3][0] = new Pair(1,-1);  map[3][1] = new Pair(1,-1);  map[3][2] = new Pair(1,-1);  map[3][3] = new Pair(0,-1);  map[3][4] = new Pair(0,-1);
  12.         map[4][0] = new Pair(2,-1);  map[4][1] = new Pair(1,-1);  map[4][2] = new Pair(1,-1);  map[4][3] = new Pair(2,-1);  map[4][4] = new Pair(1,-1);
  13.         map[5][0] = new Pair(0,-1);  map[5][1] = new Pair(0,-1);  map[5][2] = new Pair(1,-1);  map[5][3] = new Pair(1,-1);  map[5][4] = new Pair(1,-1);
  14.         map[6][0] = new Pair(0,-1);  map[6][1] = new Pair(0,-1);  map[6][2] = new Pair(2,-1);  map[6][3] = new Pair(2,-1);  map[6][4] = new Pair(2,-1);
  15.     }
  16.     static class Pair {
  17.         public int type;
  18.         public int color;
  19.         public static int nextColor = 10;
  20.        
  21.         public Pair(int type, int color) {
  22.             this.type = type;
  23.             this.color = color;
  24.         }
  25.        
  26.         public String showType() {
  27.             return this.type+"";
  28.         }
  29.         public String showColor() {
  30.             return this.color+"";
  31.         }
  32.     }
  33.     void solve() {
  34.         int[] proj = new int[map[0].length];
  35.         Arrays.fill(proj, -1); int len = 0;
  36.         for (int i = 0; i < map.length; i++) {
  37.             boolean canDown = false;
  38.             for (int j = 0; j < map[0].length; j++) {
  39.                 int type = map[i][j].type;
  40.                 if (type != 0) {
  41.                     if (j > 0 && map[i][j - 1].type == type) {
  42.                         if (proj[j] == -1) {
  43.                             map[i][j].color = map[i][j - 1].color;
  44.                         } else {
  45.                             map[i][j].color = map[i - 1][j].color;
  46.                         }
  47.                         len++;
  48.                     } else {
  49.                         map[i][j].color = ++Pair.nextColor;
  50.                         canDown = false;
  51.                         len = 0;
  52.                     }
  53.                     canDown = (i < (map.length - 1) && (map[i + 1][j].type == type)) ? true : false;
  54.                     if (canDown) {
  55.                         while (len >= 0) {
  56.                             proj[j - len] = map[i][j].color;
  57.                             len--;
  58.                         }
  59.                     } else {
  60.                         while (len >= 0) {
  61.                             proj[j - len] = -1;
  62.                             len--;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69.     Main() {
  70.         printTypes(map);
  71.         System.out.println();
  72.         solve();
  73.         printColor(map);
  74.     }
  75.     public static void main(String[] args) { new Main(); }
  76.     void printTypes(Pair[][] array) {
  77.         for (Pair[] row : array) {
  78.             for (Pair somePair : row) {
  79.                 System.out.print(somePair.showType() + " ");
  80.             }
  81.             System.out.println();
  82.         }    
  83.     }
  84.     void printColor(Pair[][] array) {
  85.         for (Pair[] row : array) {
  86.             for (Pair somePair : row) {
  87.                 System.out.print(somePair.showColor() + " ");
  88.             }
  89.             System.out.println();
  90.         }    
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement