Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于AC了 足足6K代码我的天

Posted by xsjlmzs at 2018-08-08 11:08:28 on Problem 3414
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<utility>
#include<cmath>
using namespace std;
struct node
{
    int num;
    int psta,pstb;
    int s;
};
typedef pair<int,int> P;
const int MAXN=1e2+9;
const int MAXX=1e6;
bool flag=false;
int arr[MAXX];
bool vis[MAXN][MAXN];
P fa[MAXX];
int A,B,C;
int cnt=0;
void order(int p);
void print(int n,int num)
{
    int p=0;
    printf("%d\n",n);
    while(num)
    {
        arr[p++]=fa[num].second;
        num=fa[num].first;
    }
    order(p);
}
void order(int p)
{
    for(int i=p-1;i>=0;--i)
    {
        if(arr[i]==1)
            printf("FILL(1)\n");
        else if(arr[i]==2)
            printf("FILL(2)\n");
        else if(arr[i]==3)
            printf("POUR(1,2)\n");
        else if(arr[i]==4)
            printf("POUR(2,1)\n");
        else if(arr[i]==5)
            printf("DROP(1)\n");
        else if(arr[i]==6)
            printf("DROP(2)\n");
    }
}
int BFS()
{
    queue<node>que;
    que.push(node{0,0,0,0});
    fa[cnt++]=P(-1,0);
    while(!que.empty())
    {
        node tmp=que.front();
        if(tmp.psta==C||tmp.pstb==C)
        {
            flag=true;
            print(tmp.s,tmp.num);
            return tmp.s;
        }
        que.pop();
        int Num=tmp.num;
        if(vis[tmp.psta][tmp.pstb]) continue;
        vis[tmp.psta][tmp.pstb]=true;
        if(tmp.psta==0&&tmp.pstb==0)//all empty
        {
            que.push(node{cnt,0,B,tmp.s+1});
            fa[cnt++]=P(Num,2);
            que.push(node{cnt,A,0,tmp.s+1});
            fa[cnt++]=P(Num,1);
        }
        else if(tmp.psta==0)//A empty
        {
            que.push(node{cnt,A,tmp.pstb,tmp.s+1});//fill A
            fa[cnt++]=P(Num,1);
            que.push(node{cnt,0,0,tmp.s+1});// drop B
            fa[cnt++]=P(Num,6);
            if(tmp.pstb>=A)
            {
                que.push(node{cnt,A,tmp.pstb-A,tmp.s+1});
                fa[cnt++]=P(Num,4);
            }
            else
            {
                que.push(node{cnt,tmp.pstb,0,tmp.s+1});
                fa[cnt++]=P(Num,4);
            }
        }
        else if(tmp.pstb==0)// B empty
        {
            que.push(node{cnt,tmp.psta,B,tmp.s+1});//fill B
            fa[cnt++]=P(Num,2);
            que.push(node{cnt,0,0,tmp.s+1});//drop A
            fa[cnt++]=P(Num,5);
            if(tmp.psta>=B)
            {
                que.push(node{cnt,tmp.psta-B,B,tmp.s+1});
                fa[cnt++]=P(Num,3);
            }
            else
            {
                que.push(node{cnt,0,tmp.psta,tmp.s+1});
                fa[cnt++]=P(Num,3);
            }
        }
        else //A not empty&&B not empty
        {
            if(tmp.psta==A&&tmp.pstb==B)// All full
            {
                que.push(node{cnt,0,B,tmp.s+1});
                fa[cnt++]=P(Num,5);
                que.push(node{cnt,A,0,tmp.s+1});
                fa[cnt++]=P(Num,6);
            }
            else if(tmp.psta!=A&&tmp.pstb==B)//B full
            {
                que.push(node{cnt,tmp.psta,0,tmp.s+1});//drop B
                fa[cnt++]=P(Num,6);
                que.push(node{cnt,0,B,tmp.s+1});//drop A
                fa[cnt++]=P(Num,5);
                if(B>=(A-tmp.psta))
                {
                    que.push(node{cnt,A,B-(A-tmp.psta),tmp.s+1});
                    fa[cnt++]=P(Num,4);

                }//B倒完有剩余
                else
                {
                    que.push(node{cnt,tmp.psta+B,0,tmp.s+1});//没有剩余
                    fa[cnt++]=P(Num,4);
                }
                //无需再次装满A
            }
            else if(tmp.pstb!=B&&tmp.psta==A)
            {
                que.push(node{cnt,A,0,tmp.s+1});//drop B
                fa[cnt++]=P(Num,6);
                que.push(node{cnt,0,tmp.pstb,tmp.s+1});// drop A
                fa[cnt++]=P(Num,5);
                if(A>=(B-tmp.pstb))
                {
                    que.push(node{cnt,A-(B-tmp.pstb),B,tmp.s+1});
                    fa[cnt++]=P(Num,3);
                }//pour A to B
                else
                {
                    que.push(node{cnt,0,tmp.pstb+A,tmp.s+1});
                    fa[cnt++]=P(Num,3);
                }
            }
            else
            {
                que.push(node{cnt,0,tmp.pstb,tmp.s+1});//drop A
                fa[cnt++]=P(Num,5);
                que.push(node{cnt,tmp.psta,0,tmp.s+1});//drop B
                fa[cnt++]=P(Num,6);
                que.push(node{cnt,A,tmp.pstb,tmp.s+1});//fill A
                fa[cnt++]=P(Num,1);
                que.push(node{cnt,tmp.psta,B,tmp.s+1});//fill B
                fa[cnt++]=P(Num,2);
                if(tmp.psta>=(B-tmp.pstb))//pour A to B
                {
                    que.push(node{(cnt,tmp.psta-(B-tmp.pstb)),B,tmp.s+1});
                    fa[cnt++]=P(Num,3);
                }
                else
                {
                    que.push(node{cnt,0,tmp.psta+tmp.pstb,tmp.s+1});
                    fa[cnt++]=P(Num,3);
                }
                if(tmp.pstb>=(A-tmp.psta))// pour B to A
                {
                    que.push(node{(cnt,A,tmp.pstb-(A-tmp.psta),tmp.s+1)});
                    fa[cnt++]=P(Num,4);
                }
                else
                {
                    que.push(node{cnt,tmp.psta+tmp.pstb,0,tmp.s+1});
                    fa[cnt++]=P(Num,4);
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        memset(vis,0,sizeof(vis));
        cnt=0;
        flag=false;
        if(C<A&&C<B&&A!=B&&C==abs(static_cast<double>(A-B)))
        {
            printf("2\n");
            if(A>B)
            {
                printf("FILL(1)\n");
                printf("POUR(1,2)\n");
            }
            else
            {
                printf("FILL(2)\n");
                printf("POUR(2,1)\n");
            }
        }
        else if((A==B&&C!=A))
            printf("impossible\n");
        else
        {
            int ans=BFS();
            if(!flag)
                printf("impossible\n");
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator