农夫过河问题??C语言 运行无结果
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node *PNode;
struct Node
{
int info;
PNode link;
};
struct LinkQueue
{
PNode f;
PNode r;
};
typedef struct LinkQueue *PLinkQueue;
PLinkQueue Create_EmptyLinkQueue(void)
{
PLinkQueue p;
p=(PLinkQueue)malloc(sizeof(LinkQueue));
if(p!=NULL)
{
p->f=NULL;
p->r=NULL;
}
else printf("Out of space!\n");
return p;
}
void Enter_LinkQueue(PLinkQueue q,int x)
{
PNode p=(PNode)malloc(sizeof(Node));
if(p==NULL) printf("Out of space!\n");
else
{
p->info=x;
p->link=NULL;
if(q->f==NULL) q->f=p;
else q->r->link=p;
q->r=p;
}
}
void Delete_LinkQueue(PLinkQueue q)
{
PNode p;
if(q->f==NULL) printf("Empty queue.\n");
else
{
p=q->f;
q->f=q->f->link;
free(p);
}
}
int Front_LinkQueue(PLinkQueue p)
{
if(p->f==NULL) printf("Empty queue.\n");
else return(p->f->info);
}
int IsEmpty_LinkQueue(PLinkQueue p)
{
return(p->f!=NULL);
}
int farmer(int location)
{
return(0!=location&0x08);
}
int wolf(int location)
{
return(0!=location&0x04);
}
int cabbage(int location)
{
return(0!=location&0x02);
}
int goat(int location)
{
return(0!=location&0x01);
}
int safe(int location)
{
if(goat(location)==wolf(location)&&goat(location)!=farmer(location))
return 0;
if(cabbage(location)==goat(location)&&cabbage(location)!=farmer(location))
return 0;
return 1;
}
void FarmerProblem()
{
int i,movers,location,newlocation;
int route[16];
PLinkQueue moveto;
moveto=Create_EmptyLinkQueue();
Enter_LinkQueue(moveto,0x00);
printf("当前入队状态是%d\n",0x00);
for(i=0;i<16;i++)
route[i]=-1;
route[0]=0;
while(!IsEmpty_LinkQueue(moveto)&&(route[15]==-1))
{
location=Front_LinkQueue(moveto);
printf("当前出队的状态是%d\n",location);
Delete_LinkQueue(moveto);
for(movers=1;movers<=8;movers<<=1)
{
if((0!=(location&&0x08))==(0!=(location&&movers)))
{
newlocation=location^(0x08|movers);
if(safe(newlocation)&&route[newlocation]==-1)
{
route[newlocation]=location;
Enter_LinkQueue(moveto,newlocation);
printf("当前入队的状态是%d\n",newlocation);
}
}
}
}
if(route[15]!=-1)
{
printf("反路径为:\n");
for (location=15;location>=0;location=route[location])
{
printf("The location is : %d\n", location);
if (location == 0) exit(0);
}
}
else printf("问题无解.\n");
}
int main()
{
FarmerProblem();
return 0;
}
运行显示"当前入队状态是0,问题无解."
求教???为什么运行无结果???谢谢!
#include<stdlib.h>
struct Node;
typedef struct Node *PNode;
struct Node
{
int info;
PNode link;
};
struct LinkQueue
{
PNode f;
PNode r;
};
typedef struct LinkQueue *PLinkQueue;
PLinkQueue Create_EmptyLinkQueue(void)
{
PLinkQueue p;
p=(PLinkQueue)malloc(sizeof(LinkQueue));
if(p!=NULL)
{
p->f=NULL;
p->r=NULL;
}
else printf("Out of space!\n");
return p;
}
void Enter_LinkQueue(PLinkQueue q,int x)
{
PNode p=(PNode)malloc(sizeof(Node));
if(p==NULL) printf("Out of space!\n");
else
{
p->info=x;
p->link=NULL;
if(q->f==NULL) q->f=p;
else q->r->link=p;
q->r=p;
}
}
void Delete_LinkQueue(PLinkQueue q)
{
PNode p;
if(q->f==NULL) printf("Empty queue.\n");
else
{
p=q->f;
q->f=q->f->link;
free(p);
}
}
int Front_LinkQueue(PLinkQueue p)
{
if(p->f==NULL) printf("Empty queue.\n");
else return(p->f->info);
}
int IsEmpty_LinkQueue(PLinkQueue p)
{
return(p->f!=NULL);
}
int farmer(int location)
{
return(0!=location&0x08);
}
int wolf(int location)
{
return(0!=location&0x04);
}
int cabbage(int location)
{
return(0!=location&0x02);
}
int goat(int location)
{
return(0!=location&0x01);
}
int safe(int location)
{
if(goat(location)==wolf(location)&&goat(location)!=farmer(location))
return 0;
if(cabbage(location)==goat(location)&&cabbage(location)!=farmer(location))
return 0;
return 1;
}
void FarmerProblem()
{
int i,movers,location,newlocation;
int route[16];
PLinkQueue moveto;
moveto=Create_EmptyLinkQueue();
Enter_LinkQueue(moveto,0x00);
printf("当前入队状态是%d\n",0x00);
for(i=0;i<16;i++)
route[i]=-1;
route[0]=0;
while(!IsEmpty_LinkQueue(moveto)&&(route[15]==-1))
{
location=Front_LinkQueue(moveto);
printf("当前出队的状态是%d\n",location);
Delete_LinkQueue(moveto);
for(movers=1;movers<=8;movers<<=1)
{
if((0!=(location&&0x08))==(0!=(location&&movers)))
{
newlocation=location^(0x08|movers);
if(safe(newlocation)&&route[newlocation]==-1)
{
route[newlocation]=location;
Enter_LinkQueue(moveto,newlocation);
printf("当前入队的状态是%d\n",newlocation);
}
}
}
}
if(route[15]!=-1)
{
printf("反路径为:\n");
for (location=15;location>=0;location=route[location])
{
printf("The location is : %d\n", location);
if (location == 0) exit(0);
}
}
else printf("问题无解.\n");
}
int main()
{
FarmerProblem();
return 0;
}
运行显示"当前入队状态是0,问题无解."
求教???为什么运行无结果???谢谢!
...全文
相关推荐
农夫过河问题(C语言)设计
经典的农夫过河问题。用1代表狼,2代表羊,3代表白菜。则在河的某一岸边,物体的分布有8种情况:当两物体在一起并且它们的代码之和为3或5时,将导致相克的情况出现。设计c语言算法实现过河,并将结果打印
农夫过河问题C语言实现
农夫过河问题在数据结构中有很高的地位,几乎每一本数据结构书都会涉及这个问题
农夫过河问题的求解(C语言)
找了好久,终于找到了一个C语言版的农夫过河问题的解法,认真看就看的懂,写的不错!简介明了!
c语言农夫过河思维游戏程序及源代码
C语言农夫过河思维游戏程序及源代码便是这样一个例子,它不仅向我们展示了如何用C语言解决经典的农夫过河问题,还为编程初学者提供了一个动手实践和逻辑思维训练的平台。
农夫过河问题 数据结构
博客详细介绍了农夫过河问题的编程实现,包括自定义数据类型、类设计、深度优先搜索算法以及顺序表操作。通过枚举、自定义串类、类型转换、下标运算符重载等技术手段,确保农夫、白菜、羊和狼安全过河。同时,博客还


新手乐园
