【动态规划DP】一维消消乐

题目描述

一维消消乐是一款非常简单的游戏。有 n 颗珠子排成一排,每一颗珠子有一个价值 wi(可能是负数)。游戏是这样,你可以选择如若干对相邻的珠子,让他们同时消去。每一对珠子的消失,都会使得总分数加上两颗珠子相乘的分数。注意,每个珠子只能消一次,并且珠子消去以后,还会占位。
输入格式
输入第一行一个整数n(1≤n≤10000)。
接下来一行输入 n 个整数 (−1000≤ wi ≤1000)。
输出格式
输出最大的分数。
样例输入:

8
-9 -5 -4 -2 4 -5 -4 2
样例输出:

73


动态规划题的大致流程

  1. 设计状态:dp[i][i]其中i表示第几个珠子,j表示是否消去,0表示未消,1表示消去
  2. 写出状态转移方程:dp[i][0]=max(dp[i-1][0],dp[i-1][1])
    dp[i][1]=dp[i-1][0]+w[i]*w[i-1]
  3. 设定初始状态:dp[1][0]=0
  4. 执行状态转移
    在这里插入图片描述
#include <iostream>
using namespace std;
int n;
int w[100],dp[100][100];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>w[i];
    }
    dp[1][0]=0;
    for(int i=2;i<=n;i++){
        dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
        dp[i][1]=dp[i-1][0]+w[i]*w[i-1];
    }
    cout<<max(dp[n][0],dp[n][1])<<endl;
} // namespace std;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的消消乐HTML代码: ```html <!DOCTYPE html> <html> <head> <title>消消乐</title> <style> body { background-color: #ddd; display: flex; justify-content: center; align-items: center; height: 100vh; font-size: 2rem; } .container { display: flex; flex-wrap: wrap; width: 500px; height: 500px; background-color: #fff; padding: 10px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); } .tile { display: flex; justify-content: center; align-items: center; width: 100px; height: 100px; background-color: #eee; margin: 5px; border-radius: 5px; font-weight: bold; cursor: pointer; transition: transform 0.2s; } .tile:hover { transform: scale(1.1); } .selected { background-color: #ff0; } .matched { background-color: #f00; color: #fff; } </style> </head> <body> <div class="container"></div> <script> const colors = ['red', 'green', 'blue', 'yellow', 'orange', 'purple']; const container = document.querySelector('.container'); const tiles = []; let selected = []; // 创建随机颜色的方块 function createTile() { const color = colors[Math.floor(Math.random() * colors.length)]; const tile = document.createElement('div'); tile.classList.add('tile'); tile.dataset.color = color; tile.innerText = color; tile.addEventListener('click', () => { if (selected.length < 2 && !tile.classList.contains('selected')) { tile.classList.add('selected'); selected.push(tile); if (selected.length === 2) { if (selected[0].dataset.color === selected[1].dataset.color) { selected.forEach(tile => { tile.classList.remove('selected'); tile.classList.add('matched'); tile.removeEventListener('click', () => {}); }); } else { setTimeout(() => { selected.forEach(tile => { tile.classList.remove('selected'); }); }, 1000); } selected = []; } } }); return tile; } // 创建九宫格 for (let i = 0; i < 9; i++) { const tile = createTile(); tiles.push(tile); container.appendChild(tile); } </script> </body> </html> ``` 这个代码在页面创建一个九宫格的消消乐游戏。每个方块随机出现为六种颜色之一,家需要通过点击方块来消除相同颜色的方块。点击两个不同颜色的方块,它们在1秒后翻转回去,如果点击了两个相同颜色的方块,它们就消失并变为红色,表示已经匹配成功。游戏持续进行,直到所有方块都匹配成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lydia.na

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值