Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 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.                         map[i][j].color = proj[j] == -1 ? map[i][j - 1].color : map[i - 1][j].color;
  43.                         len++;
  44.                     } else {
  45.                         map[i][j].color = ++Pair.nextColor;
  46.                         canDown = false;
  47.                         len = 0;
  48.                     }
  49.                     canDown = (i < (map.length - 1) && (map[i + 1][j].type == type)) ? true : false;
  50.                     while (len >= 0) {
  51.                         proj[j - len] = canDown ? map[i][j].color : -1;
  52.                         len--;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     Main() {
  59.         printTypes(map);
  60.         System.out.println();
  61.         solve();
  62.         printColor(map);
  63.     }
  64.     public static void main(String[] args) { new Main(); }
  65.     void printTypes(Pair[][] array) {
  66.         for (Pair[] row : array) {
  67.             for (Pair somePair : row) {
  68.                 System.out.print(somePair.showType() + " ");
  69.             }
  70.             System.out.println();
  71.         }    
  72.     }
  73.     void printColor(Pair[][] array) {
  74.         for (Pair[] row : array) {
  75.             for (Pair somePair : row) {
  76.                 System.out.print(somePair.showColor() + " ");
  77.             }
  78.             System.out.println();
  79.         }    
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement