Advertisement
alefhidalgo

Robot

Jun 20th, 2011
1,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3. import java.util.TreeSet;
  4.  
  5. /**
  6.  * Tuenti Programming Contest
  7.  * Challenge 15: The Robot Known as Alfonso D. Artiste Jr.
  8.  * @author alefhidalgo [at] gmail [dot] com
  9.  */
  10. public class Robot {
  11.  
  12.     private int w; // wide
  13.     private int l; // long
  14.     private int[][] canvas;
  15.     private LinkedHashMap<Integer, ColorCount> colorsMap;
  16.  
  17.     /**
  18.      * Constructor 
  19.      * @param w
  20.      * @param l
  21.      * @param n
  22.      */
  23.     public Robot(int w, int l, int n) {
  24.         this.w = w;
  25.         this.l = l;
  26.         canvas = new int[l][w];
  27.         for (int i = 0; i < l; i++) {
  28.             for (int j = 0; j < w; j++) {
  29.                 canvas[i][j] = 1;
  30.             }
  31.         }
  32.         colorsMap = new LinkedHashMap<Integer, ColorCount>(n);
  33.     }
  34.  
  35.     /**
  36.      * Add rectangle to the cavas
  37.      *
  38.      * @param rectangle
  39.      */
  40.     public void addRectangle(Rectangle rectangle) {
  41.         for (int i = rectangle.ly; i <= rectangle.uy; i++) {
  42.             for (int j = rectangle.lx; j <= rectangle.ux; j++) {
  43.                 canvas[i][j] = rectangle.color;
  44.             }
  45.         }
  46.     }
  47.  
  48.     /**
  49.      * printColorsCount
  50.      */
  51.     public void printColorsCount() {
  52.         for (int i = 0; i < l; i++) {
  53.             for (int j = 0; j < w; j++) {
  54.                 int c = canvas[i][j];
  55.                 if (colorsMap.containsKey(c)) {
  56.                     colorsMap.get(c).increase();
  57.                 } else {
  58.                     colorsMap.put(c, new ColorCount(c));
  59.                 }
  60.             }
  61.         }
  62.  
  63.         StringBuilder sb = new StringBuilder();
  64.         // Order and print colors count
  65.         TreeSet<Integer> orderSet = new TreeSet<Integer>(colorsMap.keySet());
  66.         for (Integer i : orderSet) {
  67.             sb.append(colorsMap.get(i).color);
  68.             sb.append(" ");
  69.             sb.append(colorsMap.get(i).totalInCanvas);
  70.             sb.append(" ");
  71.         }
  72.         System.out.println(sb.toString().trim());
  73.     }
  74.    
  75.     /* PRINT - (FOR DEBUG PURPOSE)*/
  76.     public void print() {
  77.         for (int i = (l - 1); i >= 0; i--) {
  78.             for (int j = 0; j < w; j++) {
  79.                 System.out.print(canvas[i][j]);
  80.             }
  81.             System.out.println();
  82.         }
  83.     }
  84.  
  85.     /* Class Rectangle */
  86.     private static class Rectangle {
  87.         int lx;
  88.         int ly;
  89.         int ux;
  90.         int uy;
  91.         int color;
  92.  
  93.         public Rectangle(int lx, int ly, int ux, int uy, int color) {
  94.             this.lx = lx;
  95.             this.ly = ly;
  96.             this.ux = ux - 1;
  97.             this.uy = uy - 1;
  98.             this.color = color;
  99.         }
  100.     }
  101.  
  102.     /* ColorCount class */
  103.     private static class ColorCount {
  104.         int color;
  105.         int totalInCanvas;
  106.  
  107.         public ColorCount(int color) {
  108.             this.totalInCanvas = 1;
  109.             this.color = color;
  110.         }
  111.  
  112.         public void increase() {
  113.             totalInCanvas++;
  114.         }
  115.     }
  116.  
  117.     public static void main(String args[]) {
  118.         Scanner scanner = new Scanner(System.in);
  119.         Scanner lineScanner = null;
  120.         Robot robot = null;
  121.         int w, l, n, lx, ly, ux, uy, color;    
  122.         while (scanner.hasNextLine()) {
  123.             lineScanner = new Scanner(scanner.nextLine());
  124.             w = lineScanner.nextInt();
  125.             l = lineScanner.nextInt();
  126.             n = lineScanner.nextInt();
  127.             robot = new Robot(w, l, n);
  128.             for (int i = 0; i < n; i++) {
  129.                 lx = lineScanner.nextInt();
  130.                 ly = lineScanner.nextInt();
  131.                 ux = lineScanner.nextInt();
  132.                 uy = lineScanner.nextInt();
  133.                 color = lineScanner.nextInt();
  134.                 robot.addRectangle(new Rectangle(lx, ly, ux, uy, color));
  135.             }
  136.             robot.printColorsCount();
  137.         }
  138.  
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement