AcWing 99. 激光炸弹 (矩阵前缀和)

AcWing 99. 激光炸弹 (矩阵前缀和)

地图上有 N 个目标,用整数 Xi,Yi 表示目标在地图上的位置,每个目标都有一个价值 Wi。

注意:不同目标可能在同一位置。

现在有一种新型的激光炸弹,可以摧毁一个包含 R×R 个位置的正方形内的所有目标。

激光炸弹的投放是通过卫星定位的,但其有一个缺点,就是其爆炸范围,即那个正方形的边必须和 x,y 轴平行。

求一颗炸弹最多能炸掉地图上总价值为多少的目标。

输入格式

第一行输入正整数 N 和 R,分别代表地图上的目标数目和正方形的边长,数据用空格隔开。

接下来 N 行,每行输入一组数据,每组数据包括三个整数 Xi,Yi,Wi,分别代表目标的 x 坐标,y 坐标和价值,数据用空格隔开。

输出格式

输出一个正整数,代表一颗炸弹最多能炸掉地图上目标的总价值数目。

数据范围

0≤R≤10^9
0<N≤10000,
0≤Xi,Yi≤5000
0≤Wi≤1000

输入样例:

2 1
0 0 1
1 1 1

输出样例:

1

题解:

首先预处理出整个矩阵的前缀和O(n * n):

for (int i = 1; i <= 5000; i ++) { // 求整个矩阵的前缀和 
		for (int j = 1; j <= 5000; j ++) {
			a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
		}
	}

接着我们就可以在O(1)时间复杂度下算出每一个R x R子矩阵的和。

左上角坐标为(i - r, j - r), 右下角坐标为(i, j)的子矩阵和为:

a[i][j] - a[i - r][j] - a[i][j - r] + a[i - r][j - r]

枚举每一个R x R的子矩阵,计算矩阵和,记录并输出最大值即可。

AC代码:

#include <iostream>
using namespace std;
typedef long long ll;
const int N = 5e3 + 5;
int a[N][N];
int main() {
	ios::sync_with_stdio(false);
	int n, r;
	cin >> n >> r;
	r = min(5001, r); // 如果炸弹半径大于5001则取整个地图即可 
	while (n --) {
		int x, y, w;
		cin >> x >> y >> w;
		a[x + 1][y + 1] += w;
	}
	for (int i = 1; i <= 5001; i ++) { // 求整个矩阵的前缀和 
		for (int j = 1; j <= 5001; j ++) {
			a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
		}
	}
	int mx = -1;
	for (int i = r; i <= 5001; i ++) { // 枚举每一个R x R的子矩阵,计算矩阵和 
		for (int j = r; j <= 5001; j ++) {
			mx = max(mx, a[i][j] - a[i - r][j] - a[i][j - r] + a[i - r][j - r]); // 记录最大值 
		}
	}
	cout << mx << '\n';
	return 0;
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
当然可以!下面是一个使用Cocos Creator和TypeScript创建炸弹人游戏的示例代码: ``` const { ccclass, property } = cc._decorator; enum ItemType { BombRange, SpeedUp } @ccclass export default class BombMan extends cc.Component { @property(cc.Prefab) private playerPrefab: cc.Prefab = null; @property(cc.Prefab) private enemyPrefab: cc.Prefab = null; @property(cc.Prefab) private itemPrefab: cc.Prefab[] = []; @property(cc.SpriteFrame) private brickSprite: cc.SpriteFrame = null; private player: cc.Node = null; private enemies: cc.Node[] = []; private items: cc.Node[] = []; private map: number[][] = []; private gridSize: number = 45; private gridRows: number = 15; private gridCols: number = 15; private bombRange: number = 1; private playerSpeed: number = 200; onLoad() { this.generateMap(); this.spawnPlayer(); this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } generateMap() { for (let i = 0; i < this.gridRows; i++) { this.map[i] = []; for (let j = 0; j < this.gridCols; j++) { let tileType = Math.random() < 0.8 ? 0 : 1; this.map[i][j] = tileType; if (tileType === 1) { let brickNode = new cc.Node(); let sprite = brickNode.addComponent(cc.Sprite); sprite.spriteFrame = this.brickSprite; brickNode.parent = this.node; brickNode.setPosition(j * this.gridSize, i * this.gridSize); } } } } spawnPlayer() { this.player = cc.instantiate(this.playerPrefab); this.player.parent = this.node; this.player.setPosition(this.gridSize / 2, this.gridSize / 2); } spawnEnemy() { let enemyNode = cc.instantiate(this.enemyPrefab); enemyNode.parent = this.node; // 随机生成敌人的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); enemyNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.enemies.push(enemyNode); // 随机生成敌人的生成时间间隔 this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); } spawnItem() { let itemType = Math.floor(Math.random() * this.itemPrefab.length); let itemNode = cc.instantiate(this.itemPrefab[itemType]); itemNode.parent = this.node; // 随机生成道具的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); itemNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.items.push(itemNode); // 随机生成道具的生成时间间隔 this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } // ... } ``` 这只是一个简单的示例代码,包含了地图的生成、玩家、敌人和道具的生成。你可以根据自己的需求进一步完善游戏逻辑和其他功能。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值