Code of the farmer crossing the river

Source: Internet
Author: User

 

Code

/* Algorithm for Solving the farmer's cross-river problem using a queue */

# Include <stdio. h>
# Include <stdlib. h>

# Define MAXNUM 20

Typedef int DataType;
Struct SeqQueue {/* sequence queue type definition */
Int f, r;
DataType q [MAXNUM];
};

Typedef struct SeqQueue * PSeqQueue;/* pointer type of the ordered queue type */

PSeqQueue createEmptyQueue_seq (void ){
Required qqueue paqu = (required qqueue) malloc (sizeof (struct SeqQueue ));
If (paqu = NULL)
Printf ("Out of space !! \ N ");
Else
Paqu-> f = paqu-> r = 0;
Return (paqu );
}

Int isEmptyQueue_seq (queue qqueue paqu ){
Return paqu-> f = paqu-> r;
}
/* Insert an element x into the queue */
Void enQueue_seq (PSeqQueue paqu, DataType x ){
If (paqu-> r + 1) % MAXNUM = paqu-> f)
Printf ("Full queue. \ n ");
Else {
Paqu-> q [Paqu-> r] = X;
Paqu-> r = (Paqu-> r + 1) % maxnum;
}
}

/* Delete the queue Header element */
Void dequeue_seq (queue qqueue Paqu ){
If (Paqu-> F = Paqu-> r)
Printf ("Empty queue. \ n ");
Else
Paqu-> F = (Paqu-> F + 1) % maxnum;
}

/* For non-empty queues, locate the queue Header element */
Datatype frontqueue_seq (pseqqueue Paqu ){
Return (paqu-> q [paqu-> f]);
}

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 );
}

/* If the status is secure, true is returned */
Int safe (int location ){
/* Goat cabbage */
If (goat (location) = cabbage (location ))&&
(Goat (location )! = Farmer (location )))
Return 0;
/* Wolf eats sheep */
If (goat (location) = wolf (location ))&&
(Goat (location )! = Farmer (location )))
Return 0;
Return 1;/* other statuses are secure */
}

Void farmerProblem (){
Int movers, I, location, newlocation;
Int route [16];/* record the state path that has been considered */
PSeqQueue moveTo;
/* Prepare initial values */
MoveTo = createEmptyQueue_seq ();
EnQueue_seq (moveTo, 0x00 );
For (I = 0; I <16; I ++) route [I] =-1;
Route [0] = 0;

/* Start to move */
While (! IsEmptyQueue_seq (moveTo) & (route [15] =-1 )){
/* Get the current status */
Location = frontQueue_seq (moveTo );
DeQueue_seq (moveTo );
For (movers = 1; movers <= 8; movers <= 1 ){
/* The farmer is always moving, and only things on the same side of the farmer can be moved with the farmer */
If (0! = (Location & 0x08) = (0! = (Location & movers ))){
Newlocation = location ^ (0x08 | movers );
If (safe (newlocation) & (route [newlocation] =-1 )){
Route [newlocation] = location;
EnQueue_seq (moveTo, newlocation );
}
}
}
}

/* Print the output path */
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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.