Bomb man: Enumeration

Keywords: Java

Original

Everyone has played the game of bomb man. This time, we ask which open space we can put a bomb in to kill the most people.

Suppose that the bomb's power is infinite (as long as it doesn't meet the wall, the fire in four directions can extend infinitely).

Replace the wall with Chen, represent the enemy with G, and represent the open space with. Bombs can only be placed on the open space, so as long as they are in four directions

Scan how many enemies can be killed.

 1 import java.util.Scanner;
 2 
 3 public class bomb {
 4 
 5     public static void main(String[] args) {
 6         Scanner reader=new Scanner(System.in);
 7         int n=reader.nextInt();    //That's ok
 8         int m=reader.nextInt();    //column
 9         int q=0;
10         int p=0;
11         int max=0;
12         char array[][]=new char[n][m];
13         for(int i=0;i<=n-1;i++) {    //Array assignment
14             String ss=reader.next();
15             array[i]=ss.toCharArray();
16         }
17         for(int i=0;i<=n-1;i++) {
18             for(int j=0;j<=m-1;j++) {
19                 if(array[i][j]=='.') {    //Open space
20                     int num=0;    //The number of people who could be killed
21                     int x=i;
22                     int y=j;
23                     while(array[x][y]!='#') {    //Upward scan
24                         if(array[x][y]=='G') {
25                             num++;
26                         }
27                         x--;
28                     }
29                     x=i;
30                     y=j;
31                     while(array[x][y]!='#') {    //Downward scanning
32                         if(array[x][y]=='G') {
33                             num++;
34                         }
35                         x++;
36                     }
37                     x=i;
38                     y=j;
39                     while(array[x][y]!='#') {    //Left scan
40                         if(array[x][y]=='G') {
41                             num++;
42                         }
43                         y--;
44                     }
45                     x=i;
46                     y=j;
47                     while(array[x][y]!='#') {    //Scanning right
48                         if(array[x][y]=='G') {
49                             num++;
50                         }
51                         y++;
52                     }
53                     if(num>max) {
54                         max=num;
55                         q=i;
56                         p=j;
57                     }
58                 }
59             }
60         }
61         System.out.println("("+q+","+p+")"+" "+max);
62     }
63 }

Test example:

13 13
#############
#GG.GGG#GGG.#
###.#G#G#G#G#
#.......#..G#
#G#.###.#G#G#
#GG.GGG.#.GG#
#G#.#G#.#.###
##G...G.....#
#G#.#G###.#G#
#...G#GGG.GG#
#G#.#G#G#.#G#
#GG.GGG#G.GG#
#############

Answer: (9,9) 8 - that is, in coordinates (9,9), it can kill up to eight enemies

17:16:45

2018-07-14

Posted by metallord on Sat, 15 Feb 2020 11:39:27 -0800