http://usaco.org/index.php?page=viewproblem2&cpid=647
和2048类似的游戏,大致意思就是输入一个N,数列的长度,数列中相邻两个元素如果相同可以合并并+1,比如两个3相邻,可以合并成4,求输入数列最大能出现的数字。下面贴代码及注释
#include<iostream>
#include<vector>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
typedef struct node
{
int t;//通过t个值合成
int x;//值
}node;
int main()
{
freopen("248.in", "r", stdin);
freopen("248.out","w",stdout);
vector<node> s[250];
int max=0;
int N;
cin >> N;
node temp;
for (int t =0;t < N;t++)
{
//scanf("%d", &temp.x);
cin >> temp.x;
temp.t = 1;
s[t].push_back(temp);
int j = 1;
while (j<=t)
{
if (s[t].back().x >= s[t-j].front().x && s[t].back().x <= s[t-j].back().x)//判断是否可与之前的数字合成
{
temp.x++; //值+1
int qqq = s[t].back().x; //寻找与之前位置容器中与s[t].back()一样的值
temp.t += (*(s[t-j].begin()+s[t].back().x-s[t-j].front().x)).t; //合成数相加
s[t].push_back(temp);
j = s[t].back().t; //判断合成后是否可继续合成
}
else break;
}
//for (auto x : s[t])
// cout << x.x << " " << x.t << " ";
if (max < s[t].back().x) max = s[t].back().x;
}
cout << max << endl;
}