题目:http://acm.hdu.edu.cn/showproblem.php?pid=1495
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 107;
int s, n, m;
int vis[MAXN][MAXN][MAXN];
int min_step = INF;
struct node
{
int z, x, y, step;
node(){}
node(int _z, int _x, int _y ,int _step):z(_z), x(_x), y(_y), step(_step){}
};
void bfs()
{
vis[s][0][0] = 1;
queue<node> q;
q.push(node(s, 0, 0, 0));
while(!q.empty())
{
node now = q.front(); q.pop();
int x = now.x, y = now.y, z = now.z;
if((x == y && x == s/2) || (x == z && x == s/2) || (y == z && y == s/2))
{
min_step = min(min_step, now.step);
}
int nx, ny, nz;
if(x != n)
{
if(z != 0)
{
nx = x + z - n;
if(nx <= 0) nx = x + z, nz = 0 ; // z全倒入x,x仍没装满
else nx = n, nz = z + x - n; // x装满了,且z有剩余
if(!vis[nz][nx][y])
{
vis[nz][nx][y] = 1;
q.push(node(nz, nx, y, now.step + 1));
}
}
if(y != 0)
{
nx = x + y - n;
if(nx <= 0) nx = x + y, ny = 0;
else nx = n, ny = y + x - n;
if(!vis[z][nx][ny])
{
vis[z][nx][ny] = 1;
q.push(node(z, nx, ny, now.step + 1));
}
}
}
if(y != m)
{
if(z != 0)
{
ny = y + z - m;
if(ny <= 0) ny = y + z, nz = 0;
else ny = m, nz = z + y - m;
if(!vis[nz][x][ny])
{
vis[nz][x][ny] = 1;
q.push(node(nz, x, ny, now.step + 1));
}
}
if(x != 0)
{
ny = x + y - m;
if(ny <= 0) ny = x + y, nx = 0;
else ny = m, nx = x + y - m;
if(!vis[z][nx][ny])
{
vis[z][nx][ny] = 1;
q.push(node(z,nx, ny, now.step + 1));
}
}
}
if(z != s)
{
if(x != 0)
{
nz = x + z;
nx = 0;
if(!vis[nz][nx][y])
{
vis[nz][nx][y] = 1;
q.push(node(nz, nx, y, now.step + 1));
}
}
if(y != 0)
{
nz = y + z;
ny = 0;
if(!vis[nz][x][ny])
{
vis[nz][x][ny] = 1;
q.push(node(nz, x, ny, now.step + 1));
}
}
}
if(x != 0)
{
if(y != m)
{
ny = y + x - m;
if(ny <= 0) ny = x + y, nx = 0;
else ny = m, nx = x + y - m;
if(!vis[z][nx][ny])
{
vis[z][nx][ny] = 1;
q.push(node(z, nx, ny, now.step + 1));
}
}
if(z != s)
{
nz = z + x;
nx = 0;
if(!vis[nz][nx][y])
{
vis[nz][nx][y] = 1;
q.push(node(nz, nx, y, now.step + 1));
}
}
}
if(y != 0)
{
if(x != n)
{
nx = x + y - n;
if(nx <= 0) nx = x + y, ny = 0;
else nx = n, ny = x + y - n;
if(!vis[z][nx][ny])
{
vis[z][nx][ny] = 1;
q.push(node(z, nx, ny, now.step + 1));
}
}
if(z != s)
{
nz = z + y;
ny = 0;
if(!vis[nz][x][ny])
{
vis[nz][x][ny] = 1;
q.push(node(nz, x, ny, now.step + 1));
}
}
}
if(z != 0)
{
if(x != n)
{
nx = z + x - n;
if(nx <= 0) nx = x + z, nz = 0;
else nx = n, nz = x + z - n;
if(!vis[nz][nx][y])
{
vis[nz][nx][y] = 1;
q.push(node(nz, nx, y, now.step + 1));
}
}
if(y != m)
{
ny = z + y - m;
if(ny <= 0) ny = z + y, nz = 0;
else ny = m, nz = z + y - m;
if(!vis[nz][x][ny])
{
vis[nz][x][ny] = 1;
q.push(node(nz, x, ny, now.step + 1));
}
}
}
}
}
int main()
{
while(cin >> s >> n >> m && (m + n + s))
{
if(s % 2 == 1) // 奇数不可平分
{
cout <<"NO" << endl;
continue;
}
min_step = INF;
memset(vis, 0, sizeof(vis));
bfs();
if(min_step == INF) cout << "NO" << endl;
else cout << min_step << endl;
}
return 0;
}