C++農夫が川を渡る問題

2892 ワード

/**       
 *
 *          C      ,   8                 ,1  、0   
 *      4        
 *        (1:  ,0:  )
 *         (1:  ,0:  )
 *      ?? (1:  ,0:  )
 *        (1:  ,0:  )
 *           (         1000   &   , 1    , 0     )
 *       (        ,        ,              )
 *                  ,      ,        push     
 *         ,           ,0-15  
 *
 */
#include 
#include 
#include "SqQueue.h"
#define BIT(x) (1<
#include 

using namespace std;

//     。    。     ,     
enum Status{
    FARMER  = BIT(3),   //  
    WOLF    = BIT(2),   // 
    GOAT    = BIT(1),   // 
    CABBAGE = BIT(0)    //  
};

//farmer   
int farmer(int location){
    if((location&FARMER) != 0)
        return 1;
    return 0;
}

//wolf   
int wolf(int location){
    if((location&WOLF) != 0)
        return 1;
    return 0;
}

//goat   
int goat(int location){
    if((location&GOAT) != 0)
        return 1;
    return 0;
}

//cabbage   
int cabbage(int location){
    if((location&CABBAGE) != 0)
        return 1;
    return 0;
}

int isSafe(int location){
    if((wolf(location) == goat(location)) && (farmer(location) != goat(location)))
        return 0;
    if((cabbage(location) == goat(location)) && (farmer(location) != goat(location)))
        return 0;
    return 1;
}

void farmer_location(int location){
    if(farmer(location) == 1){
        printf("      
"); }else{ printf("
"); } } void farmer_acrry(int currStatus, int afterAtatus){ int status = currStatus^afterAtatus; switch(status){ case 12: printf("
"); break; case 10: printf("
"); break; case 9: printf("
"); break; default: printf("
"); break; } } void farmer_cross_river(){ // std::vector visited(16, false); SqQueue q; // int arr[16]; // , -1 for(int i = 0; i < 16; i++){ arr[i] = -1; } int location; //int 4 q.push(0); arr[0] = 0; // int newLocation; // // visited[0] = true; while( (!q.isEmpty()) && (arr[15] == -1)){ // , location = q.front(); q.pop(); // 、 、 、 for(int allStatus = 1; allStatus <= 8; allStatus <<= 1){ // , if((farmer(location)!=0) == ((allStatus&location)!=0)){ // , , newLocation = location^( FARMER|allStatus ); // if(isSafe(newLocation) && (arr[newLocation]==-1)){ cout <