第1关:农夫过河
任务描述
本关任务:一个农夫带着一只狼、一只羊和一棵白菜,身处河的南岸。他要把这些东西全部运到北岸。
问题是他面前只有一条小船,船小到只能容下他和一件物品,另外只有农夫能撑船。
请问农夫该采取什么方案才能将所有的东西运过河呢?
相关知识
为了完成本关任务,你需要掌握:1.队列的基本操作,2.广度优先搜索算法。
编程要求
根据提示,在右侧编辑器补充代码。不要删除原有的注释。
测试说明
平台会对你编写的代码进行测试:
测试输入:无
预期输出:
The reverse path is :
The location is : 15
The location is : 6
The location is : 14
The location is : 2
The location is : 11
The location is : 1
The location is : 9
The location is : 0
开始你的任务吧,祝你成功!
farmer-across-river/farmer.c
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
// -------- 补充代码(2) begin -----------------
int famer(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) == cabbage(location) && goat(location) != famer(location))
return 0;
if (goat(location) == wolf(location) && goat(location) != famer(location))
return 0;
return 1;
}
// -------- 补充代码(2) end -----------------
void farmerProblem() {
int movers, i, location, newlocation;
int t;
int route[16]; /*记录已考虑的状态路径*/
PSeqQueue moveTo; /*定义队列moveTo */
// -------- 补充代码(3) begin -----------------
moveTo = createEmptyQueue_seq();
enQueue_seq(moveTo, 0x00);
for (i = 0; i < 16; i++) route[i] = -1;
route[0] = 0;
while (!isEmptyQueue_seq(moveTo) && (route[15] == -1)) {
location = frontQueue_seq(moveTo);
deQueue_seq(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;
enQueue_seq(moveTo, newlocation);
}
}
}
// -------- 补充代码(3) end -----------------
/* 打印出路径 */
if (route[15] != -1) {
printf("The reverse path is:\n");
for (location = 15; location >= 0; location = route[location]) {
printf("The location is : %d\n", location);
if (location == 0) return;
}
}
else
printf("No solution.\n");
}
int main() {
farmerProblem();
return 0;
}
farmer-across-river/queue.h
#ifndef QUEUE_H
#define QUEUE_h
#define MAXNUM 20
typedef int DataType;
struct SeqQueue { /* 顺序队列类型定义 */
int f, r;
DataType q[MAXNUM];
};
typedef struct SeqQueue* PSeqQueue; /* 顺序队列类型的指针类型 */
PSeqQueue createEmptyQueue_seq(void) {
PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
if (paqu == NULL)
printf("Out of space!! \n");
else
paqu->f = paqu->r = 0;
return (paqu);
}
int isEmptyQueue_seq(PSeqQueue paqu) {
return paqu->f == paqu->r;
}
/* 在队列中插入一元素x */
void enQueue_seq(PSeqQueue paqu, DataType x) {
if ((paqu->r + 1) % MAXNUM == paqu->f)
printf("Full queue.\n");
else {
// 补充代码(1) begin-----------
paqu->q[paqu->r] = x;
paqu->r = (paqu->r + 1) % MAXNUM;
// 补充代码(1) end ------------
}
}
/* 删除队列头部元素 */
void deQueue_seq(PSeqQueue paqu) {
if (paqu->f == paqu->r)
printf("Empty Queue.\n");
else
paqu->f = (paqu->f + 1) % MAXNUM;
}
/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq(PSeqQueue paqu) {
return (paqu->q[paqu->f]);
}
#endif