571 - Jugs

来源:互联网 发布:江西百川网络托管 编辑:程序博客网 时间:2024/05/22 07:48
描述:算是哈希判重问题吧,不过数据不大,直接用数组标记就行#include <cstdio>#include <cstring>bool v[1005][1005];char s[6][10]= {"fill A","fill B","empty A","empty B","pour A B","pour B A"};struct Jugs{    int A,B;    int pos;    int oper;};Jugs p[1000005];void show(int x){    if(x>0) show(p[x].pos);    if(x>0) printf("%s\n",s[p[x].oper]);}int main(){   // freopen("a.txt","r",stdin);    int n,m,goal,flag;    while(scanf("%d%d%d",&n,&m,&goal)!=EOF)    {        if(!goal)        {            puts("success");            continue;        }        for(int i=0; i<=m; ++i)            for(int j=0; j<=m; ++j) v[i][j]=0;        v[0][0]=1;        p[0].A=p[0].B=flag=0;        p[0].oper=p[0].pos=-1;        int last=1,first=0;        while(first<last)        {            int A=p[first].A,B=p[first].B;            for(int i=0; i<6; i++)            {                int a=0,b=0;                if(i==2&&A) a=0,b=B;                if(i==0&&A<n) a=n,b=B;                if(i==3&&B) a=A,b=0;                if(i==1&&B<m) b=m,a=A;                if(i==4&&A&&B<m)                {                    if(A+B<m) b=A+B;                    else a=A+B-m,b=m;                }                if(i==5&&A<n&&B)                {                    if(A+B<n) a=A+B;                    else a=n,b=A+B-n;                }                if(!v[a][b])                {                    v[a][b]=1;                    p[last].pos=first;                    p[last].oper=i;                    p[last].A=a;                    p[last].B=b;                    if(b==goal)                    {                        flag=1;                        break;                    }                    ++last;                }            }            if(flag) break;            ++first;        }        show(last);        puts("success");    }    return 0;}