Вы находитесь на странице: 1из 9

�Ź�����(������)����̶�̬��ʾ

һ����Ŀ˵��
�������Ź���� ��һ��c��ľŹ����У������ ����һ�� ����İ ‫������ڷ‬е ĸ � � � � 

�� ��������ʾ������Ҫ��ʵ�������� ���þŹ������Ϊ�� �������� ��ʾ����ʽ������Ĺ����ǣ�ÿ��ֻ����


ܽ

��ϡ��¡������ң����‫�ڵ‬һ������ƽ�Ƶ�� ��С�� ��ʵ����һ�������

�� ��������

������Ŀ����
�����Ź�������� ������еľ�������֮һ���������‫��ڣ‬c����������У��ţ�����ʣ�µ�û�зŵ���Ϊ�

�ÿ���ƶ�ֻ���Ǻ����‫ڵ‬Ŀ ������� ���������ij�ʼ̬‫����״‬һ


ͨ �н���������

‫����ת‬Ŀ�����У����� �������� ��������‫������ת‬

�� �������� �� ��������

�����Ź������У����������������‫����ת‬Ŀ�깲��}�ֿ��

‫ͬ�����ֲ�}�������ܣ‬ʱ��b��Ҳ�����������к�ż�����С����ǿ��
�һ��������е���������Ҵ�� �����һ��һά�����ʾ������ ���������Ǿ
�� �ʾ�ɣ������������������������������������У���� �
������������������ ������������
ܹ г�4�Ľ��ʽ���ǣ�

�ƣ��ƣ��٣������‫����أ‬Уƣ��‫�أ‬

��������һ������ǰ��������С����ĸ����Ϊ�����ż�����һ�ֽ ����ô������������Ǿ �� ����Ľ��

�ƣ�����������
�ƣ�����������
�ƣ�����������
�ƣ�����������
�ƣ�����������
�ƣ�����������
�ƣ�����������
�ƣ�����������
�٣�������������������������������������
�����٣�������ż��������������о����� �������Ľ��������4 �Ľ��

���������ŵĽ������� ���������ұ ��ŷ���

���㷨����

�����Ź�������� �����ǽ���� ���λ�ã�ֱ�u���Ŀ��λ��Ϊֹ�� � α �ʾ � � � ǣ �

�� ��������

����Ҫ��õ����ŵľ���Ҫʹ�ù����������Ź������������У����֣�Ҳ��� ǣ ��������������ŷ

������ �Ƿdz���ģ���ʹ�õĹ��������Ҫ��‫ס‬ÿһ�����������ʽ��Ҫ���������¼�Ļ��� �

ú���
ܶ ‫ڴ‬棬���ǰ��� ����ʵ� � � ѹ � �ʹ � ã ģ ‫ף‬ϣң���ʽ���棬

ѹ � � � �ʽ � � ÿ � � � � � � � ã � λ � �ʾ � � � � � � � � ǣ � � c � � � � � � � � � � ֽ‫��ڣ����ڣ‬Ķ���

�Ʊ�ʾ��ʽ���������������ã�λ��ʾ����ʹ����һ��С���ɾ��ǽ�����ʾλ��������Ȼ���ö��4�ģ����ֱ�ʾ���

��‫�ڵ‬λ�ã�� ����ãģ ‫ף‬ϣңõ�ʾ�ˡ�����λ� �������������룬�ȳ˷��‫�ٶ‬Ҫ � � 㡣�����˼�����4�洢

�����˽���������ɺ �����·����
��ṹ���£�

class cninegird
{
public:
struct placelist
{
dword place;
placelist* left;
placelist* right;
};
struct scanbuf
{
dword place;
int scanid;
};
struct pathlist
{
unsigned char path[9];
};

private:
placelist *m_pplacelist;
scanbuf *m_pscanbuf;
rect m_rresetbutton;
rect m_rautobutton;

public:
int m_ipathsize;
clock_t m_itime;
uint m_istepcount;
unsigned char m_itargetchess[9];
unsigned char m_ichess[9];
hwnd m_hclientwin;
pathlist *m_ppathlist;
bool m_bautorun;

private:
inline bool addtree(dword place , placelist*& parent);
void freetree(placelist*& parent);
inline void arraytodword(unsigned char *array , dword & data);
inline void dwordtoarray(dword data , unsigned char *array);
inline bool movechess(unsigned char *array , int way);
bool estimateuncoil(unsigned char *array);
void getpath(uint depth);

public:
void movechess(int way);
bool computefeel();
void activeshaw(hwnd hview);
void drawgird(hdc hdc , rect clientrect);
void drawchess(hdc hdc , rect clientrect);
void reset();
void onbutton(point pnt , hwnd hview);

public:
cninegird();
~cninegird();
};

