看不到我,看不到我~~~ 运行截图: 代码: #include <iostream> #include <bits/stdc++.h> using namespace std; int step[10][10][5][6];//每个20斤酒桶最多倒出9斤,所以剩下11斤无用,状态就是0~9,所以共有10×10×4×5 = 2000种状态 int full[4] = {9,9,4,5}; //满状态 bool finish; struct now {     int barrel[4]; }; stack<struct now> que; stack<struct now> sta; void dfs(struct now now_step)//dfs {     struct now next_step;     if(finish == true)         return;     sta.push(now_step);     if(now_step.barrel[2] == 2 && now_step.barrel[3] == 2)//找到     {         step[now_step.barrel[0]][now_step.barrel[1]][now_step.barrel[2]][now_step.barrel[3]] = 1;         finish = true;         return;     }     for(int i = 0;i < 4; i++)//所有情况     {         for(int j = 0;j < 4;j++)         {             if(i == j)                 continue;             next_step.barrel[0] = now_step.barrel[0];             next_step.barrel[1] = now_step.barrel[1];             next_step.barrel[2] = now_step.barrel[2];             next_step.barrel[3] = now_step.barrel[3];             if(now_step.barrel[i] + now_step.barrel[j] >= full[i])             {                 next_step.barrel[i] = full[i];                 next_step.barrel[j] = now_step.barrel[i] + now_step.barrel[j] - full[i];             }             else             {                 next_step.barrel[i] = now_step.barrel[i] + now_step.barrel[j];                 next_step.barrel[j] = 0;             }             if(step[next_step.barrel[0]][next_step.barrel[1]][next_step.barrel[2]][next_step.barrel[3]] == 0)//剪枝             {                 step[next_step.barrel[0]][next_step.barrel[1]][next_step.barrel[2]][next_step.barrel[3]] = 1;                 step[next_step.barrel[1]][next_step.barrel[0]][next_step.barrel[2]][next_step.barrel[3]] = 1;                 dfs(next_step);             }         }     }     if(finish == false)//清栈         sta.pop(); } int main() {     struct now start;     int foot = 0;     start.barrel[0] = 9;     start.barrel[1] = 9;     start.barrel[2] = 0;     start.barrel[3] = 0;     for(int i =0 ;i < 10;i++)         for(int j =0 ;j < 10;j++)             for(int k =0 ;k < 5;k++)                 for(int l =0 ;l < 6;l++)                     step[i][j][k][l] = 0;     step[9][9][0][0] = 1;     finish = false;     dfs(start);     while(!sta.empty())     {         que.push(sta.top());         sta.pop();     }     printf("          桶1  桶2  桶3  桶4 \n");     while(!que.empty())     {         if(foot == 0)             printf("初  始:  ");         else             printf("第%2d步:  ",foot);         printf("  %d    %d    %d    %d\n",que.top().barrel[0] + 11,que.top().barrel[1] + 11 ,que.top().barrel[2],que.top().barrel[3]);         foot++;         que.pop();     }     return 0; }
1 1

相关推荐

点赞 评论 收藏
转发
牛客网
牛客企业服务