思路:
用结构体存每一次的状态,然后去更新下一次;
分类讨论:
s -> n;
n -> s;
s -> m;
m -> s;
n -> m;
m -> n;
相似题目:
QWQ
代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <stack>
#define me memset
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N = 210;
const int null = 0x3f3f3f3f;
int s,n,m;
bool st[N][N][N];
struct Edge
{
int s,n,m,step;
}edge;
void bfs()
{
queue<Edge> q;
edge.s = s;
edge.n = 0;
edge.m = 0;
edge.step = 0;
st[edge.s][edge.n][edge.m] = true;
q.push(edge);
while(q.size())
{
auto t = q.front();
q.pop();
//cout<<"chushi------>"<<t.s<<' '<<t.n<<' '<<t.m<<endl;
if((t.n == t.m && t.s == 0) || (t.n == t.s && t.m == 0) || (t.m == t.s && t.n == 0))
{
// cout << t.s << " " << t.n << " " << t.m << endl;
cout << t.step << endl;
return ;
}
//s -> n;
if(t.n != n && t.s != 0)
{
Edge tt = t;
int idx = n - tt.n;
if(idx >= tt.s)
{
if(!st[0][tt.n + tt.s][tt.m])
{
st[0][tt.n + tt.s][tt.m] = true;
tt.n += tt.s;
tt.s = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[tt.s - idx][n][tt.m])
{
st[tt.s - idx][n][tt.m] = true;
tt.s -= idx;
tt.n = n;
tt.step ++;
q.push(tt);
}
}
// cout << "sn" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
//n -> s;
if(t.s != s && t.n != 0)
{
Edge tt = t;
int idx = s - tt.s;
if(idx >= tt.n)
{
if(!st[tt.s + tt.n][0][tt.m])
{
st[tt.s + tt.n][0][tt.m] = true;
tt.s += tt.n;
tt.n = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[s][tt.n - idx][tt.m])
{
st[s][tt.n - idx][tt.m] = true;
tt.n -= idx;
tt.s = s;
tt.step ++;
q.push(tt);
}
}
// cout << "ns" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
//s -> m;
if(t.m != m && t.s != 0)
{
Edge tt = t;
int idx = m - tt.m;
if(idx >= tt.s)
{
if(!st[0][tt.n][tt.m + tt.s])
{
st[0][tt.n][tt.m + tt.s] = true;
tt.m += tt.s;
tt.s = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[tt.s - idx][tt.n][m])
{
st[tt.s - idx][tt.n][m] = true;
tt.s -= idx;
tt.m = m;
tt.step ++;
q.push(tt);
}
}
// cout << "sm" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
//m -> s;
if(t.s != s && t.m != 0)
{
Edge tt = t;
int idx = s - tt.s;
if(idx >= tt.m)
{
if(!st[tt.s + tt.m][tt.n][0])
{
st[tt.s + tt.m][tt.n][0] = true;
tt.s += tt.m;
tt.m = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[s][tt.n][tt.m - idx])
{
st[s][tt.n][tt.m - idx] = true;
tt.m -= idx;
tt.s = s;
tt.step ++;
q.push(tt);
}
}
// cout << "ms" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
//n -> m;
if(t.m != m && t.n != 0)
{
Edge tt = t;
int idx = m - tt.m;
if(idx >= tt.n)
{
if(!st[tt.s][0][tt.m + tt.n])
{
st[tt.s][0][tt.m + tt.n] = true;
tt.m += tt.n;
tt.n = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[tt.s][tt.n - idx][m])
{
st[tt.s][tt.n - idx][m] = true;
tt.n -= idx;
tt.m = m;
tt.step ++;
q.push(tt);
}
}
// cout << "nm" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
//m -> n;
if(t.n != n && t.m != 0)
{
Edge tt = t;
int idx = n - tt.n;
if(idx >= tt.m)
{
if(!st[tt.s][tt.n + tt.m][0])
{
st[tt.s][tt.n + tt.m][0] = true;
tt.n += tt.m;
tt.m = 0;
tt.step ++;
q.push(tt);
}
}
else
{
if(!st[tt.s][n][tt.m - idx])
{
st[tt.s][n][tt.m - idx] = true;
tt.m -= idx;
tt.n = n;
tt.step ++;
q.push(tt);
}
}
// cout << "m n" << endl;
// cout << tt.s << " " << tt.n << " " << tt.m << endl;
}
}
puts("NO");
}
int main()
{
while(cin >> s >> n >> m)
{
if(s == 0 || n == 0 || m == 0) break;
if(s % 2 == 1)
{
puts("NO");
continue;
}
bfs();
me(st,0,sizeof st);
}
return 0;
}