题目意思:
猴子定义了4个状态  空手移动,推箱子,爬箱子,摘香蕉 用 (w,x,y,z)描述
w定义了猴子位置
x为1表示猴子在箱子上,0表示不在箱子上
y表示箱子位置
z为1表示猴子摘到香蕉(结束),为0表示没有摘到香蕉(继续搜索)

目前仍有些bug

随机生产 猴子 箱子 香蕉的位置,通过BFS搜索并记录路径得出猴子的行走轨迹(因为是宽松搜,得到的就是最优解)

#include<iostream> #include<queue> #include<algorithm> #include<cstdlib> #include<ctime> #include<cstring> #include<cmath> #define INF 0x3f3f3f3f #define MAX 100000 using namespace std; int a,b,c;//a 猴子 b箱子 c香蕉 int dir[2][2] = {{0,-1},{0,1}}; struct Node{     int w,x,y,z;  int close; }; int pa[MAX]; int end; Node nodes[MAX]; int getDis(int x1, int x2){     return abs(x2-x1); } int step = 1; int flag = 1; void go(int w,int x, int y, int z)     {      printf("第%d步 :",step++);   if(x == 1 && z== 0){             printf("猴子推箱子到达位置%d\n",w);             printf("第%d步 :",step++);             printf("猴子在位置%d爬上箱子\n",w);         }         else if(x == 1 && z == 1){             printf("猴子在%d位置摘到香蕉\n",w);         }         else if(x == 0 && y != w){             printf("猴子空手到达%d位置\n",w);         }         else if(x == 0 && y == w && w != c){            if(flag){              if(a != b)                printf("猴子到达箱子位置%d\n",w);               else{                 printf("猴子到达箱子位置%d\n",b);       printf("第%d步 :",step++);                 printf("猴子推箱子到达位置%d\n",w);      }              flag = 0;      }      else        printf("猴子推箱子到达位置%d\n",w);         }         else if(x == 0 &&y == w && w == c){          printf("猴子推箱子到达位置%d\n",w);   }     }
void print(int e)     {         int w,x,y,z,close;         if(pa[e] != -1){             print(pa[e]);             Node node = nodes[e];             w = node.w;//猴子位置             x = node.x;//是否在箱子上             y = node.y;//箱子位置             z = node.z;//是否摘到香蕉             //printf("\t");                         go(w,x,y,z);         }     } void getBana()     {         int w,x,y,z;         int head, tail;         memset(pa,-1,sizeof(pa));         head = tail = 0;         printf("猴子,箱子,香蕉的初始位置分别为%d %d %d\n\n",a,b,c);         if(a == b ){          int s1 = 1;          if(b == c){           printf("第%d步 :",s1++);              printf("猴子在位置%d爬上箱子\n",a);              printf("第%d步 :",s1++);              printf("猴子在%d位置摘到香蕉\n",a);              return ;    }    if(b != c){     if(b < c){      while(b < c){       ++b;       printf("第%d步 :",s1++);       printf("猴子推箱子到达位置%d\n",b);      }     }     if(b > c){      while(b > c){       --b;       printf("第%d步 :",s1++);       printf("猴子推箱子到达位置%d\n",b);      }     }     return ;    }         }      if(b == c){//箱子初始位置与香蕉位置相同 不能往下走了       int s2 = 1;       if(b > a){        while(a < b){         ++a;         printf("第%d步 :",s2++);         printf("猴子空手到达%d\n",a);     }    }    else{     while(a > b){      --a;      printf("第%d步 :",s2++);      printf("猴子空手到达%d\n",a);     }    }    printf("第%d步 :",s2++);       printf("猴子在位置%d爬上箱子\n",a);       printf("第%d步 :",s2++);             printf("猴子在%d位置摘到香蕉\n",a);   }
        nodes[tail].w = a;         nodes[tail].x = 0;         nodes[tail].y = b;         nodes[tail].z = 0;         tail++;         while(head < tail){             Node node = nodes[head];             w = node.w;//猴子位置             x = node.x;//是否在箱子上             y = node.y;//箱子位置             z = node.z;//是否摘到香蕉             //go(w,x,y,z);             if(x == 1 && z == 1){                 end = head;                 break;             }             if(x == 1 && w == c){              pa[tail] = head;                 nodes[tail].w = w;                 nodes[tail].x = 1;                 nodes[tail].y = y;                 nodes[tail].z = 1;                 tail++;             }             if(x != 1){                 if(w == y){//不在箱子上 并且在同一位置                     for(int i = 0; i < 2; ++i) {                         int nw = w + dir[i][1];                         if(nw < 0) continue;                         if(w == y && getDis(c,nw) > getDis(c,w)) continue;                         pa[tail] = head;                         nodes[tail].w = nw;//往前推                         nodes[tail].x = 0;                         nodes[tail].y = nw;                         nodes[tail].z = 0;                         nodes[tail].close = 1;                         tail++;                         pa[tail] = head;                         nodes[tail].w = nw;//上箱子尝试摘                         nodes[tail].x = 1;                         nodes[tail].y = nw;                         nodes[tail].z = 0;                         tail++;
                    }                 }                 if(w != y){//不在箱子上  不在同一位置                     for(int i = 0; i < 2; ++i) {                         int nw = w + dir[i][1];                         if(nw < 0) continue;                          pa[tail] = head;                         nodes[tail].w = nw;//上箱子尝试摘                         nodes[tail].x = 0;                         nodes[tail].y = b;                         nodes[tail].z = 0;                         tail++;                     }                 }             }             head++;                    }            print(end);     }         int main()     {         ios::sync_with_stdio(false);         srand((unsigned)time(NULL));         a = rand()%(10-0)+1;         b = rand()%(10-0)+1;         c = rand()%(10-0)+1;         //cin >> a >> b >> c;         getBana();             return 0;     }
人工智能实验 猴子摘香蕉

相关文章: