woonie

Bomb.java

Feb 9th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Grid {
  4.  
  5.     // declare the member field
  6.     private int x, y, bombRadius;
  7.     private int[][] buildingArray, bombArray, bombLocationArray, destroyedBuildings1;
  8.     // declare the constructor
  9.     public Grid(int row, int column){
  10.         //initialize
  11.         x = row;
  12.         y = column;
  13.         bombRadius = 0;
  14.         buildingArray = new int[x][y];
  15.         bombArray = new int[x][y];
  16.         bombLocationArray = new int[x][y];
  17.         destroyedBuildings1 = new int[x][y];
  18.         for (int i = 0; i < x; i++){
  19.             for (int j = 0; j < y; j++){
  20.                 buildingArray[i][j] = 0;
  21.                 bombArray[i][j] = 0;
  22.                 bombLocationArray[i][j] = 0;
  23.                 destroyedBuildings1[i][j] = 0;
  24.             }
  25.         }
  26.     }
  27.  
  28.     public void add(int row, int col){
  29.         buildingArray[row][col] = 1;
  30.     }
  31.  
  32.     /**
  33.      *     addBomb   : a new bomb placed at cell (x, y) and radius r. Update the necessary information for
  34.      *                     each affected cell
  35.      *     Pre-condition :
  36.      *     Post-condition:
  37.      */
  38.     public void addBomb(int a, int b) {
  39.         // implementation
  40.         bombLocationArray[a][b] = 1;
  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.     /**
  100.      *     getDestroyed   : to count the number of destroyed buildings
  101.      *     Pre-condition  :
  102.      *     Post-condition :
  103.      */
  104.     public int getDestroyed() {
  105.         // implementation
  106.         int count = 0;
  107.         for (int aa = 0; aa < x; aa++){
  108.             for (int bb = 0; bb < y; bb++){
  109.                 if (destroyedBuildings1[aa][bb] == 1){
  110.                     count = count + 1;
  111.                 }
  112.             }
  113.         }
  114.         return count;
  115.  
  116.     }
  117.  
  118.    
  119.     /**
  120.      *     count          : to count the number of bombs that affects cell (x, y)
  121.      *     Pre-condition  :
  122.      *     Post-condition :
  123.      */
  124.     public int count(int x, int y) {
  125.         // implementation
  126.         return bombArray[x][y];
  127.     }
  128.  
  129.     public void createDestroyed(){
  130.         int count, building;
  131.         for (int f = 0; f < x; f++){
  132.             for (int g = 0; g < y; g++){
  133.                 count = count(f, g);
  134.                 building = buildingArray[f][g];
  135.                 if (building == 1 && count > 0){
  136.                     destroyedBuildings1[f][g] = 1;
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.  
  143.     /** getAdjacent : returns number of adjacent squares that has a building destroyed by bomb. */
  144.     public int getAdjacent(int a, int b) {
  145.         int g1, g2, g3, g4, count;
  146.         g1 = 0;
  147.         g2 = 0;
  148.         g3 = 0;
  149.         g4 = 0;
  150.         if ((a + 2) <= x){
  151.             g1 = destroyedBuildings1[a + 1][b];
  152.         }
  153.         if ((b + 2) <= y){
  154.             g2 = destroyedBuildings1[a][b + 1];
  155.         }
  156.         if ((b - 1) >= 0){
  157.             g3 = destroyedBuildings1[a][b - 1];
  158.         }
  159.         if ((a - 1) >= 0){
  160.             g4 = destroyedBuildings1[a - 1][b];
  161.         }
  162.         count = g1 + g2 + g3 + g4;
  163.         return count;
  164.     }
  165.    
  166.     /**
  167.      *     getPerimeter   : to determine the perimeter of the destroyed buildings
  168.      *     Pre-condition  :
  169.      *     Post-condition :
  170.      */
  171.     public int getPerimeter() {
  172.         // implementation
  173.         int perimeter, adjacent, numberOfDestroyedBuildings;
  174.         numberOfDestroyedBuildings = getDestroyed();
  175.         perimeter = numberOfDestroyedBuildings * 4;
  176.         for (int cc = 0; cc < x; cc++){
  177.             for (int dd = 0; dd < y; dd++){
  178.                 if (destroyedBuildings1[cc][dd] == 1){
  179.                     adjacent = getAdjacent(cc, dd);
  180.                     perimeter = perimeter - adjacent;
  181.                 }
  182.             }
  183.         }
  184.  
  185.         return perimeter;
  186.     }
  187.  
  188. }
  189.  
  190.  
  191. class Bomb {
  192.  
  193.     public static void main(String[] args) {
  194.  
  195.         // declare the necessary variables
  196.         int row, column, numberOfBombs, bombRadius, xCo, yCo;
  197.         String input;
  198.         // create new object from class Grid
  199.         Grid loopCity;
  200.         // declare a Scanner object to read input
  201.         Scanner myScanner = new Scanner(System.in);
  202.         // read input and process them accordingly
  203.         row = myScanner.nextInt();
  204.         column = myScanner.nextInt();
  205.         loopCity = new Grid(row, column);
  206.         for (int i = 0; i < row; i++){
  207.             for (int j = 0; j < column; j++){
  208.                 input = myScanner.next();
  209.                 if (input.equals("X")){
  210.                     loopCity.add(i, j);
  211.                 }
  212.             }
  213.         }
  214.         numberOfBombs = myScanner.nextInt();
  215.         bombRadius = myScanner.nextInt();
  216.         loopCity.setBombRadius(bombRadius);
  217.         for (int k = 0; k < numberOfBombs; k++){
  218.             xCo = myScanner.nextInt();
  219.             yCo = myScanner.nextInt();
  220.             loopCity.addBomb(xCo, yCo);
  221.         }
  222.         loopCity.processBomb();
  223.         loopCity.createDestroyed();
  224.         System.out.print(loopCity.getDestroyed() + " " + loopCity.getPerimeter());
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment