Place a bomb in your place, where you can kill the most enemies, your position cannot be the same as the enemy input 3 3############# #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############# #输出将炸弹放置在 (7,11), can eliminate up to 10 enemies #include<stdio.h>char A[20][21];int book[20][20],max,mx,my , N,m;int getnum (int i,int j) {int sum,x,y; sum = 0; Sum is used to count (the number of enemies that can be eliminated), so it is necessary to initialize 0//To copy the coordinate i,j to two new variables x, y so that the number of people that can be eliminated in the next four directions to the upper and lower left/right counts x = i; y = j; while (a[x][y]! = ' # ')//judge The point is not the wall, if not the wall will continue {//If the current point is the enemy, then count if (a[x][y] = = ' G ') sum++; The role of x--is to continue up the statistical x--; }//Down statistics kills the number of enemies x = i; y = j; while (a[x][y]! = ' # ')//judge The point is not the wall, if not the wall will continue {//If the current point is the enemy, then count if (a[x][y] = = ' G ') sum++; The role of X + + is to continue to count X + +; }//Left count the number of enemies eliminated x = i; y = j; while (a[x][y]! = ' # ')//judge The point is not the wall, if not the wall will continue {//If the current point is an enemy, then count if (a[x][y] = = ' G ') sum++; The role of y--is to continue to statistical y--to the left; }//Right count the number of enemies eliminated x = i; y = j; while (a[x][y]! = ' # ')//judge The point is not the wall, if not the wall will continue {//If the current point is the enemy, then count if (a[x][y] = = ' G ') sum++; The role of y++ is to continue to the right to statistics y++; } return sum; } void Dfs (int x,int y) {//defines an array for walking direction int next[4][2] ={{0,1},//Right walk {1,0},//Go down {0,-1},//Go left { -1,0}//Go up}; int k,sum,tx,ty; sum = Getnum (x, y);//Calculates the total number of enemies that are currently eliminated if (Sum > Max) {//If the current point counts more enemies than Max, update Max and use Mx,m Y records the coordinates of the current point max = sum; mx = x; my = y; }//Enumeration Four directions for (k = 0; k <= 3; k++) {//next node coordinates tx = x + next[k][0]; ty = y + next[k][1]; Determine if cross-border if (TX < 0 | | tx > N-1 | | Ty < 0 | | ty > m-1) continue; if (a[tx][ty] = = '. ' && boOk[tx][ty] = = 0) {Book[tx][ty] = 1;//Mark this point has traversed Dfs (tx,ty); }} return; } int main () {int i,startx,starty; scanf ("%d%d%d%d", &n,&m,&startx,&starty); Read n-line characters for (i = 0; I <= n-1; i++) scanf ("%s", A[i]); Start from where you stand book[startx][starty] = 1; max = Getnum (Startx,starty); mx = startx; my = Starty; DFS (Startx,starty); printf ("Place the bomb in (%d%d), you can kill%d enemies \ n", Mx,my,max); return 0; }
Maximum number of kills in breadth-first search