您的位置:首页 > 其它

hdu 1495 非常可乐(广搜)

2015-07-26 22:47 369 查看
题目地址

题目大意:给出三个数s,n,m,(s=m+n)初始态为s装满可乐,n和m为空,求是否能将可能均分,若能输出倒可乐的最小次数

解题思路:对于3个杯中每个杯子有可乐时用队列将状态加入队尾,且标记当前状态,直到队列中有均分的情况停止

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <list>
#include <set>

using namespace std;

const int maxn = 100+10;

int visit[maxn][maxn][maxn];
int s,n,m;

struct Node
{
int s,n,m,step;
};

int check(int x,int y,int z)
{
if(x==0 && y==z) return 1;
if(y==0 && x==z) return 1;
if(z==0 && x==y) return 1;
return 0;
}
int bfs()
{
queue<Node> q;
Node a,next;
a.s = s,a.n = 0,a.m = 0,a.step = 0;
visit[s][0][0] = 1;
q.push(a);
while(!q.empty())
{
a = q.front();
q.pop();
if(check(a.s,a.n,a.m)) return a.step;
if(a.n)//n还有
{
if(a.n>=s-a.s)//n能将s倒满,将n倒入s装满s
{
next = a;
next.n = next.n-(s-a.s);//剩余的n
next.s = s;//s满了
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else //n不能将s倒满,将n全部倒入s中
{
next = a;
next.s = next.n+next.s;
next.n = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
if(a.n>=m-a.m)//n能将m倒满,将n倒入m装满m
{
next = a;
next.n = next.n-(m-a.m);
next.m = m;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else//n不能将m倒满,将n全部倒入m中
{
next = a;
next.m = next.n+next.m;
next.n = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
}
if(a.m)//m还有
{
if(a.m>=s-a.s)//m能将s倒满,将m倒入s装满s
{
next = a;
next.m = next.m-(s-a.s);
next.s = s;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else //m不能将s倒满,将m全部倒入s中
{
next = a;
next.s = next.m+next.s;
next.m = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
if(a.m>=n-a.n)//m能将n倒满,将m倒入n装满n
{
next = a;
next.m = next.m-(n-a.n);
next.n = n;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else//m不能将n倒满,将m全部倒入n中
{
next = a;
next.n = next.n+next.m;
next.m = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
}
if(a.s)//s还有
{
if(a.s>=m-a.m)//s能将m倒满,将s倒入m装满m
{
next = a;
next.s = next.s-(m-a.m);
next.m = m;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else //S不能将m倒满,将s全部倒入m中
{
next = a;
next.m = next.m+next.s;
next.s = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
if(a.s>=n-a.n)//s能将n倒满,将s倒入n装满n
{
next = a;
next.s = next.s-(n-a.n);
next.n = n;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
else//s不能将n倒满,将s全部倒入n中
{
next = a;
next.n = next.n+next.s;
next.s = 0;
if(!visit[next.s][next.n][next.m])
{
next.step = a.step+1;
q.push(next);
visit[next.s][next.n][next.m] = 1;
}
}
}
}
return 0;
}
int main()
{
int ans;
while(scanf("%d%d%d",&s,&n,&m) != EOF)
{
if(!(s+n+m)) break;
if(s%2)
{
printf("NO\n");
continue;
}
memset(visit,0,sizeof(visit));
ans = bfs();
if(ans)
printf("%d\n",ans);
else
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: