分析:要求平分可乐,显然可乐体积为奇数时不可能被平分;为偶数时,可乐转移有6种运动状态:
S–>N S–>M
N–>N S–>M
M–>S M–>N
用BFS跑一遍,枚举每一种状态即可
#include <iostream>
#include <stdio.h>
#include <string>
#include<cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e2+5;
struct node{
int s,n,m,step;
};
int n,m;
bool vis[maxn][maxn][maxn];
int BFS(int s)
{
queue<node>q;
q.push(node{s,0,0,0});
vis[s][0][0]=true;
int v=s/2;
while (!q.empty()){
node tem=q.front(); q.pop();
int x=tem.s,y=tem.n,z=tem.m;
if((x==v&&y==v)||(x==v&&z==v)||(y==v&&z==v)){return tem.step;}
int tx,ty,tz;
//1 s-->n
if(x>n-y) tx=x+y-n,ty=n,tz=z;
else tx=0,ty=y+x,tz=z;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1});}
// 2 s-->m
if(x>m-z) tx=x+z-m,ty=y,tz=m;
else tx=0,ty=y,tz=x+z;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1}); }
// 3 n-->m
if(y>m-z) tx=x,ty=y+z-m,tz=m;
else tx=x,ty=0,tz=y+z;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1});}
// 4 n-->s
if(y>s-x) tx=s,ty=y+x-s,tz=z;
else tx=x+y,ty=0,tz=z;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1});}
// 5 m-->s
if(z>s-x) tx=s,ty=y,tz=z+x-s;
else tx=x+z,ty=y,tz=0;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1});}
// 6 m-->n
if(z>n-y) tx=x,ty=n,tz=z+y-n;
else tx=x,ty=z+y,tz=0;
if(!vis[tx][ty][tz]) {
vis[tx][ty][tz]=true;
q.push(node{tx,ty,tz,tem.step+1});}
}
return -1;
}
int main()
{
int s;
while (~scanf("%d%d%d",&s,&n,&m)&&s){
memset(vis,false,sizeof(vis));
if(s%2){ printf("NO\n"); continue;}
int ans=BFS(s);
if(ans==-1)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}