Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * name : woonie
- */
- import java.util.*;
- class Grid {
- // declare the member field
- private int x, y, bombRadius;
- private int[][] buildingArray, bombArray, bombLocationArray, destroyedBuildings1;
- // declare the constructor
- public Grid(int row, int column){
- //initialize
- x = row;
- y = column;
- bombRadius = 0;
- buildingArray = new int[x][y];
- bombArray = new int[x][y];
- bombLocationArray = new int[x][y];
- destroyedBuildings1 = new int[x][y];
- for (int i = 0; i < x; i++){
- for (int j = 0; j < y; j++){
- buildingArray[i][j] = 0;
- bombArray[i][j] = 0;
- bombLocationArray[i][j] = 0;
- destroyedBuildings1[i][j] = 0;
- }
- }
- }
- public void add(int row, int col){
- buildingArray[row][col] = 1;
- }
- /** addBomb : a new bomb placed at cell (x, y) and radius r. Update the necessary information for each affected cell */
- public void addBomb(int a, int b) {
- // implementation
- bombLocationArray[a][b] = 1;
- }
- public void setBombRadius(int r){
- bombRadius = r;
- }
- public void processBomb(){
- int temp, temp1, temp2, temp3, temp4;
- for (int p = 0; p < x; p++){
- for (int q = 0; q < y; q++){
- if (bombLocationArray[p][q] == 1){
- for (int r = 0; r <= bombRadius; r++){
- for (int r2 = 0; r2 <= bombRadius; r2++){
- if (r == 0 && r2 == 0){
- temp = bombArray[p][q];
- bombArray[p][q] = temp + 1;
- } else if (r == 0){
- if ((q - r2) >= 0){
- temp = bombArray[p][q - r2];
- bombArray[p][q - r2] = temp + 1;
- }
- if ((q + r2) <= (y - 1)){
- temp2 = bombArray[p][q + r2];
- bombArray[p][q + r2] = temp2 + 1;
- }
- } else if (r2 == 0){
- if ((p - 1) >= 0){
- temp = bombArray[p - 1][q];
- bombArray[p - 1][q] = temp + 1;
- }
- if ((p + 1) <= (x - 1)){
- temp2 = bombArray[p + 1][q];
- bombArray[p + 1][q] = temp2 + 1;
- }
- } else
- if (((p - r) >= 0) && ((q - r2) >= 0)){
- temp = bombArray[p - r][q - r2];
- bombArray[p - r][q - r2] = temp + 1;
- }
- if (((p + r) <= (x - 1)) && ((q + r2) <= (y - 1))){
- temp2 = bombArray[p + r][q + r2];
- bombArray[p + r][q + r2] = temp2 + 1;
- }
- if (((p - r) >= 0) && ((q + r2) <= (y - 1))){
- temp3 = bombArray[p - r][q + r2];
- bombArray[p - r][q + r2] = temp3 + 1;
- }
- if (((p + r) <= (x - 1)) && ((q - r2) >= 0)){
- temp4 = bombArray[p + r][q - r2];
- bombArray[p + r][q - r2] = temp4 + 1;
- }
- }
- }
- }
- }
- }
- }
- /**
- * getDestroyed : to count the number of destroyed buildings
- */
- public int getDestroyed() {
- // implementation
- int count = 0;
- for (int aa = 0; aa < x; aa++){
- for (int bb = 0; bb < y; bb++){
- if (destroyedBuildings1[aa][bb] == 1){
- count = count + 1;
- }
- }
- }
- return count;
- }
- /** count : to count the number of bombs that affects cell (x, y) */
- public int count(int x, int y) {
- // implementation
- return bombArray[x][y];
- }
- public void createDestroyed(){
- int count, building;
- for (int f = 0; f < x; f++){
- for (int g = 0; g < y; g++){
- count = count(f, g);
- building = buildingArray[f][g];
- if (building == 1 && count > 0){
- destroyedBuildings1[f][g] = 1;
- }
- }
- }
- }
- /** getAdjacent : returns number of adjacent squares that has a building destroyed by bomb. **/
- public int getAdjacent(int a, int b) {
- int g1, g2, g3, g4, count;
- g1 = 0;
- g2 = 0;
- g3 = 0;
- g4 = 0;
- if ((a + 2) <= x){
- g1 = destroyedBuildings1[a + 1][b];
- }
- if ((b + 2) <= y){
- g2 = destroyedBuildings1[a][b + 1];
- }
- if ((b - 1) >= 0){
- g3 = destroyedBuildings1[a][b - 1];
- }
- if ((a - 1) >= 0){
- g4 = destroyedBuildings1[a - 1][b];
- }
- count = g1 + g2 + g3 + g4;
- return count;
- }
- /** getPerimeter : to determine the perimeter of the destroyed buildings */
- public int getPerimeter() {
- // implementation
- int perimeter, adjacent, numberOfDestroyedBuildings;
- numberOfDestroyedBuildings = getDestroyed();
- perimeter = numberOfDestroyedBuildings * 4;
- for (int cc = 0; cc < x; cc++){
- for (int dd = 0; dd < y; dd++){
- if (destroyedBuildings1[cc][dd] == 1){
- adjacent = getAdjacent(cc, dd);
- perimeter = perimeter - adjacent;
- }
- }
- }
- return perimeter;
- }
- }
- class Bomb {
- public static void main(String[] args) {
- // declare the necessary variables
- int row, column, numberOfBombs, bombRadius, xCo, yCo;
- String input;
- // create new object from class Grid
- Grid loopCity;
- // declare a Scanner object to read input
- Scanner myScanner = new Scanner(System.in);
- // read input and process them accordingly
- row = myScanner.nextInt();
- column = myScanner.nextInt();
- loopCity = new Grid(row, column);
- for (int i = 0; i < row; i++){
- for (int j = 0; j < column; j++){
- input = myScanner.next();
- if (input.equals("X")){
- loopCity.add(i, j);
- }
- }
- }
- numberOfBombs = myScanner.nextInt();
- bombRadius = myScanner.nextInt();
- loopCity.setBombRadius(bombRadius);
- for (int k = 0; k < numberOfBombs; k++){
- xCo = myScanner.nextInt();
- yCo = myScanner.nextInt();
- loopCity.addBomb(xCo, yCo);
- }
- loopCity.processBomb();
- loopCity.createDestroyed();
- System.out.print(loopCity.getDestroyed() + " " + loopCity.getPerimeter());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment