炸弹人--枚举

2024-05-02 06:48
文章标签 枚举 炸弹

本文主要是介绍炸弹人--枚举,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

炸弹人–枚举

要求

‘#’代表墙,‘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)

这篇关于炸弹人--枚举的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/953623

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

【Rust练习】12.枚举

练习题来自:https://practice-zh.course.rs/compound-types/enum.html 1 // 修复错误enum Number {Zero,One,Two,}enum Number1 {Zero = 0,One,Two,}// C语言风格的枚举定义enum Number2 {Zero = 0.0,One = 1.0,Two = 2.0,}fn m

枚举相关知识点

1.是用户定义的数据类型,为一组相关的常量赋予有意义的名字。 2.enum常量本身带有类型信息,即Weekday.SUN类型是Weekday,编译器会自动检查出类型错误,在编译期间可检查错误。 3.enum定义的枚举类有什么特点。         a.定义的enum类型总是继承自java.lang.Enum,且不能被继承,因为enum被编译器编译为final修饰的类。         b.只能定义

【C语言】结构体、枚举、联合体

【C语言】结构体、枚举、联合体 文章目录 @[TOC](文章目录) 前言一、结构体声明1.一般格式2.typedef 重命名结构体类型定义变量 二、结构体数组三、结构体与指针及函数传参四、结构体传参五.结构体在内存的存储六、参考文献总结 前言 使用工具: 1.编译器:VScode 2.C Primer Plus 第六版-1 提示:以下是本篇文章正文内容,下面案例可供参考

10730-Antiarithmetic?【暴力枚举】

水题 求一个序列是否存在3个数按顺序构成等差数列 直接枚举等差数列的差值 时间复杂度降到 n * n / 3 开pos数组记录每个值得为之 楷vis数组记录目前i是否出现过 强行AC 15221397 10730 Antiarithmetic? Accepted C++ 0.035 2015-03-26 12:09:56 #include<cstdio>#include

Java基础入门 【第七章 抽象、接口、内部类、枚举】(二)

匿名内部类书写格式: 父类或接口类型变量名=new父类或接口(构造方法实参列表){ //重写所有的抽象方法 @Override public返回值类型method1(形参列表){ 方法体实现 } @Override public返回值类型method2(形参列表){ 方法体实现 } //省略... }; //匿名内部类对象调用方法 变量名.重写方法(实参列表); 匿名

UVA10010(八方向暴力枚举)

Where's Waldorf? Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18656 Description Where's Waldo

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There

CF#278 (Div. 2) A.(暴力枚举)

A. Giga Tower time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/A Giga To