节目安排 最大兼容问题
#include
using namespace std;
#define MAX 100
int b[MAX], c[MAX] = { 0 };
struct ENT //节目开始、结束结构体
{
int start;
int end;
};
void arr(int i);
ENT a[MAX];
int n, first = 100000;
int e = 1000000;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].start >> a[i].end;
arr(1);
return 0;
}
void arr(int i)
{
int tempt = 0;
if (i > n)
{
cout << "( ";
for (int k = 1; k <= n; k++)
cout << b[k] << " ";
cout << “)”<<endl;
return;
}
if (i == 1)//找最先开始的节目
{
for (int j = i;j <= n; j++)
if (first > a[j].start)
{
first = a[j].start;
tempt = j;
}
b[i] = tempt;
c[tempt] = 1;
arr(i + 1);
}
for (int j = 1; j <= n; j++)
{
if (c[j]==1||(a[b[i - 1]].end < a[j].start)||(i-1)==j) continue;
if (e > a[j].start)//求下一个节目
{
e = a[j].start;
tempt = j;
}
}
e = a[tempt].end;
b[i] = tempt;
c[tempt] = 1;
arr(i + 1);
}
希望对朋友有一些帮助,仅供参考,如有不完善之处,请多多指教,谢谢。
来源:CSDN
作者:老鱼不老只是愚
链接:https://blog.csdn.net/mili12/article/details/103954973