洛谷—P1215母亲的牛奶(深度优先搜索\记录路径)

在这里插入图片描述
解题思路:
这道题目思路很明确,肯定是要用深搜的,由于不同的桶之间,这里每次决策有6条可能的路径,但是要记录采取决策后每个桶的牛奶数量是否已经访问过,如果访问过,则不再重复访问。

#include<iostream>
#include<algorithm>
using namespace std;
bool visit[25][25][25];
int ans[25];
int a, b, c,count;
void pull(int& x, int& y,int b)
{
	if (x + y <= b) {
		y += x;
		x = 0;
	}
	else
	{
		x -= (b - y);
		y = b;
	}
}
void dfs(int x, int y, int z,int depth)
{
	for (int i = 0; i < 6; ++i)
	{
		int tempx = x, tempy = y, tempz = z;
		if (i == 0&&tempz!=0&&tempx!=a) pull(tempz, tempx, a);
		else if (i == 1&&tempz!=0&&tempy!=b) pull(tempz, tempy, b);
		else if (i == 2&&tempy!=0&&tempz!=c) pull(tempy, tempz, c);
		else if (i == 3&&tempy!=0&&tempx!=a) pull(tempy, tempx, a);
		else if (i == 4&&tempx!=0&&tempy!=b) pull(tempx, tempy, b);
		else if (i == 5&&tempx!=0&&tempz!=c) pull(tempx, tempz, c);
		if (!visit[tempx][tempy][tempz])
		{
			visit[tempx][tempy][tempz] = true;
			dfs(tempx, tempy, tempz,depth+1);
		}
	}
}
int main()
{
	cin >> a >> b >> c;
	visit[0][0][c] = true;
	dfs(0, 0, c,0);
	for (int i = 0; i <= c; ++i)
		for (int j = 0; j <= b; ++j)
			if (visit[0][j][i])
				cout << i << ' ';
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值