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;
}
感觉写得太麻烦了…程序长的一批… 但是最后提交之后的内存时间占用看起来都不错