Advertisement
Guest User

Untitled

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