| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:过河问题请教,代码输出结果存在问题
只看楼主 加入收藏
hjselena
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-31
收藏
 问题点数:0 回复次数:2 
过河问题请教,代码输出结果存在问题
过河问题,有警察,小偷,爸爸,儿子,妈妈,女儿要过河。
限定条件:
(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();
}
搜索更多相关主题的帖子: 过河 代码 结果 输出 
2008-09-25 10:37
hgrhgy
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-9-22
收藏
得分:0 
麻烦写上些注释- -!
2008-09-25 13:58
hjselena
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-31
收藏
得分:0 
/******************************************
code详细注释如下,0x32表示警察,0x16表示小偷,0x08表示爸爸,0x04表示儿子,0x02表示妈妈,0x01表示女儿。初始为0x00,最后状态为63,即00111111。
******************************************/
#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) {//得到警察的位置,河南岸为1,河北岸为0
    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)) )  //女儿与爸爸同一边,并且妈妈不在时,返回0
        return 0;
    if ((son(location) == monther(location)) &&  
          (son(location) != father(location)))  //儿子与妈妈同一边,并且爸爸不在时返回0
    if (((chief(location) == monther(location))||(chief(location) == father(location))
        ||(chief(location) == son(location))||(chief(location) == girl(location)))&&  
          (chief(location) != police(location)))  //小偷的安全状态,小偷与除警察外任意人在一边警察不在返回0

        return 0;
    return 1;   
}
passriver()
{
    Linklist ptr;
    ptr=cre();
    int node[64];//0x00到63共64种状态
    int i,st,new_st;
    for(i=0;i<64;i++)
        node[i]=-1;//初始其他状态赋-1;
    node[0]=0;//初始状态赋0x00;
    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();
}
2008-09-25 14:37
快速回复:过河问题请教,代码输出结果存在问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014112 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved