C语言过河问题,编程!谢谢了,急!

有一个警察,抓了一个贼,来到河边要过河。当他们来到河边时,发现河水上涨,只能坐船过河,同时,河边有一对夫妻带着两个儿子和两个女儿也要过河。请你编程输出他们过河的步骤。
限定条件:
(1) 渡口只有一条船,一次只能渡两个人,而且只有大人才能够划船,到河对岸后需要一个人将船送回到河这边。
(2) 贼只能和警察在一块,因为当警察不在身边时,贼就会伤害那对夫妻及他们的孩子。
(3) 贼不敢逃跑。
(4) 当母亲不在女儿身边时,父亲会责骂女儿。
(5) 当父亲不在儿子身边时,结亲也会责骂儿子。

用深度优先遍历算法编程实现过河

你好,我来为你解答:
解法如下:
1.农夫带羊过去,自己回来
2.农夫带狼过去,带羊回来
3.农夫带白菜过去,自己回来
4.农夫带羊过去
全部安全过岸.

程序暂时没有时间做
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-26
#include <stdio.h>

#define MAX 100

typedef enum BOOL
{
FALSE = 0, TRUE = 1
}BOOL;

typedef union Items
{
struct
{
char boy : 1;
char girl : 1;
char father : 1;
char mother : 1;
char police : 1;
char thief : 1;
char : 0;
};
char c;
}*pItems, Items;

struct
{
Items item[MAX];
int boat[MAX];
int length;
}stack;

int Boat[2];
Items ItemMask[12];
char* msg[4] = { "comes back single.", "comes back together.",
"pasts the river single.", "past the river together." };
char* msgn[12] = { "father", "mother", "police", "thief",
"police and the thief", "police and the father",
"police and the mother", "police and the boy",
"police and the girl", "father and the boy",
"mother and the girl", "father and the mother"};

BOOL IsLegal ( Items item )
{
Items t1, t2, t3;
Items t4, t5, t6;
t1.c = 0, t2.c = 0, t3.c = 0;
t4.c = 0, t5.c = 0, t6.c = 0;

t1.girl = 1, t1.father = 1, t1.mother = 0;
t4.girl = 1, t4.father = 1, t4.mother = 1;
t2.boy = 1, t2.father = 0, t2.mother = 1;
t5.boy = 1, t5.father = 1, t5.mother = 1;
t3.thief = 1, t3.police = 0;
t6.thief = 1, t6.police = 1;

if (( t4.c & item.c ) == t1.c ) {
return FALSE;
}
if (( t5.c & item.c ) == t2.c ) {
return FALSE;
}
if ((( item.c & t6.c ) == t3.c ) && (( item.c ^ t3.c ) != 0)) {
return FALSE;
}
return TRUE;
}

BOOL IsInStack ( Items item, int boat )
{
int i = 0;
for ( i = 0; i < stack.length; ++i ) {
if (( item.c == stack.item[i].c ) && ( boat == stack.boat[i] )) {
return TRUE;
}
}
return FALSE;
}

BOOL IsStackFull ()
{
return stack.length >= MAX ? TRUE : FALSE;
}

BOOL Rule ( pItems itemL, pItems itemR, int boat, int i )
{
Items left, right;
if ( 0 == boat ) {
left.c = itemL->c;
right.c = itemR->c;
}
else {
left.c = itemR->c;
right.c = itemL->c;
}

if (( left.c & ItemMask[i].c ) != ItemMask[i].c ) {
return FALSE;
}
left.c ^= ItemMask[i].c;
right.c ^= ItemMask[i].c;

if ( 0 == boat ) {
itemL->c = left.c;
itemR->c = right.c;
}
else {
itemL->c = right.c;
itemR->c = left.c;
}
return TRUE;
}

BOOL PastRiver ( Items itemL, Items itemR, int boat )
{
Items newL, newR;
int i = 0, j = 0;

if ( itemL.c == 0 ) {
return TRUE;
}

for ( i = 0; i < 12; ++i ){
newL.c = itemL.c, newR.c = itemR.c;
if ( TRUE == Rule ( &newL, &newR, boat, i )) {
if (( TRUE == IsLegal (newL)) && ( TRUE == IsLegal (newR) )) {
if (( FALSE == IsInStack ( newL, boat )) && ( FALSE == IsStackFull() )) {
stack.item[stack.length].c = newL.c;
stack.boat[stack.length] = boat;
stack.length ++;
if ( TRUE == PastRiver ( newL, newR, Boat[boat] )) {
if (( 0 == boat ) && ( i < 4 )) {
j = 2;
}
if (( 0 == boat ) && ( i >= 4 )) {
j = 3;
}
if (( 1 == boat ) && ( i < 4 )) {
j = 0;
}
if (( 1 == boat ) && ( i >= 4 )) {
j = 1;
}
printf ( "The %s %s\n", msgn[i], msg[j] );
return TRUE;
}
}
}
}
}
return FALSE;
}

void Init()
{
int i = 0;
stack.length = 0;
Boat[0] = 1;
Boat[1] = 0;
for ( i = 0; i < 12; ++i ) {
ItemMask[i].c = 0;
}
ItemMask[0].father = 1;
ItemMask[1].mother = 1;
ItemMask[2].police = 1;
ItemMask[3].thief = 1;
ItemMask[4].police = 1, ItemMask[4].thief = 1;
ItemMask[5].police = 1, ItemMask[5].father = 1;
ItemMask[6].police = 1, ItemMask[6].mother = 1;
ItemMask[7].police = 1, ItemMask[7].boy = 1;
ItemMask[8].police = 1, ItemMask[8].girl = 1;
ItemMask[9].father = 1, ItemMask[9].boy = 1;
ItemMask[10].mother = 1, ItemMask[10].girl = 1;
ItemMask[11].father = 1, ItemMask[11].mother = 1;
}

int main ()
{
Items itemL, itemR;
itemL.c = 0, itemR.c = 0;
itemL.father = 1, itemL.mother = 1;
itemL.boy = 1, itemL.girl = 1;
itemL.police = 1, itemL.thief = 1;

Init();
stack.item[stack.length].c = itemL.c;
stack.boat[stack.length] = 1;
stack.length++;
if ( FALSE == PastRiver ( itemL, itemR, 0 )) {
printf ("BAD!!\n");
}
getchar();
return 0;
}本回答被提问者采纳
第2个回答  2013-03-26
这个好吧的很么.......
相似回答