|  | 
 
 
 楼主|
发表于 2018-2-17 14:15:50
|
显示全部楼层 
| 首先要深刻理解BFS,它不止用于地图,迷宫的问题。像这种求状态转移最快的步数或步骤的,就可以用BFS。这题也可以用广搜,以后求最短,最快,最少的问题(等权)都可以往广搜上面想想。。。理解每个”step“的含义,这里每一个step就是在正确的倒水路径上的每一次倒水,一共有6次倒水的方法,让每个“step”做这6个“动作”,然后一直这样往下”各个方向弥漫“,就一定会找到答案。 (注:对方向和步骤的理解不能太死板。要像理解面向对象一样。)
 
 
 复制代码#include<cstdio>
#include<queue>
#define maxn 101
using namespace std;
struct N
{
        int a,b,c;
        int t;
};
queue<N> Q;
bool mark[maxn][maxn][maxn];
void AtoB(int &a,int sa,int &b,int sb)
{
        if(sb-b>=a)
        {
                b+=a;
                a=0;
        }
        else
        {
                a-=sb-b;
                b=sb;
        }
}
int BFS(int s,int n,int m)
{
        int h=s/2;
        while(!Q.empty())
        {
                N now=Q.front();
                Q.pop();
                int a,b,c;
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(a,s,b,n);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(b,n,a,s);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(a,s,c,m);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(c,m,a,s);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(b,n,c,m);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
                a=now.a;
                b=now.b;
                c=now.c;
                AtoB(c,m,b,n);
                if(mark[a][b][c]==false)
                {
                        mark[a][b][c]==true;
                        N tmp;
                        tmp.a=a;
                        tmp.b=b;
                        tmp.c=c;
                        tmp.t=now.t+1;
                        if(a==h&&b==h)
                                return tmp.t;
                        if(b==h&&c==h)
                                return tmp.t;
                        if(a==h&&c==h)
                                return tmp.t;
                        Q.push(tmp);
                }
        }
        return -1;
}
int main()
{
        int s,n,m;
        while(scanf("%d%d%d",&s,&n,&m)!=EOF)
        {
                if(s==0)
                        break;
                if(s%2==1)
                {
                        printf("NO\n");
                        continue;
                }
                for(int i=0;i<=s;i++)
                        for(int j=0;j<=n;j++)
                                for(int k=0;k<=m;k++)
                                        mark[i][j][k]=false;
                N tmp;
                tmp.a=s;
                tmp.b=0;
                tmp.c=0;
                tmp.t=0;
                while(!Q.empty())
                        Q.pop();
                Q.push(tmp);
                mark[s][0][0]=true;
                int rec=BFS(s,n,m);
                if(rec==-1)
                        printf("NO\n");
                else
                        printf("%d\n",rec);
        }
        return 0;
}
 | 
 |