������������������ʹ����vectorģ����random_shuffle����������4����������‫�������ݣ‬Ŀ��

����ʲô�����룺

void cninegird::reset()
{
if(m_bautorun) return;
vector vs;
int i;
for (i = 1 ; i < 9 ; i ++)
vs.push_back(i);
vs.push_back(0);
random_shuffle(vs.begin(), vs.end());
random_shuffle(vs.begin(), vs.end());
for ( i = 0 ; i < 9 ; i ++)
{
m_ichess[i] = vs[i];
}

if (!estimateuncoil(m_ichess))
{
unsigned char array[9] = {1,2,3,8,0,4,7,6,5};
memcpy(m_itargetchess , array , 9);
}
else
{
unsigned char array[9] = {1,2,3,4,5,6,7,8,0};
memcpy(m_itargetchess , array , 9);
}
m_istepcount = 0;
}

���ѹ����ʵ�֣�

inline void cninegird::arraytodword(unsigned char *array , dword& data)


{
unsigned char night = 0;
for ( int i = 0 ; i < 9 ; i ++)
{
if (array[i] == 8)
{
night = (unsigned char)i;
break;
}
}

array[night] = 0;
data = 0;
data = (dword)((dword)array[0] << 29 | (dword)array[1] << 26 |
(dword)array[2] << 23 | (dword)array[3] << 20 |
(dword)array[4] << 17 | (dword)array[5] << 14 |
(dword)array[6] << 11 | (dword)array[7] << 8 |
(dword)array[8] << 5 | night);

array[night] = 8;
}

��ѹ��ʱ��ѹ������ ����ѹ � � � 룺

inline void cninegird::dwordtoarray(dword data , unsigned char *array)


{
unsigned char chtem;
for ( int i = 0 ; i < 9 ; i ++)
{
chtem = (unsigned char)(data >> (32 - (i + 1) * 3) & 0x00000007);
array[i] = chtem;
}
chtem = (unsigned char)(data & 0x0000001f);
array[chtem] = 8;
}

�������‫)�ڿ‬չ����� �dz��Ĵ ������‫����ڱ‬ʱ��ʹ�õ �� ǣ ģ ‫ף‬ϣң�������


ͣ ÿһ����

���¼��һ�����������У�����С��������е����У������ʱ���ÿ���������ε���ʽ�Ƚ 켸

���ǣ δ η � � � � � � Ѽ � � � � � ѭ � � � � � õ � �ĺ � � � � � � �Ϊ � � j � � � � � ‫����ڲ‬ʱ��ͬ ʱ ���

������� �����������4�‫ٶ������ �ظ‬ȡ������������룺

inline bool cninegird::addtree(dword place , placelist*& parent)


{
if (parent == null)
{
parent = new placelist();
parent->left = parent->right = null;
parent->place = place;
return true;
}
if (parent->place == place)
return false;

if (parent->place > place)


{
return addtree(place , parent->right);
}
return addtree(place , parent->left);
}

���������������л���ż�����еĴ��룺

bool cninegird::estimateuncoil(unsigned char *array)


{
int sun = 0;
for ( int i = 0 ; i < 8 ; i ++)
{
for ( int j = 0 ; j < 9 ; j ++)
{
if (array[j] != 0)
{
if (array[j] == i +1 )
break;
if (array[j] < i + 1)
sun++;
}
}
}
if (sun % 2 == 0)
return true;
else
return false;
}

�����ƶ����ո�λ�Ĵ��� � ֻҪ � � � � � Ƿ � � � ƶ � � � � � � � � �

����ˣ������ƶ���ʱ�� ˳ �����һ���Dz����Ѿ���Ŀ����������4���û��ֹ��ƶ��Ǹ�����ʾ�õģ����룺

inline bool cninegird::movechess(unsigned char *array , int way)


