HDOJ1495 非常可乐(BFS)

在这里插入图片描述

Sample Input 示例输入
7 4 3
4 1 3
0 0 0

Sample Output 示例输出
NO
3

先贴个大佬的代码吧…看到题我只能想到bfs 完全不知道用数论咋做…
https://blog.csdn.net/queque_heiya/article/details/103095581

我的代码(考虑所有情况BFS):

注意
在这里插入图片描述

#include <iostream>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;

struct triplet{
	int first = 0;
	int second = 0;
	int third = 0;
	int step = 0;
};

int main()
{
	int S,N,M;
	while(scanf("%d %d %d",&S,&N,&M)!=EOF)
	{
		if(S==0&&N==0&&M==0)
			break;
		queue<triplet> tempQueue;
		int visited[S+1][N+1][M+1];
		memset(visited,0,(S+1)*(N+1)*(M+1)*sizeof(int));
		triplet header;
		header.first = S;
		bool found = false;
		tempQueue.push(header);
		visited[S][0][0] = 1;
		while(!tempQueue.empty())
		{
			triplet tempTriplet = tempQueue.front();
			tempQueue.pop();
			// 检查是否达到要求
			if(tempTriplet.first==0 && tempTriplet.second == tempTriplet.third)
			{
				cout << tempTriplet.step<<endl;
				found = true;
				break;
			}
			else if(tempTriplet.second==0 && tempTriplet.first == tempTriplet.third)
			{
				cout << tempTriplet.step<<endl;
				found = true;
				break;
			}
			else if(tempTriplet.third==0 && tempTriplet.second == tempTriplet.first)
			{
				cout << tempTriplet.step<<endl;
				found = true;
				break;
			}
			// 从第一个杯子里向外倒
			if(tempTriplet.first!=0)
			{
				// 往第二个里倒
				if(tempTriplet.second!=N)
				{
					if(N-tempTriplet.second >= tempTriplet.first)
					{
						if(visited[0][tempTriplet.first+tempTriplet.second][tempTriplet.third]==0)
						{
							visited[0][tempTriplet.first+tempTriplet.second][tempTriplet.third] = 1;
							triplet pushTriplet;
							pushTriplet.second = tempTriplet.first+tempTriplet.second;
							pushTriplet.third = tempTriplet.third;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
					else
					{
						if(visited[tempTriplet.first-(N-tempTriplet.second)][N][tempTriplet.third]==0)
						{
							visited[tempTriplet.first-(N-tempTriplet.second)][N][tempTriplet.third] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first-(N-tempTriplet.second);
							pushTriplet.second = N;
							pushTriplet.third = tempTriplet.third;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}	
				}
				
				// 往第三个里倒
				if(tempTriplet.third!=M)
				{
					if(M-tempTriplet.third >= tempTriplet.first)
					{
						if(visited[0][tempTriplet.second][tempTriplet.third+tempTriplet.first]==0)
						{
							visited[0][tempTriplet.second][tempTriplet.third+tempTriplet.first] = 1;
							triplet pushTriplet;
							pushTriplet.second = tempTriplet.second;
							pushTriplet.third = tempTriplet.third+tempTriplet.first;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
					else
					{
						if(visited[tempTriplet.first-(M-tempTriplet.third)][tempTriplet.second][M]==0)
						{
							visited[tempTriplet.first-(M-tempTriplet.third)][tempTriplet.second][M] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first-(M-tempTriplet.third);
							pushTriplet.second = tempTriplet.second;
							pushTriplet.third = M;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
				} 
			}
			// 从第二个杯子里向外倒
			if(tempTriplet.second!=0)
			{
				// 往第一个里倒
				if(visited[tempTriplet.first+tempTriplet.second][0][tempTriplet.third]==0)
				{
					visited[tempTriplet.first+tempTriplet.second][0][tempTriplet.third] = 1;
					triplet pushTriplet;
					pushTriplet.first = tempTriplet.first+tempTriplet.second;
					pushTriplet.third = tempTriplet.third;
					pushTriplet.step = tempTriplet.step+1;
					tempQueue.push(pushTriplet);
				}
				
				// 往第三个里倒
				if(tempTriplet.third!=M)
				{
					if(M-tempTriplet.third >= tempTriplet.second)
					{
						if(visited[tempTriplet.first][0][tempTriplet.third+tempTriplet.second]==0)
						{
							visited[tempTriplet.first][0][tempTriplet.third+tempTriplet.second] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first;
							pushTriplet.third = tempTriplet.third+tempTriplet.second;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
					else
					{
						if(visited[tempTriplet.first][tempTriplet.second-(M-tempTriplet.third)][M]==0)
						{
							visited[tempTriplet.first][tempTriplet.second-(M-tempTriplet.third)][M] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first;
							pushTriplet.second = tempTriplet.second-(M-tempTriplet.third);
							pushTriplet.third = M;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
				} 
			}
			// 从第三个杯子里向外倒
			if(tempTriplet.third!=0)
			{
				// 往第一个里倒
				if(visited[tempTriplet.first+tempTriplet.third][tempTriplet.second][0]==0)
				{
					visited[tempTriplet.first+tempTriplet.third][tempTriplet.second][0] = 1;
					triplet pushTriplet;
					pushTriplet.first = tempTriplet.first+tempTriplet.third;
					pushTriplet.second = tempTriplet.second;
					pushTriplet.step = tempTriplet.step+1;
					tempQueue.push(pushTriplet);
				}
				
				// 往第二个里倒
				if(tempTriplet.second!=N)
				{
					if(N-tempTriplet.second >= tempTriplet.third)
					{
						if(visited[tempTriplet.first][tempTriplet.third+tempTriplet.second][0]==0)
						{
							visited[tempTriplet.first][tempTriplet.third+tempTriplet.second][0] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first;
							pushTriplet.second = tempTriplet.third+tempTriplet.second;
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
					else
					{
						if(visited[tempTriplet.first][N][tempTriplet.third-(N-tempTriplet.second)]==0)
						{
							visited[tempTriplet.first][N][tempTriplet.third-(N-tempTriplet.second)] = 1;
							triplet pushTriplet;
							pushTriplet.first = tempTriplet.first;
							pushTriplet.second = N;
							pushTriplet.third = tempTriplet.third-(N-tempTriplet.second);
							pushTriplet.step = tempTriplet.step+1;
							tempQueue.push(pushTriplet);
						}
					}
				} 
			}
		}
		if(!found)
			cout << "NO" <<endl;
	}
	return 0;
} 

感觉写得太麻烦了…程序长的一批… 但是最后提交之后的内存时间占用看起来都不错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值