在c#中实现猜数字算法的方法
算法:采用逐步缩小状态空间法,首先找出4个正确的数(位置不管),然后再确定其位置,对下一个测试的4个数字采用估价函数选取值最大的一个状态,这样状态空间缩小的很快,估价函数是该状态当未被排除时其参考的状态的 A的个数加B的个数,下面是全部程序,测试代码:(每一个CASE是随机产生的)
Cmain2 C = new Cmain2();
C.Search();
程序:(代码比较乱,思路不乱)
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Threading;
namespace猜数字
{
publicclassCCase
{
publicint[]Case;
publicintvalue;
publicintA;
publicintB;
}
publicclassmyReverserClass:IComparer
{
intIComparer.Compare(Objectx,Objecty)
{
return((newCaseInsensitiveComparer()).Compare(((CCase)y).value,((CCase)x).value));
}
}
publicclassmyReverserClass1:IComparer
{
intIComparer.Compare(Objectx,Objecty)
{
return((newCaseInsensitiveComparer()).Compare(((CCase)x).A,((CCase)y).A));
}
}
publicclassCmain2
{
CCaseCMyCase;
publicCCaseAnswer;
ArrayListRightAr=newArrayList();
ArrayListCaseAr=newArrayList();
ArrayListACase=newArrayList();
publicvoidInit()
{
for(inti=0;i<10;i++)
{
for(intj=i+1;j<10;j++)
{
for(intk=j+1;k<10;k++)
{
for(intl=k+1;l<10;l++)
{
CCaseMyCase=newCCase();
MyCase.Case=newint[4]{i,j,k,l};
CaseAr.Add(MyCase);
}
}
}
}
CMyCase=(CCase)CaseAr[0];
CreateAnswer();
}
publicCmain2()
{
Init();
}
///<summary>
///搜索正确数字
///</summary>
publicvoidSearch()
{
Console.WriteLine("随机生成的4个数字: "+Answer.Case[0]+""+Answer.Case[1]+""+Answer.Case[2]+""+Answer.Case[3]+" 下面开始模拟猜数字: ");
//Answer.Case=newint[4]{9,6,4,3};
while((CMyCase.A+CMyCase.B!=4)&&CaseAr.Count>1)
{
//Thread.Sleep(1000);
CreateNextCase();
}
Console.WriteLine(" 已找到满意解,继续寻找最优解: ");
GetTheRightAnswer();
Console.WriteLine("找到了答案:"+CMyCase.Case[0]+""+CMyCase.Case[1]+""+CMyCase.Case[2]+""+CMyCase.Case[3]+"");
Console.WriteLine("共进行了"+ACase.Count+"次CASE");
}
///<summary>
///产生下一用于测试的4数字
///</summary>
publicvoidCreateNextCase()
{
ACase.Add(CMyCase);
CheckAnwser();
Console.WriteLine("CASE"+ACase.Count+":"+CMyCase.Case[0]+""+CMyCase.Case[1]+""+CMyCase.Case[2]+""+CMyCase.Case[3]+" 测试结果:"+CMyCase.A+"A"+CMyCase.B+"B");
CaseAr.Remove(CMyCase);
ClearCaseAr();
Console.WriteLine("剩下的状态空间数:"+CaseAr.Count+" ");
if(CaseAr.Count<=1)
{
if(CaseAr.Count==1)
CMyCase=(CCase)CaseAr[0];
return;
}
CMyCase=(CCase)CaseAr[0];
}
///<summary>
///逐步缩小状态空间
///</summary>
publicvoidClearCaseAr()
{
intvalue=CMyCase.value;
for(inti=0;i<CaseAr.Count;)
{
intcount=0;
CCaseccase=((CCase)CaseAr[i]);
for(intj=0;j<4;j++)
{
for(intk=0;k<4;k++)
{
if(CMyCase.Case[j]==ccase.Case[k])
count++;
}
}
if(count!=value)
{
CaseAr.RemoveAt(i);
}
else
{
ccase.value=value;
i++;
}
}
myReverserClasscmp=newmyReverserClass();
CaseAr.Sort(cmp);
}
///<summary>
///随即产生要猜的CASE
///</summary>
publicvoidCreateAnswer()
{
Answer
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Threading;
namespace猜数字
{
publicclassCCase
{
publicint[]Case;
publicintvalue;
publicintA;
publicintB;
}
publicclassmyReverserClass:IComparer
{
intIComparer.Compare(Objectx,Objecty)
{
return((newCaseInsensitiveComparer()).Compare(((CCase)y).value,((CCase)x).value));
}
}
publicclassmyReverserClass1:IComparer
{
intIComparer.Compare(Objectx,Objecty)
{
return((newCaseInsensitiveComparer()).Compare(((CCase)x).A,((CCase)y).A));
}
}
publicclassCmain2
{
CCaseCMyCase;
publicCCaseAnswer;
ArrayListRightAr=newArrayList();
ArrayListCaseAr=newArrayList();
ArrayListACase=newArrayList();
publicvoidInit()
{
for(inti=0;i<10;i++)
{
for(intj=i+1;j<10;j++)
{
for(intk=j+1;k<10;k++)
{
for(intl=k+1;l<10;l++)
{
CCaseMyCase=newCCase();
MyCase.Case=newint[4]{i,j,k,l};
CaseAr.Add(MyCase);
}
}
}
}
CMyCase=(CCase)CaseAr[0];
CreateAnswer();
}
publicCmain2()
{
Init();
}
///<summary>
///搜索正确数字
///</summary>
publicvoidSearch()
{
Console.WriteLine("随机生成的4个数字: "+Answer.Case[0]+""+Answer.Case[1]+""+Answer.Case[2]+""+Answer.Case[3]+" 下面开始模拟猜数字: ");
//Answer.Case=newint[4]{9,6,4,3};
while((CMyCase.A+CMyCase.B!=4)&&CaseAr.Count>1)
{
//Thread.Sleep(1000);
CreateNextCase();
}
Console.WriteLine(" 已找到满意解,继续寻找最优解: ");
GetTheRightAnswer();
Console.WriteLine("找到了答案:"+CMyCase.Case[0]+""+CMyCase.Case[1]+""+CMyCase.Case[2]+""+CMyCase.Case[3]+"");
Console.WriteLine("共进行了"+ACase.Count+"次CASE");
}
///<summary>
///产生下一用于测试的4数字
///</summary>
publicvoidCreateNextCase()
{
ACase.Add(CMyCase);
CheckAnwser();
Console.WriteLine("CASE"+ACase.Count+":"+CMyCase.Case[0]+""+CMyCase.Case[1]+""+CMyCase.Case[2]+""+CMyCase.Case[3]+" 测试结果:"+CMyCase.A+"A"+CMyCase.B+"B");
CaseAr.Remove(CMyCase);
ClearCaseAr();
Console.WriteLine("剩下的状态空间数:"+CaseAr.Count+" ");
if(CaseAr.Count<=1)
{
if(CaseAr.Count==1)
CMyCase=(CCase)CaseAr[0];
return;
}
CMyCase=(CCase)CaseAr[0];
}
///<summary>
///逐步缩小状态空间
///</summary>
publicvoidClearCaseAr()
{
intvalue=CMyCase.value;
for(inti=0;i<CaseAr.Count;)
{
intcount=0;
CCaseccase=((CCase)CaseAr[i]);
for(intj=0;j<4;j++)
{
for(intk=0;k<4;k++)
{
if(CMyCase.Case[j]==ccase.Case[k])
count++;
}
}
if(count!=value)
{
CaseAr.RemoveAt(i);
}
else
{
ccase.value=value;
i++;
}
}
myReverserClasscmp=newmyReverserClass();
CaseAr.Sort(cmp);
}
///<summary>
///随即产生要猜的CASE
///</summary>
publicvoidCreateAnswer()
{
Answer
本文地址:http://www.45fan.com/a/question/69313.html