C#穷举法问题
两个小队的对抗赛,队员分别是A、B、C和X、Y、Z,但是A不想和X比,B不想和X、Z比,用程序求出最合适的出场顺序用C#代码实现...
两个小队的对抗赛,队员分别是A、B、C和X、Y、Z,但是A不想和X比,B不想和X、Z比,用程序求出最合适的出场顺序
用C#代码实现 展开
用C#代码实现 展开
2个回答
展开全部
public partial class Form1 : Form
{
private Hashtable mapRefuse;
private Hashtable list1Already;
private List<string> list1Result;
private List<string> usedList;
List<string> list1;
List<string> list2;
public Form1()
{
InitializeComponent();
mapRefuse = new Hashtable();
list1Already = new Hashtable();
list1Result = new List<string>();
usedList = new List<string>();
}
private void button1_Click(object sender, EventArgs e)
{
list1 = new List<string> { "A", "B", "C" };
list2 = new List<string> { "X", "Y", "Z" };
mapRefuse.Add("A", new List<string> { "X" });
mapRefuse.Add("B", new List<string> { "X", "Z" });
mapRefuse.Add("C", new List<string>());
bool flg = false;
while (!flg)
{
flg = match();
}
foreach (string str in list1Result)
{
MessageBox.Show(str);
}
}
private bool match()
{
bool flg;
list1Result.Clear();
usedList.Clear();
foreach (string t1 in list1)
{
List<string> t1Reuse = (List<string>)mapRefuse[t1];
List<string> listT1Already = (List<string>)list1Already[t1];
if (listT1Already == null)
{
listT1Already = new List<string>();
list1Already.Add(t1, listT1Already);
}
flg = false;
foreach (string t2 in list2)
{
if (!usedList.Contains(t2))
{
if (!t1Reuse.Contains(t2))
{
if (!listT1Already.Contains(t2))
{
list1Result.Add(t1 + "-" + t2);
listT1Already.Add(t2);
usedList.Add(t2);
flg = true;
break;
}
}
}
}
if(!flg)
return flg;
}
return list1Result.Count==list1.Count;
} }
{
private Hashtable mapRefuse;
private Hashtable list1Already;
private List<string> list1Result;
private List<string> usedList;
List<string> list1;
List<string> list2;
public Form1()
{
InitializeComponent();
mapRefuse = new Hashtable();
list1Already = new Hashtable();
list1Result = new List<string>();
usedList = new List<string>();
}
private void button1_Click(object sender, EventArgs e)
{
list1 = new List<string> { "A", "B", "C" };
list2 = new List<string> { "X", "Y", "Z" };
mapRefuse.Add("A", new List<string> { "X" });
mapRefuse.Add("B", new List<string> { "X", "Z" });
mapRefuse.Add("C", new List<string>());
bool flg = false;
while (!flg)
{
flg = match();
}
foreach (string str in list1Result)
{
MessageBox.Show(str);
}
}
private bool match()
{
bool flg;
list1Result.Clear();
usedList.Clear();
foreach (string t1 in list1)
{
List<string> t1Reuse = (List<string>)mapRefuse[t1];
List<string> listT1Already = (List<string>)list1Already[t1];
if (listT1Already == null)
{
listT1Already = new List<string>();
list1Already.Add(t1, listT1Already);
}
flg = false;
foreach (string t2 in list2)
{
if (!usedList.Contains(t2))
{
if (!t1Reuse.Contains(t2))
{
if (!listT1Already.Contains(t2))
{
list1Result.Add(t1 + "-" + t2);
listT1Already.Add(t2);
usedList.Add(t2);
flg = true;
break;
}
}
}
}
if(!flg)
return flg;
}
return list1Result.Count==list1.Count;
} }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询