1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #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;
}
|