巨佬的数学解法
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int a,b,c;
while(cin>>a>>b>>c&&(a&&b&&c))
{
a/=gcd(b,c);
if(a&1)
cout<<"NO"<<endl;
else
cout<<a-1<<endl;
}
return 0;
}
蒟蒻的BFS写法
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
//多组输入的初始化
//判断是否需要LL
#define endl '\n'
#define ms(x, y) memset(x, y, sizeof x)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int a, b, c;
int w[3];
bool st[110][110][110];
struct node
{
int v[3];//因为用数组存储便于枚举
int step;
};
void bfs()
{
ms(st, false);
queue<node> q;
q.push({w[0], 0, 0, 0});
st[w[0]][0][0] = true;
while(q.size())
{
node t = q.front();
q.pop();
if(t.v[0] == t.v[2] && t.v[1] == 0)
{
cout << t.step << endl;
return;
}
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if(i != j)
{
node temp = t;
int minn = min(temp.v[i], w[j] - temp.v[j]);//从i倒水到j
temp.v[i] -= minn, temp.v[j] += minn;
if(!st[temp.v[0]][temp.v[1]][temp.v[2]])
{
temp.step ++;
q.push(temp);
st[temp.v[0]][temp.v[1]][temp.v[2]] = true;
}
}
}
}
cout << "NO" << endl;
}
int main()
{
while(cin >> w[0] >> w[1] >> w[2])
{
if(w[0] == 0 && w[1] == 0 && w[2] == 0) break;
if(w[1] > w[2]) swap(w[1], w[2]);
if(w[0] % 2 == 1)//可乐是奇数
cout << "NO" << endl;
else bfs();
}
return 0;
}
orz!