The setting condition of the question is the same as that of the previous one, but the bomb can only be placed where it can go. The starting position of the villain is the fourth row and the fourth column. Use 'ා' to represent the wall, '. To represent the walking position, and' G 'to represent the little monster. From row 0, column 0
Because the question needs to consider whether it can be reached, it needs to use breadth first search to find the path it can reach and find out the number of small monsters that can be destroyed by placing bombs at the point it can reach.
Analyze this map, put bombs on (1, 11) to kill up to 11 small monsters, but the villain can't go to this position, so the correct answer should be to put bombs on (7, 11) to kill up to 10 small monsters.
/*
#############
#GG.GGG#GGG.#
###.#G#G#G#G#
#.......#..G#
#G#.###.#G#G#
#GG.GGG.#.GG#
#G#.#G#.#.#.# The second line of the map is changed from right to left
##G...G.....#
#G#.#G###.#G#
#...G#GGG.GG#
#G#.#G#G#.#G#
#GG.GGG#G.GG#
#############
*/
#include<stdio.h>
#include<string.h>
int main()
{
char map[20][20];
memset(map,0,sizeof(map));
int q,w,i,j,m=13,n=13,sum=0,flag,f1=0,f2=0;
//scanf("%d %d\n",&m,&n);
for (i=0;i<m;i++)
gets(map[i]);
int stratx=3,straty=3,tx,ty,head=0,tail=0,k,que[400][2],book[20][20];
//bookIt is used to mark whether to pass this point
memset(book,0,sizeof(book));
int next[4][2]={0,1,1,0,0,-1,-1,0};
que[tail][0]=stratx;
que[tail][1]=straty;
tail++;
while (head<tail)
{
for (k=0;k<3;k++)
{
//Next point coordinates
tx=que[head][0]+next[k][0];
ty=que[head][1]+next[k][1];
//Judgement boundary
if (tx<0||tx>m-1||ty<0||ty>n-1)
continue;
//Judge whether to pass
if (map[tx][ty]=='.'&&book[tx][ty]==0)
{
//Mark this point has passed
book[tx][ty]=1;
//Join the team
que[tail][0]=tx;
que[tail][1]=ty;
tail++;
}
}
head++;
}
for (i=1;i<m-1;i++)
{
for (j=1;j<n-1;j++)
{
if (map[i][j]=='.'&&book[i][j]==1)
{
flag=0;
q=i;w=j;
while (map[q][w]!='#')
{
if (map[q][w]=='G')
flag++;
q--;
}
q=i;w=j;
while (map[q][w]!='#')
{
if (map[q][w]=='G')
flag++;
q++;
}
q=i;w=j;
while (map[q][w]!='#')
{
if (map[q][w]=='G')
flag++;
w--;
}
q=i;w=j;
while (map[q][w]!='#')
{
if (map[q][w]=='G')
flag++;
w++;
}
map[i][j]=flag+'0';
if (flag>=sum)
{
sum=flag;
f1=i;
f2=j;
}
}
}
}
//Display map
/*for (i=0;i<m;i++)
puts(map[i]);*/
printf("In (%d,%d)Place bombs to kill the most%dA little monster.",f1,f2,sum);
}