这是《数据结构案例教程》上的,用了位运算,个人认为写的十分巧妙。
#include<stdio.h>
#include<stdlib.h>
void farmerProblem();
#define MAXQSIZE 100
typedef int ElemType;
typedef struct
{
ElemType data[MAXQSIZE];
int front,rear;
}SqQueue;
void Init_SqQueue(SqQueue *Q)
{
Q -> front = Q -> rear = 0;
}
int Empty_SqQueue(SqQueue *Q)
{
return(Q -> rear == Q ->front);
}
void In_SqQueue(SqQueue *Q,int e)
{
if(Q -> rear == MAXQSIZE)
{
return;
}
Q -> data[Q -> rear] = e;
Q -> rear += 1;
}
void Out_SqQueue(SqQueue *Q,int *e)
{
if(Q -> rear == Q -> front)
{
return;
}
*e = Q -> data[Q -> front];
Q -> front += 1;
}
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 locat
数据结构之农夫过河问题位运算解法

这篇博客探讨了如何利用位运算巧妙地解决《数据结构案例教程》中提出的农夫过河问题。通过深入分析,揭示了位运算在解决此类问题时的独特优势。
最低0.47元/天 解锁文章
5336

被折叠的 条评论
为什么被折叠?



