问题描述



代码解决
#include <stdio.h>
char a[21][21];
struct node {
int x;
int y;
};
int getnum(int x,int y) {
int num=0;
int tx, ty;
tx = x; ty = y;
while (a[tx][ty] != '#') {
if (a[tx][ty] == 'G') {
num++;
}
ty++;
}
tx = x; ty = y;
while (a[tx][ty] != '#') {
if (a[tx][ty] == 'G') {
num++;
}
ty--;
}
tx = x; ty = y;
while (a[tx][ty] != '#') {
if (a[tx][ty] == 'G') {
num++;
}
tx++;
}
tx = x; ty = y;
while (a[tx][ty] != '#') {
if (a[tx][ty] == 'G') {
num++;
}
tx--;
}
return num;
}
int main() {
int i, j, k,n,m,tail,head,max=0,sum=0,tx,ty,mx,my,startx,starty;
struct node que[401];
int book[20][20] = {0};
printf("请输入你要输入几行几列的地图:");
scanf_s("%d %d", &n, &m);
printf("请输入的地图:\n");
for(i=0;i<=n-1;i++)
scanf_s("%s", a[i],20);
printf("请输入初始位置:");
scanf_s("%d %d", &startx, &starty);
int next[4][2] = { {0,1},{0,-1},{1,0},{1,0} };
tail = 1;
head = 1;
que[tail].x = startx;
que[tail].y = starty;
book[startx][starty] = 1;
tail++;
max = getnum(startx, starty);
mx = startx;
my = starty;
while (head < tail) {
for (k = 0; k <= 3; k++) {
tx = que[head].x + next[k][0];
ty = que[head].y + next[k][1];
if (tx<1 || tx>n || ty<1 || ty>m)
continue;
if (book[tx][ty] == 0 && a[tx][ty] == '.') {
book[tx][ty] = 1;
que[tail].x = tx;
que[tail].y = ty;
tail++;
sum = getnum(tx, ty);
if (sum > max) {
max = sum;
mx = tx;
my = ty;
}
}
}
head++;
}
printf("将炸弹放置到(%d,%d)处,可以消灭%d个敌人。",mx,my, max);
getchar(); getchar();
return 0;
}
输入
请输入你要输入几行几列的地图: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#
#############
请输入初始位置: 3 3
- #表示墙 G表示敌人 .表示空地
- 地图大小规定最大为:20*20
输出
将炸弹放置到(7,11)处,可以消灭10个敌人。
总结: