过河问题请教,代码输出结果存在问题
过河问题,有警察,小偷,爸爸,儿子,妈妈,女儿要过河。限定条件:
(1) 渡口只有一条船,一次只能渡两个人,而且只有大人才能够划船,到河对岸后需要一个人将船送回到河这边。
(2) 贼只能和警察在一块,因为当警察不在身边时,贼就会伤害那对夫妻及他们的孩子。
(3) 贼不敢逃跑。
(4) 当母亲不在女儿身边时,父亲会责骂女儿。
(5) 当父亲不在儿子身边时,结亲也会责骂儿子。
以下是用队列实现过河问题,但是输出结果有问题,请各位提出问题所在
#include "stdio.h"
#include "stdlib.h"
#define MAX 200
typedef struct stu
{
int f,r;
int num[MAX];
}*Linklist,Lnode;
Linklist cre()
{
Linklist head;
head=(Linklist)malloc(sizeof(Lnode));
if(head!=NULL)
head->f=head->r=0;
return head;
}
insert(Linklist head,int no)
{
if(head->f==(head->r+1)%MAX)
return;
else
{
head->num[head->r]=no;
head->r++;
}
}
int isempty(Linklist head)
{
return (head->f==head->r);
}
int getfont(Linklist head)
{
return head->num[head->f];
}
del(Linklist head)
{
head->f=(head->f+1)%MAX;
}
int police(int location) {
return 0 != (location & 0x32);
}
int chief(int location) {
return 0 != (location & 0x16);
}
int father(int location) {
return 0 != (location & 0x08);
}
int son(int location) {
return 0 != (location & 0x04);
}
int monther(int location) {
return 0 != (location & 0x02);
}
int girl(int location) {
return 0 !=(location & 0x01);
}
int safe(int location) {
if ((girl(location) == father(location)) &&
(girl(location) != monther(location)) )
return 0;
if ((son(location) == monther(location)) &&
(son(location) != father(location)))
if (((chief(location) == monther(location))||(chief(location) == father(location))
||(chief(location) == son(location))||(chief(location) == girl(location)))&&
(chief(location) != police(location)))
return 0;
return 1;
}
passriver()
{
Linklist ptr;
ptr=cre();
int node[64];
int i,st,new_st;
for(i=0;i<64;i++)
node[i]=-1;
node[0]=0;
insert(ptr,0x00);
int mover;
int a[3]={0x02,0x08,0x32};
while(!isempty(ptr)&&node[63]==-1)
{
for(int i=0;i<3;i++)
{
st=getfont(ptr);
del(ptr);
for(mover=1;mover<=32;mover<<=1)
{
if((0!=(st&a[i]))==(0!=(st&mover)))
{
new_st=st^(mover|a[i]);
if(safe(new_st)&&node[new_st]==-1)
{
node[new_st]=st;
insert(ptr,new_st);
}
}
}
}
}
if(node[63]!=-1)
{
int k;
for(k=63;k>=0;k=node[k])
{
printf("%d\n",k);
if(k==0)
return;
}
}
}
main()
{
passriver();
}