wbxlx(五笔小练习) 请求支援

勉强编完,但是有的数据对,有的数据就死循环了,求大牛修正指教,谢谢!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include <algorithm>
#include <vector>
using namespace std;
char box;
struct eightcodes
{
char a[10];
int f,g,h;
bool operator <(const eightcodes& other) const
{
return f<=other.f;
}
};
typedef list <eightcodes> LIST;
typedef vector <eightcodes> VECTOR;
LIST openlist;
VECTOR closelist;
char end[10];
eightcodes start;
eightcodes now[5];
int i,j,count,k,count1;
int h(eightcodes test)
{
test.h=0;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
if(test.a[i]==end[j])
count1=abs(i-j);
}
if(count1==0) k=0;
else if(count1==1) k=1;
else if(count1==2) k=2;
else if(count1==3) k=1;
else if(count1==4) k=2;
else if(count1==5) k=3;
else if(count1==6) k=2;
else if(count1==7) k=3;
else if(count1==8) k=4;
test.h=test.h+k;

}

return test.h;
}
int g(eightcodes test)
{
test.g=openlist.front().g+1;
return test.g;
}
int f(eightcodes test)
{
return test.g+test.h;
}
int findzero(eightcodes test)
{
for(i=1;i<10;i++)
{
if(test.a[i]=='0')
break;
}
cout<<"zero point is"<<i<<endl;
return i;
}

void run(int t)
{

if(t-3>0)
{
box=openlist.front().a[t-3];
now[1].a[t-3]='0';
now[1].a[t]=box;
cout<<"now1 is ";
for(i=1;i<10;i++)
{
if(i!=t&&i!=t-3)
now[1].a[i]=openlist.front().a[i];
cout<<now[1].a[i]<<" ";
}
cout<<endl;
now[1].g=g(now[1]);cout<<"now1 g is "<<now[1].g<<endl;
now[1].h=h(now[1]);cout<<"now1 h is "<<now[1].h<<endl;
now[1].f=f(now[1]);
for(i=(int)closelist.size()-1;i>=0;--i)
{
for(j=1;j<10;j++)
{
if(closelist[i].a[j]!=now[1].a[j])
k=k+1;
}
}
if(k!=9) openlist.push_back(now[1]);
}
if(t+1<10)
{
box=openlist.front().a[t+1];
now[2].a[t+1]='0';
now[2].a[t]=box;
cout<<"now2 is ";
for(i=1;i<10;i++)
{
if(i!=t&&i!=t+1)
now[2].a[i]=openlist.front().a[i];
cout<<now[2].a[i]<<" ";
}
cout<<endl;
now[2].g=g(now[2]);cout<<"now2 g is "<<now[2].g<<endl;
now[2].h=h(now[2]);cout<<"now2 h is "<<now[2].h<<endl;
now[2].f=f(now[2]);
for(i=(int)closelist.size()-1;i>=0;--i)
{
for(j=1;j<10;j++)
{
if(closelist[i].a[j]!=now[2].a[j])
k=k+1;
}
}
if(k!=9) openlist.push_back(now[2]);
}
if(t+3<10)
{
box=openlist.front().a[t+3];
now[3].a[t+3]='0';
now[3].a[t]=box;
cout<<"now3 is ";
for(i=1;i<10;i++)
{
if(i!=t&&i!=t+3)
now[3].a[i]=openlist.front().a[i];
cout<<now[3].a[i]<<" ";
}
cout<<endl;
now[3].g=g(now[3]);cout<<"now3 g is "<<now[3].g<<endl;
now[3].h=h(now[3]);cout<<"now3 h is "<<now[3].h<<endl;
now[3].f=f(now[3]);
for(i=(int)closelist.size()-1;i>=0;--i)
{
for(j=1;j<10;j++)
{
if(closelist[i].a[j]!=now[3].a[j])
k=k+1;
}
}
if(k!=1) openlist.push_back(now[3]);
}
if(t-1>0)
{
box=openlist.front().a[t-1];
now[4].a[t-1]='0';
now[4].a[t]=box;
cout<<"now4 is ";
for(i=1;i<10;i++)
{
if(i!=t&&i!=t-1)
now[4].a[i]=openlist.front().a[i];
cout<<now[4].a[i]<<" ";
}
cout<<endl;
now[4].g=g(now[4]);cout<<"now4 g is "<<now[4].g<<endl;
now[4].h=h(now[4]);cout<<"now4 h is "<<now[4].h<<endl;
now[4].f=f(now[4]);
for(i=(int)closelist.size()-1;i>=0;--i)
{
for(j=1;j<10;j++)
{
if(closelist[i].a[j]==now[4].a[j])
k=k+1;
}
}
if(k!=9) openlist.push_back(now[4]);
}
closelist.push_back(openlist.front());
openlist.pop_front();
cout<<"openlist size is "<<openlist.size()<<endl;
cout<<"closelist size is "<<closelist.size()<<endl;
}
int main()
{
for(i=1;i<10;i++)
cin>>start.a[i];
for(i=1;i<10;i++)
cin>>end[i];
start.g=0;
start.h=-1;
openlist.push_front(start);
while(openlist.size()>0)
{
run(findzero(openlist.front()));
openlist.sort();
if(closelist.back().h==0)
break;

}
cout<<closelist.back().g<<endl;
return 0;
}

作者: OMG_PPP   发布时间: 2011-04-08