找出炸弹放在哪里消灭的敌人最多
#includeusing namespace std; char a[20][20]; struct note { int x; int y; }; int getnum(int i,int j){ int p=i, q=j, sum = 0; //向下统计可以消灭的敌人数(炸弹不能穿墙) while (a[p][q] != '#') { if (a[p][q] == 'G') sum++; p++; } //向上统计可以消灭的敌人数(炸弹不能穿墙) p = i; q = j; while (a[p][q] != '#') { if (a[p][q] == 'G') sum++; p--; } //向左统计可以消灭的敌人数(炸弹不能穿墙) p = i; q = j; while (a[p][q] != '#') { if (a[p][q] == 'G') sum++; q--; } //向右统计可以消灭的敌人数(炸弹不能穿墙) p = i; q = j; while (a[p][q] != '#') { if (a[p][q] == 'G') sum++; q++; } return sum; } //起点开始四周扩展,计算哪些点是小人可以到达的们可以到达再放置炸弹计算消灭的敌人数; int main() { struct note que[401];//假设地图20*20 int book[20][20] = { 0 }; int head, tail,i,j,startx,starty,m,n,k,tx,ty,bigsum=0, newsum,newtx,newty; int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} }; //输入n行m列 cin >> n >> m; //输入起点 //矩阵输入数据 cin >> startx >> starty; for (i = 0;i < n;i++) for (j = 0;j < m;j++) cin >> a[i][j]; head = 1; tail = 1; que[tail].x = startx; que[tail].y = starty; tail++; book[startx][starty] = 1; 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<0 || tx>n-1 || ty<0 || ty>m-1) { continue; } if (a[tx][ty] == '.'&&book[tx][ty] == 0) { book[tx][ty] = 1; que[tail].x = tx; que[tail].y = ty; tail++; newsum = getnum(tx, ty); if (newsum > bigsum) { bigsum = newsum; newtx = tx; newty = ty; } } } head++; } cout << newtx << " " << newty << " " << bigsum; return 0; }
样例输入:
13 13 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# #############
样例输出:
暑期编程PK赛 得CSDN机械键盘等精美礼品!7 11 10



