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

很好理解的一种写法,很适合刚学不久的人去理解,没有太复杂的语句

Posted by 1213153744 at 2018-10-24 18:02:21 on Problem 3414
#include<cstdio>
int book[101][101];
struct asd
{
    int k1;
    int k2;
    int o;
    int step;
    int xb;      
}ovo[100001];
int a,b,c;
void dfs(int id);
int main()
{
    scanf("%d%d%d",&a,&b,&c);
    int head=1;
    int tail=1;
    int hhh=0;
    ovo[tail].k1=0;
    ovo[tail].k2=0;
    ovo[tail].o=0;
    ovo[tail].step=0;
    ovo[tail].xb=0;
    tail++;
    book[0][0]=1;
    while(head!=tail)
    {
        int kr=ovo[head].k1;
        int kl=ovo[head].k2;
        int p,q;
        for(int i=1;i<=6;i++)
        {
            if(i==1)//fill(1)
            {
                if(kr<a)
                {
                   p=a;
                   q=kl;
                   if(book[p][q]==0)
                   {
                       book[p][q]=1;
                       ovo[tail].k1=a;
                       ovo[tail].k2=kl;
                       ovo[tail].o=1;
                       ovo[tail].step=ovo[head].step+1;
                       ovo[tail].xb=head;
                       tail++;
                   }
                }
            }    
            else if(i==2)//fill(2);
            {
               if(kl<b)
               {
                   p=kr;
                   q=b;
                   if(book[p][q]==0)
                   {
                       book[p][q]=1;
                       ovo[tail].k1=kr;
                       ovo[tail].k2=b;
                       ovo[tail].o=2;
                       ovo[tail].step=ovo[head].step+1;
                       ovo[tail].xb=head;
                       tail++;           
                   } 
               }                   
            }
            else if(i==3)//drop(1)
            {
               if(kr!=0)
               {
                   p=0;
                   q=kl;
                   if(book[p][q]==0)
                   {
                       book[p][q]=1;
                       ovo[tail].k1=0;
                       ovo[tail].k2=kl;
                       ovo[tail].o=3;
                       ovo[tail].step=ovo[head].step+1;
                       ovo[tail].xb=head;
                       tail++;                 
                   }         
               }                 
            }
            else if(i==4)//drop(2)
            {
               if(kl!=0)
               {
                   p=kr;
                   q=0;
                   if(book[p][q]==0)
                   {
                       book[p][q]=1;
                       ovo[tail].k1=kr;
                       ovo[tail].k2=0;
                       ovo[tail].o=4;
                       ovo[tail].step=ovo[head].step+1;
                       ovo[tail].xb=head;
                       tail++;                 
                   }         
               }      
            }
            else if(i==5)//pour(1,2)
            {
               if(kr+kl>=b)//倒不完或者刚好倒完 
               {
                   int u=0;
                   u=b-kl;
                   p=kr-u;
                   q=b;
                   if(book[p][q]==0)
                   {
                        book[p][q]=1;
                        ovo[tail].k1=p;
                        ovo[tail].k2=q;
                        ovo[tail].o=5;
                        ovo[tail].step=ovo[head].step+1;
                        ovo[tail].xb=head;
                        tail++;                 
                   }         
               }
               if(kr+kl<b)//倒不满
               {
                    p=0;
                    q=kr+kl;
                    if(book[p][q]==0)
                    {
                        book[p][q]=1;
                        ovo[tail].k1=p;
                        ovo[tail].k2=q;
                        ovo[tail].o=5;
                        ovo[tail].step=ovo[head].step+1;
                        ovo[tail].xb=head;
                        tail++;             
                    }                    
               }      
            }
            else if(i==6)//pour(2,1)
            {
               if(kr+kl>=a)//倒不完或者刚好倒完 
               {
                   int u=0;
                   u=a-kr;
                   p=a;
                   q=kl-u;
                   if(book[p][q]==0)
                   {
                        book[p][q]=1;
                        ovo[tail].k1=p;
                        ovo[tail].k2=q;
                        ovo[tail].o=6;
                        ovo[tail].step=ovo[head].step+1;
                        ovo[tail].xb=head;
                        tail++;                 
                   }         
               }
               if(kr+kl<a)//倒不满
               {
                    p=kr+kl;
                    q=0;
                    if(book[p][q]==0)
                    {
                        book[p][q]=1;
                        ovo[tail].k1=p;
                        ovo[tail].k2=q;
                        ovo[tail].o=6;
                        ovo[tail].step=ovo[head].step+1;
                        ovo[tail].xb=head;
                        tail++;             
                    }                    
               }          
            }
            if(p==c||q==c)
            {
                hhh=1;
                break;              
            }
        }
        if(hhh==1)
           break;
        head++;                 
    }
    if(hhh==1)
    {
       printf("%d\n",ovo[tail-1].step);
       dfs(tail-1);
    }
    else 
        printf("impossible\n");
    getchar();
    getchar();
    return 0;
}
void dfs(int id)
{
     if(id==1)
     {
         if(ovo[id].o==1)
             printf("FILL(1)\n");
         else if(ovo[id].o==2)
             printf("FILL(2)\n");
         else if(ovo[id].o==3)
             printf("DROP(1)\n");
         else if(ovo[id].o==4)
             printf("DROP(2)\n");
         else if(ovo[id].o==5)
             printf("POUR(1,2)\n");
         else if(ovo[id].o==6)
             printf("POUR(2,1)\n"); 
     }
     else
     {
         dfs(ovo[id].xb);
         if(ovo[id].o==1)
             printf("FILL(1)\n");
         else if(ovo[id].o==2)
             printf("FILL(2)\n");
         else if(ovo[id].o==3)
             printf("DROP(1)\n");
         else if(ovo[id].o==4)
             printf("DROP(2)\n");
         else if(ovo[id].o==5)
             printf("POUR(1,2)\n");
         else if(ovo[id].o==6)
             printf("POUR(2,1)\n");      
     } 
     return;   
}

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