本文主要是介绍炸弹人--枚举,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
炸弹人–枚举
要求
‘#’代表墙,‘G’代表敌人,‘.’代表空地,炸弹安放在空地上,炸弹威力不能穿过墙,哪里安放炸弹炸死的敌人最多?
思路
对每个点进行枚举,上下左右统计可消灭敌人的数量。
代码
#include <stdio.h>int main()
{char a[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#","#############"};int b[13][13];int x,y;for (int i = 1; i < 13; ++i){for (int j = 1; j < 13; ++j){if (a[i][j] != '.') {continue;}x = i;y = j;while(a[x][y] != '#') { //righty++;if(a[x][y] == 'G') {b[i][j]++;}}x = i;y = j;while(a[x][y] != '#') { //downx++;if(a[x][y] == 'G') {b[i][j]++;}}x = i;y = j;while(a[x][y] != '#') { //lefty--;if(a[x][y] == 'G') {b[i][j]++;}}x = i;y = j;while(a[x][y] != '#') { // upx--;if(a[x][y] == 'G') {b[i][j]++;}}}}for (int i = 0; i < 13; ++i){for (int j = 0; j < 13; ++j){printf("%c ", a[i][j]);}printf("\n");}printf("\n");for (int i = 0; i < 13; ++i){for (int j = 0; j < 13; ++j){printf("%d ", b[i][j]);}printf("\n");}return 0;
}
结果:
# # # # # # # # # # # # #
# G G . G G G # G G G . #
# # # . # G # G # G # G #
# . . . . . . . # . . G #
# G # . # # # . # G # G #
# G G . G G G . # . G G #
# G # . # G # . # . # . #
# # G . . . G . . . . . #
# G # . # G # # # . # G #
# . . . G # G G G . G G #
# G # . # G # G # . # G #
# G G . G G G # G . G G #
# # # # # # # # # # # # #0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0 0 0 11 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 0 0 0 2 0 1 0 4 1 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 5 0 0 0 6 0 5 0 0 0
0 0 0 0 0 0 0 1 0 3 0 8 0
0 0 0 2 2 5 0 3 2 5 2 10 0
0 0 0 0 0 0 0 0 0 3 0 0 0
0 4 1 1 0 0 0 0 0 8 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0
0 0 0 5 0 0 0 0 0 6 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
python 代码
a =["#############","#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#","#############"]n = 13
b = [([0] * n) for i in range(n)]def mprint(x):for i in range(0, n):print(x[i])for i in range(0, n):for j in range(0, n):if a[i][j] != '.':continuex = iy = jwhile a[x][y] != '#': # righty = y + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#': # downx = x + 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#': # lefty = y - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1x = iy = jwhile a[x][y] != '#': # upx = x - 1if a[x][y] == 'G':b[i][j] = b[i][j] + 1mprint(a)
mprint(b)
这篇关于炸弹人--枚举的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!