woonie

Bomb.java

Feb 9th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * name      : woonie
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. class Grid {
  8.  
  9.     // declare the member field
  10.     private int x, y, bombRadius;
  11.     private int[][] buildingArray, bombArray, bombLocationArray, destroyedBuildings1;
  12.     // declare the constructor
  13.     public Grid(int row, int column){
  14.         //initialize
  15.         x = row;
  16.         y = column;
  17.         bombRadius = 0;
  18.         buildingArray = new int[x][y];
  19.         bombArray = new int[x][y];
  20.         bombLocationArray = new int[x][y];
  21.         destroyedBuildings1 = new int[x][y];
  22.         for (int i = 0; i < x; i++){
  23.             for (int j = 0; j < y; j++){
  24.                 buildingArray[i][j] = 0;
  25.                 bombArray[i][j] = 0;
  26.                 bombLocationArray[i][j] = 0;
  27.                 destroyedBuildings1[i][j] = 0;
  28.             }
  29.         }
  30.     }
  31.  
  32.     public void add(int row, int col){
  33.         buildingArray[row][col] = 1;
  34.     }
  35.  
  36.     /** addBomb   : a new bomb placed at cell (x, y) and radius r. Update the necessary information for each affected cell   */
  37.     public void addBomb(int a, int b) {
  38.         // implementation
  39.         bombLocationArray[a][b] = 1;
  40.     }
  41.  
  42.     public void setBombRadius(int r){
  43.         bombRadius = r;
  44.     }
  45.  
  46.     public void processBomb(){
  47.         int temp, temp1, temp2, temp3, temp4;
  48.         for (int p = 0; p < x; p++){
  49.             for (int q = 0; q < y; q++){
  50.                 if (bombLocationArray[p][q] == 1){
  51.                     for (int r = 0; r <= bombRadius; r++){
  52.                         for (int r2 = 0; r2 <= bombRadius; r2++){
  53.                             if (r == 0 && r2 == 0){
  54.                                 temp = bombArray[p][q];
  55.                                 bombArray[p][q] = temp + 1;
  56.                             } else if (r == 0){
  57.                                 if ((q - r2) >= 0){
  58.                                     temp = bombArray[p][q - r2];
  59.                                     bombArray[p][q - r2] = temp + 1;
  60.                                 }
  61.                                 if ((q + r2) <= (y - 1)){
  62.                                     temp2 = bombArray[p][q + r2];
  63.                                     bombArray[p][q + r2] = temp2 + 1;
  64.                                 }
  65.                             } else if (r2 == 0){
  66.                                 if ((p - 1) >= 0){
  67.                                     temp = bombArray[p - 1][q];
  68.                                     bombArray[p - 1][q] = temp + 1;
  69.                                 }
  70.                                 if ((p + 1) <= (x - 1)){
  71.                                     temp2 = bombArray[p + 1][q];
  72.                                     bombArray[p + 1][q] = temp2 + 1;
  73.                                 }
  74.                             } else
  75.                             if (((p - r) >= 0) && ((q - r2) >= 0)){
  76.                                 temp = bombArray[p - r][q - r2];
  77.                                 bombArray[p - r][q - r2] = temp + 1;
  78.                             }
  79.                             if (((p + r) <= (x - 1)) && ((q + r2) <= (y - 1))){
  80.                                 temp2 = bombArray[p + r][q + r2];
  81.                                 bombArray[p + r][q + r2] = temp2 + 1;
  82.                             }
  83.                             if (((p - r) >= 0) && ((q + r2) <= (y - 1))){
  84.                                 temp3 = bombArray[p - r][q + r2];
  85.                                 bombArray[p - r][q + r2] = temp3 + 1;
  86.                             }
  87.                             if (((p + r) <= (x - 1)) && ((q - r2) >= 0)){
  88.                                 temp4 = bombArray[p + r][q - r2];
  89.                                 bombArray[p + r][q - r2] = temp4 + 1;
  90.                             }
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     /**
  99.      *     getDestroyed   : to count the number of destroyed buildings
  100.      */
  101.     public int getDestroyed() {
  102.         // implementation
  103.         int count = 0;
  104.         for (int aa = 0; aa < x; aa++){
  105.             for (int bb = 0; bb < y; bb++){
  106.                 if (destroyedBuildings1[aa][bb] == 1){
  107.                     count = count + 1;
  108.                 }
  109.             }
  110.         }
  111.         return count;
  112.     }
  113.  
  114.     /**  count        : to count the number of bombs that affects cell (x, y) */
  115.     public int count(int x, int y) {
  116.         // implementation
  117.         return bombArray[x][y];
  118.     }
  119.  
  120.     public void createDestroyed(){
  121.         int count, building;
  122.         for (int f = 0; f < x; f++){
  123.             for (int g = 0; g < y; g++){
  124.                 count = count(f, g);
  125.                 building = buildingArray[f][g];
  126.                 if (building == 1 && count > 0){
  127.                     destroyedBuildings1[f][g] = 1;
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     /** getAdjacent : returns number of adjacent squares that has a building destroyed by bomb. **/
  134.     public int getAdjacent(int a, int b) {
  135.         int g1, g2, g3, g4, count;
  136.         g1 = 0;
  137.         g2 = 0;
  138.         g3 = 0;
  139.         g4 = 0;
  140.         if ((a + 2) <= x){
  141.             g1 = destroyedBuildings1[a + 1][b];
  142.         }
  143.         if ((b + 2) <= y){
  144.             g2 = destroyedBuildings1[a][b + 1];
  145.         }
  146.         if ((b - 1) >= 0){
  147.             g3 = destroyedBuildings1[a][b - 1];
  148.         }
  149.         if ((a - 1) >= 0){
  150.             g4 = destroyedBuildings1[a - 1][b];
  151.         }
  152.         count = g1 + g2 + g3 + g4;
  153.         return count;
  154.     }
  155.    
  156.     /** getPerimeter   : to determine the perimeter of the destroyed buildings */  
  157.     public int getPerimeter() {
  158.         // implementation
  159.         int perimeter, adjacent, numberOfDestroyedBuildings;
  160.         numberOfDestroyedBuildings = getDestroyed();
  161.         perimeter = numberOfDestroyedBuildings * 4;
  162.         for (int cc = 0; cc < x; cc++){
  163.             for (int dd = 0; dd < y; dd++){
  164.                 if (destroyedBuildings1[cc][dd] == 1){
  165.                     adjacent = getAdjacent(cc, dd);
  166.                     perimeter = perimeter - adjacent;
  167.                 }
  168.             }
  169.         }
  170.         return perimeter;
  171.     }
  172. }
  173.  
  174. class Bomb {
  175.  
  176.     public static void main(String[] args) {
  177.         // declare the necessary variables
  178.         int row, column, numberOfBombs, bombRadius, xCo, yCo;
  179.         String input;
  180.         // create new object from class Grid
  181.         Grid loopCity;
  182.         // declare a Scanner object to read input
  183.         Scanner myScanner = new Scanner(System.in);
  184.         // read input and process them accordingly
  185.         row = myScanner.nextInt();
  186.         column = myScanner.nextInt();
  187.         loopCity = new Grid(row, column);
  188.         for (int i = 0; i < row; i++){
  189.             for (int j = 0; j < column; j++){
  190.                 input = myScanner.next();
  191.                 if (input.equals("X")){
  192.                     loopCity.add(i, j);
  193.                 }
  194.             }
  195.         }
  196.         numberOfBombs = myScanner.nextInt();
  197.         bombRadius = myScanner.nextInt();
  198.         loopCity.setBombRadius(bombRadius);
  199.         for (int k = 0; k < numberOfBombs; k++){
  200.             xCo = myScanner.nextInt();
  201.             yCo = myScanner.nextInt();
  202.             loopCity.addBomb(xCo, yCo);
  203.         }
  204.         loopCity.processBomb();
  205.         loopCity.createDestroyed();
  206.         System.out.print(loopCity.getDestroyed() + " " + loopCity.getPerimeter());
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment