当s为奇数时直接输出NO 否则容易超时。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<string.h>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<queue>
#define hash hashh
using namespace std;
typedef long long ll;
#define inf 1000000000+7
#define sp system("pause")
int vis[110][120][120];
class node
{
public:
int a[3],cot;
};
int b[3];
node pour(node &x, int from, int to)
{
node por = x;
if (!por.a[from])return x;
if (por.a[to] == b[to])return x;
if (b[to] - por.a[to] > por.a[from])
{
por.a[to] += por.a[from];
por.a[from] = 0;
return por;
}
else
{
por.a[from] -= (b[to] - por.a[to]);
por.a[to] = b[to];
return por;
}
}
bool cheak(node &x)
{
if (x.a[0] == 0 && x.a[1] == x.a[2])return true;
if (x.a[1] == 0 && x.a[0] == x.a[2])return true;
if (x.a[2] == 0 && x.a[1] == x.a[0])return true;
return false;
}
int main()
{
while (scanf("%d %d %d",&b[0],&b[1],&b[2])!=EOF)
{
if (b[0] == 0 && b[1] == 0 && b[2] == 0)break;
if (b[0] & 1)
{
printf("NO\n");
continue;
}
memset(vis, 0, sizeof vis);
queue<node>q;
node start;
start.cot = 1;
start.a[0] = b[0];
start.a[1] = 0;
start.a[2] = 0;
node in;
in = pour(start, 0, 1);
vis[start.a[0]][start.a[1]][start.a[2]] = 1;
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 0, 2);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
int ans = -1;
while (!q.empty())
{
start = q.front();
if (cheak(start))
{
ans = start.cot;
break;
}
q.pop();
start.cot++;
in = pour(start, 0, 2);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 0, 1);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 1, 0);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 1, 2);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 2, 1);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
in = pour(start, 2, 0);
if (!vis[in.a[0]][in.a[1]][in.a[2]])
{
q.push(in);
vis[in.a[0]][in.a[1]][in.a[2]] = 1;
}
}
if (ans == -1)
{
printf("NO\n");
}
else printf("%d\n", ans);
}
return 0;
}