#include
#include
#include
using namespace std;
类似旅行商问题(本质:n!树形图)
题目:现在共有a个红球,b个蓝色球,c个绿球,要求相邻的球颜色不同(相同颜色的球不做区分,例如红色的表示都一样),共有多少种摆法
int result = 0;
const int sumN = 3+4+5;
int color[sumN+1] = {-1};
struct comp
{
typedef std::pair
bool operator () (const value_type & ls, const value_type &rs) const
{
return ls.first < rs.first || (ls.first == rs.first && ls.second < rs.second);
}
};
std::map
//序号1-sunN开始
//1-3代表蓝绿红三颜色球
//search():实现初始递归
//search2()函数:实现改进递归+加入map,动态规划思路
void search(int idx,vector
{
if (idx >= sumN+1)
{
result += 1;
return;
}
int flag = color[idx - 1];
for (int i = 1; i <= 3 ; i++)
{
if (i == flag || a[i] <= 0)
continue;
else
{
color[idx] = i;
vector
tmp[i]--;
search(idx + 1, tmp);
}
}
}
//
int search2(int idx, vector
{
int resulttmp=0;
if (idx >= sum + 1)
{
return 1;
}
vector
for (int i = 0; i < idx; i++)
colormy.push_back(color[i]);
std::map
res.find(std::make_pair(idx, std::make_pair(a, colormy)));
if (it != res.end())
return it->second;
int flag = color[idx - 1];
for (int i = 1; i <= 3; i++)
{
if (i == flag || a[i] <= 0)
continue;
else
{
color[idx] = i;
vector
tmp[i]--;
resulttmp +=search2(idx + 1, tmp,sum);
}
}
//
res.insert(std::make_pair(std::make_pair(idx,std::make_pair(a, colormy)), resulttmp));
return resulttmp;
}
int main()
{
int n; int m; int k;
cin >> n; cin >> m; cin >> k;
int sum = n + m + k;
vector
cout << "结果是:"<< search2(1, a,sum)<< endl;;