{
int zero , chang;
bool moveok = false;
for ( zero = 0 ; zero < 9 ; zero ++)
{
if (array[zero] == 0)
break;
}
point pnt;
pnt.x = zero % 3;
pnt.y = int(zero / 3);
switch(way)
{
case 0 : //up
if (pnt.y + 1 < 3)
{
chang = (pnt.y + 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 1 : //down
if (pnt.y - 1 > -1)
{
chang = (pnt.y - 1) * 3 + pnt.x ;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 2 : //left
if (pnt.x + 1 < 3)
{
chang = pnt.y * 3 + pnt.x + 1;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
case 3 : //right
if (pnt.x - 1 > -1)
{
chang = pnt.y * 3 + pnt.x - 1;
array[zero] = array[chang];
array[chang] = 0;
moveok = true;
}
break;
}
if (moveok && !m_bautorun)
{
m_istepcount ++ ;

dword temp1 ,temp2;


arraytodword(array , temp1);
arraytodword(m_itargetchess , temp2);
if (temp1 == temp2)
{
messagebox(null , "���������ô�� ��!" , "^_^" , 0);
}
}
return moveok;
}
�������‫��ڽ‬й������ʱ� ���������

‫����� ���¼����������ڵ‬ˣ���� õ �Ŀ�����е�ʱ������ֻҪ � � � �� � � � � � � � �

�� õ ���������·���ˡ��ñ� m_ipathsize4��¼������
ܲ 庯����룺

void cninegird::getpath(uint depth)


{
int now = 0 , maxpos = 100 ;
uint parentid;
if (m_ppathlist != null)
{
delete[] m_ppathlist;
}
m_ppathlist = new pathlist[maxpos];
parentid = m_pscanbuf[depth].scanid;

dwordtoarray(m_pscanbuf[depth].place , m_ppathlist[++now].path);

while(parentid != -1)
{
if (now == maxpos)
{
maxpos += 10;
pathlist * temlist = new pathlist[maxpos];
memcpy(temlist , m_ppathlist , sizeof(pathlist) * (maxpos - 10));
delete[] m_ppathlist;
m_ppathlist = temlist;
}
dwordtoarray(m_pscanbuf[parentid].place , m_ppathlist[++now].path);
parentid = m_pscanbuf[parentid].scanid;
}
m_ipathsize = now;
}

������̬���е���ʾ��������ˣ�Ϊ���������м�ʱˢ�µĻ� ����һ��� �����Ҫ � � � �ˢ � µ �ʱ � � � ã ӣ � � 壨

� ɣΣ �
�����4� һ���
ͣ ̾ ����ˡ����룺

unsigned __stdcall movechessthread(lpvoid pparam)


{
cninegird * pgird = (cninegird *)pparam;
rect rect;
pgird->m_istepcount = 0;
::getclientrect(pgird->m_hclientwin , &rect);
for ( int i = pgird->m_ipathsize ; i > 0 ; i --)
{
memcpy(pgird->m_ichess , pgird->m_ppathlist[i].path , 9);
pgird->m_istepcount ++;
invalidaterect( pgird->m_hclientwin , &rect , false);
sleep(300);
}
char msg[100];
sprintf(msg , "^_^ ! � ��!\r\n��� ����ʱ%d ����" , pgird->m_itime);
messagebox(null , msg , "~_~" , 0);
pgird->m_bautorun = false;
return 0l;
}

����������һ��������� � �
���
� �õ
ȵ��� 飬����‫���ת‬ɣģ

‫ף‬ϣң�����Ŀ��
ͣ Ƚϣ��������ɣ����
ͬ ͬ ���һ����

�λ�ã���������������һ����ֱ��û�в������ˣ������� ������λ�õ���λ�ã�����ֱ���ҵ�Ŀ����Ϊֹ������

bool cninegird::computefeel()
{
unsigned char *array = m_ichess;
uint i;
const int maxsize = 362880;
unsigned char temparray[9];

dword target , fountain , parent , parentid = 0 , child = 1;


arraytodword(m_itargetchess , target);
arraytodword(array , fountain);
if (fountain == target)
{
return false;
}
if (m_pscanbuf != null)
{
delete[] m_pscanbuf;
}
m_pscanbuf = new scanbuf[maxsize];
addtree(fountain ,m_pplacelist);
m_pscanbuf[ 0 ].place = fountain;
m_pscanbuf[ 0 ].scanid = -1;
clock_t tim = clock();
while(parentid < maxsize && child < maxsize)
{
parent = m_pscanbuf[parentid].place;
for ( i = 0 ; i < 4 ; i ++)��// 0 :up , 1:down ,2:left,3:right
{
dwordtoarray(parent , temparray);
if (movechess(temparray,i)) //�Ƿ��ƶ��ɹ�
{
arraytodword(temparray , fountain);
if (addtree(fountain, m_pplacelist)) //����������
{
m_pscanbuf[ child ].place = fountain;
m_pscanbuf[ child ].scanid = parentid;
if (fountain == target) //�Ƿ��ҵ����
{
m_itime = clock() - tim;
getpath(child);//����·��
freetree(m_pplacelist);
delete[] m_pscanbuf;
m_pscanbuf = null;
return true;
}
child ++;
}
}
} // for i
parentid++;
}
m_itime = clock() - tim;

freetree(m_pplacelist);
delete[] m_pscanbuf;
m_pscanbuf = null;
return false;
}

Вам также может понравиться