Note/大学笔记/C++/炸弹人游戏题目.md

123 lines
2.5 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 炸弹人游戏
date: 2022-03-09 00:16:12.441
updated: 2022-09-05 20:04:13.861
url: https://hhdxw.top/archives/75
categories:
- C&C++
tags:
- C&C++
---
## 一、题目:
你只有一枚炸弹,这枚炸弹威力超强(杀伤距离超长,可以消灭杀伤范围内的所有敌人)。请问在哪里放置炸弹可以消灭最多的敌人?最多可以消灭多少敌人?
## 二、注意:
灰色底纹表示墙,炸弹不能穿透。
人脸 表示敌人。
空白位置表示空地。
炸弹只能安放到空地上。
![img](https://lsky.hhdxw.top/imghub/img/wps1700.tmp.jpg)
## 三、代码实现如下:
```c
#include <bits/stdc++.h>
using namespace std;
int main()
{
//0.墙 1.表示敌人 2.空地
int max = 0,p,q;
int a[13][13] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,2,1,1,1,0,1,1,1,2,0,
0,0,0,2,0,1,0,1,0,1,0,1,0,
0,2,2,2,2,2,2,2,0,2,2,1,0,
0,1,0,2,0,0,0,2,0,1,0,1,0,
0,1,1,2,1,1,1,2,0,2,1,1,0,
0,1,0,2,0,1,0,2,0,2,0,0,0,
0,0,1,2,2,2,1,2,2,2,2,2,0,
0,1,0,2,0,1,0,0,0,2,0,1,0,
0,2,2,2,1,0,1,1,1,2,1,1,0,
0,1,0,2,0,1,0,1,0,2,0,1,0,
0,1,1,2,1,1,1,0,1,2,1,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,};
// j代表每一列i代表每一行
for(int i = 1;i < 12;i++)
{
for(int j = 1;j < 12;j++)
{
if(a[i][j] == 2)
{
//sum临时计算敌人数量x,y,分别是横纵坐标
int x,y,sum=0;
//向上
x=i;
y=j;
//判断不撞到墙就进入,撞到墙就停止移动
while(a[x][y]!=0)
{
//判断是不是敌人,是,则加一
if(a[x][y]==1)
sum++;
//控制移动炸弹范围
x--;
}
//向下
x=i;
y=j;
while(a[x][y]!=0)
{
if(a[x][y]==1)
sum++;
x++;
}
//向左
x=i;
y=j;
while(a[x][y]!=0)
{
if(a[x][y]==1)
sum++;
y--;
}
//向右
x=i;
y=j;
while(a[x][y]!=0)
{
if(a[x][y]==1)
sum++;
y++;
}
//统计四个方向行走过程中碰到的敌人的数量,并与上一次做比较寻找最大值
if(sum>max)
{
max=sum;
p=i;
q=j;
}
}
}
}
cout << "最多人数是:" << max << "人" << endl;
cout << p << "行" << q << "列" << endl;
return 0;
}
```
## 四、运行结果如下:
![image-20220309101417055](https://lsky.hhdxw.top/imghub/img/image-20220309101417055.png)