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
| #include <stdio.h> char a[21][21]; int book[20][20] = { 0 }; int n,m, max = 0, sum = 0 , mx, my ; 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; } void dfs(int x,int y) { int k,tx,ty; int next[4][2] = { {0,1},{0,-1},{1,0},{1,0} };
for (k = 0; k <= 3; k++) { tx = x + next[k][0]; ty = 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; sum = getnum(tx, ty); if (sum > max) { max = sum; mx = tx; my = ty; } dfs(tx, ty);
} } return; } int main() { int i, j, k, tail, head, tx, ty, startx, starty; 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); book[startx][starty] = 1; max = getnum(startx, starty); mx = startx; my = starty; dfs(startx, starty); printf("将炸弹放置到(%d,%d)处,可以消灭%d个敌人。", mx, my, max); getchar(); getchar(); return 0; }
|