关注
看不到我,看不到我~~~ 运行截图: 代码: #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
相关推荐
04-10 17:50
长沙师范学院 计算机类 点赞 评论 收藏
转发
投递特斯拉等公司10个岗位 >
点赞 评论 收藏
转发
牛客热帖
- 1... 想来字节技术实习,看我这篇就够了!——保姆级面经大放送1.8W
- 2... 外卖员面试经验1.7W
- 3... 25届第一份实习怎么找?1.4W
- 4... 【0429快问快答】99%牛油的疑惑解答(更新至38个问题1.1W
- 5... 0实习经验上岸字节,分享一下过程经验1.1W
- 6... 【奖】休息放松or学习提升,五一假期和牛牛一起“充充电”🔋1.0W
- 7... 准备去参加自己的婚礼8437
- 8... 美团后端日常实习一二面(已oc)7807
- 9... 【💰有奖征集】非技术岗位笔面经邀你来分享!攒人品时间到!5970
- 10... 阿里国际 笔试 04295030
正在热议
# 牛友的五一计划 #
18626次浏览 380人参与
# 晒一晒我的offer #
2831055次浏览 49975人参与
# 牛客帮帮团来啦!有问必答 #
400960次浏览 7836人参与
# 无实习如何秋招上岸 #
173394次浏览 2730人参与
# 如何看待offer收割机的行为 #
194688次浏览 2990人参与
# 如何一边实习一边秋招 #
201919次浏览 4009人参与
# 华为求职进展汇总 #
442808次浏览 4446人参与
# 春招别灰心,我们一人来一句鼓励 #
21565次浏览 313人参与
# 产品实习,你更倾向大公司or小公司 #
31285次浏览 491人参与
# 非技术岗薪资爆料 #
9008次浏览 187人参与
# 硬件人的春招flag #
14568次浏览 199人参与
# 女生做医疗销售有前景吗 #
3896次浏览 49人参与
# 字节跳动工作体验 #
54014次浏览 1579人参与
# 聊聊这家公司值得去吗 #
63771次浏览 1280人参与
# 第一次面试 #
17928次浏览 277人参与
# 在国企工作的人,躺平了吗? #
73035次浏览 883人参与
# 机械人,你的秋招第一份简历被谁挂了 #
27030次浏览 492人参与
# 来聊聊机械薪资天花板是哪家 #
22946次浏览 180人参与
# 你更愿意参加线上面试还是线下面试? #
6987次浏览 95人参与
# 如何KTV领导 #
7555次浏览 73人参与