Original
Before using enumeration method to solve bomb man, please refer to my previous blog for details: https://www.cnblogs.com/chiweiming/p/9295262.html
The enumeration method ignores the map layout. The enumeration method directly searches the map globally, and counts the number of enemies from four directions as soon as it meets the open space, but some open spaces
Surrounded by the enemy, the bomber can't go in, and if the points surrounded by the enemy can kill the most points of the enemy, the result will be wrong, so
First use the breadth first search (DFS can also be) to find out which point bombers can reach, and then count the number of deadly enemies on the basis of the reachable points.
If you are not familiar with BFS algorithm, please see my blog: https://www.cnblogs.com/chiweiming/p/9337316.html
Java:
import java.util.*; public class bomb_Two { static int n; //That's ok static int m; //column static char maze[][]; //Storage map static int book[][]; //sign static int x[]; //Queue abscissa static int y[]; //Queue ordinate static int step[]; //Step number static int max=0; //Store the maximum number of enemies static int save_x; //Store target abscissa static int save_y; //Store target ordinates static void Totall(int x,int y) { //Count the number of enemies that can be eliminated int total=0; int dx=x; int dy=y; while(maze[dx][dy]!='#') { //right if(maze[dx][dy]=='G') { total++; } dy++; } dx=x; dy=y; while(maze[dx][dy]!='#') { //lower if(maze[dx][dy]=='G') { total++; } dx++; } dx=x; dy=y; while(maze[dx][dy]!='#') { //Left if(maze[dx][dy]=='G') { total++; } dy--; } dx=x; dy=y; while(maze[dx][dy]!='#') { //upper if(maze[dx][dy]=='G') { total++; } dx--; } if(total>max) { save_x=x; save_y=y; max=total; } } public static void main(String[] args) { Scanner reader=new Scanner(System.in); n=reader.nextInt(); m=reader.nextInt(); int start_x=reader.nextInt(); //Bomber's initial position int start_y=reader.nextInt(); x=new int[n*m]; y=new int[n*m]; step=new int[n*m]; int dir[][]= {{0,1},{1,0},{0,-1},{-1,0}}; //Right, down, left, up int head=0; //Head node int tail=0; //Tail node maze=new char[n][m]; book=new int[n][m]; for(int i=0;i<n;i++) { String ss=reader.next(); maze[i]=ss.toCharArray(); } for(int i=0;i<n;i++) { //Tag array initialization for(int j=0;j<m;j++) { book[i][j]=0; } } Totall(start_x,start_y); //Count the number of enemies that can be destroyed at the initial position //Initial location in queue x[tail]=start_x; y[tail]=start_y; step[tail]=0; book[start_x][start_y]=1; tail++; //Tail pointer backward while(head<=tail) { for(int i=0;i<4;i++) { int dx=x[head]+dir[i][0]; int dy=y[head]+dir[i][1]; if(dx<0 || dx>n || dy<0 || dy>m) { //Transboundary judgment continue; } if(maze[dx][dy]!='.' || book[dx][dy]==1) { //Obstacle point and open space judgment continue; } //Meet the requirements x[tail]=dx; y[tail]=dy; step[tail]=step[head]+1; book[dx][dy]=1; tail++; Totall(dx,dy); //Count the number of enemies } head++; //Outgoing queue } System.out.println("("+save_x+","+save_y+")"+" "+max); } }
12:07:58
2018-07-20