第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;
}本回答被提问者采纳