Mother's Milk chapter 1.4

  写的少啊,用的办法各种麻烦,想着用dfs+判断重复退出,a,b,c 其实只用记录2个的状态足以判断是否已经dfs,

我开始打算用个struct{int a,int b,int c}来记录...后来网上看nowco的报告,直接一个visited[a][b]保存a,b即可

若已经dfs,怎visited[a][b]=1,连遍历都省了...

/*

ID: hubiao cave

PROG: milk3

LANG: C++

*/




#include<iostream>
#include<fstream>
#include<string>

using namespace std;
struct temp
{
    int a;
    int b;
};

bool operator == (const temp&,const temp&);

int visited[22][22];
int cleft[21];
int poor(int step,int&,int&,int&);
void dfs(int a,int b,int c);
int asize,bsize,csize;
int main()
{

    ifstream fin("milk3.in");
    ofstream fout("milk3.out");

    fin>>asize>>bsize>>csize;

    cleft[csize]=1;

    dfs(0,0,csize);

    int flag=0;
    for(int i=0;i<=20;i++)
    {
        if(cleft[i]==1)
        {
            
            if(flag)
                fout<<" ";
            fout<<i;
            flag=1;
        }
    }
    fout<<endl;

    

    

    return 0;


}



void dfs(int a,int b,int c)
{
    if(visited[a][b])
        return;
    visited[a][b]=1;
    int ta,tb,tc;
    for(int i=1;i<=6;i++)
    {
        ta=a;
        tb=b;
        tc=c;
        poor(i,ta,tb,tc);
        dfs(ta,tb,tc);
    }
}
int poor(int step,int&a ,int&b,int&c)
{
    switch(step)
    {
    case 1://a->b
        if(b<bsize)
        {
            if(a>=bsize-b)
            {
                int t;
                t=bsize-b;
                a=a-t;
                b=bsize;
                if(a==0)
                    cleft[c]=1;
            }
            else
            {
                b=b+a;
                a=0;
                cleft[c]=1;
            }
        }
        else
        {
            if(a==0)
                cleft[c]=1;
        }
        break;
    case 2://a->c
        if(c<csize)
        {
            if(a>=csize-c)
            {
                int t=csize-c;
                a=a-t;
                c=csize;
                if(a==0)
                    cleft[c]=1;
            }
            else
            {
                c=c+a;
                a=0;
                cleft[c]=1;
            }
        }
        else
        {
            cleft[c]=1;
        }
        break;
    case 3://b->a
        if(a<asize)
        {
            if(b>=asize-a)
            {
                int t=asize-a;
                b=b-t;
                a=asize;
            }
            else
            {
                a=b+a;
                b=0;
                if(a==0)
                    cleft[c]=1;
            }
        }

        break;
    case 4://b->c
        if(c<csize)
        {
            if(b>=csize-c)
            {
                int t=csize-c;
                b=b-t;
                c=csize;
                if(a==0)
                    cleft[c]=1;
            }
            else
            {
                c=b+c;
                b=0;
                if(a==0)
                    cleft[c]=1;
            }
        }
        else
        {
            if(a==0)
                cleft[c]=1;
        }
        break;
    case 5://c->a
        if(a<asize)
        {
            if(c>=asize-a)
            {
                int t=asize-a;
                c=c-t;
                a=asize;
            }
            else
            {
                a=a+c;
                c=0;
                if(a==0)
                    cleft[c]=1;
            }
        }

        break;
    case 6://c->b
        if(b<bsize)
        {
            if(c>=bsize-b)
            {
                int t=bsize-b;
                b=bsize;
                c=c-t;
                if(a==0)
                    cleft[c]=1;
            }
            else
            {
                b=b+c;
                c=0;
                if(a==0)
                    cleft[c]=1;
            }
        }
        else
        {
            if(a==0)
                cleft[c]=1;
        }
        break;
    }
    return 0;
}
bool operator == (const temp& t1,const temp& t2)
{
    if(t1.a==t2.a&&t1.b==t2.b)
        return true;
    return false;
}
原文地址:https://www.cnblogs.com/cavehubiao/p/3259689.html