Poj3414 extensive search

Source: Internet
Author: User

<span style= "color: #330099;" >/*d-dtime limit:1000ms Memory limit:65536kb 64bit IO format:%i64d &%i64usubmit Status Practice POJ 3414 Descriptionyou is given the pots, having the volume of A and B liters respectively. The following operations can be Performed:fill (i) FILL the pot I (1≤i≤2) from the Tap;drop (i) Empty the PO t i to the drain; Pour (i,j) pour from pot I to pot J; After this operation either the pot J was full (and there could be some water left in the pot I), or the pot I was empty (and All its contents has been moved to the pot J). Write a program to find the shortest possible sequence of these operations that'll yield exactly C liters of water in on E of the pots. Inputon the first and only line is the numbers A, B, and C. These is all integers in the range from 1 to C≤max (A, B). Outputthe first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If thEre is several sequences of minimal length, output any one of them. If the desired result can ' t is achieved, the first and only line of the file must contain the word ' impossible '. Sample Input3 5 4Sample output6fill (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1) by Grant yuan2014.7.14poj 3414 wide Search */# include<iostream> #include <cstdio> #include <cstring> #include <cstdlib>using namespace std;  BOOL Flag=0;int next[6]={0,1,2,3,4,5};int a,b,c;int aa,bb,cc;typedef struct{int A;  int b;  int F;  int sum; int ope;} Node;int res;node q[10000];bool mark[101][101];int top,base;int top1;int s[10000];bool can (int x1,int y1) {if (x1>=0&    amp;&x1<=aa&&y1>=0&&y1<=bb&&mark[x1][y1]==0) return 1; return 0;}    void Slove () {int a1,b1,f1,a2,b2;     while (top>=base) {//cout<< "Zhang" <<endl; if (q[base].a==cc| |           Q[BASE].B==CC) {flag=1;           Res=q[base].sum;          Break        } for (int i=0;i<6;i++) {  if (i==0) {a1=aa;               b1=q[base].b;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;                q[top].sum=q[base].sum+1;                Q[top].ope=i;            Mark[a1][b1]=1;}               } else if (i==1) {a1=q[base].a;               B1=BB;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;                q[top].sum=q[base].sum+1;                Q[top].ope=i;            Mark[a1][b1]=1;}               } else if (i==2)//1dao2 {int m,n;               M=Q[BASE].A;               n=bb-q[base].b;                   if (m>=n) {a1=m-n;                B1=BB;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;                q[top].sum=q[base].sum+1;         Q[top].ope=i;       Mark[a1][b1]=1;}                  } else{a1=0;                  b1=m+q[base].b;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;                q[top].sum=q[base].sum+1;                Q[top].ope=i;                  Mark[a1][b1]=1;}               }} else if (i==3)//1dao2 {int m,n;               M=AA-Q[BASE].A;               n=q[base].b;                   if (n>=m) {a1=aa;                B1=n-m;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;                q[top].sum=q[base].sum+1;                Q[top].ope=i; Mark[a1][b1]=1;}                  } else{b1=0;                  A1=N+Q[BASE].A;                if (Can (A1,B1)) {q[++top].a=a1;                Q[TOP].B=B1;                Q[top].f=base;   q[top].sum=q[base].sum+1;             Q[top].ope=i;                  Mark[a1][b1]=1;}                   }} else if (i==4) {a1=0;                   b1=q[base].b;                    if (Can (A1,B1)) {q[++top].a=a1;                    Q[TOP].B=B1;                    Q[top].f=base;                     q[top].sum=q[base].sum+1;                     Q[top].ope=i;                    Mark[a1][b1]=1;                   }} else if (i==5) {b1=0;                   A1=Q[BASE].A;                    if (Can (A1,B1)) {q[++top].a=a1;                    Q[TOP].B=B1;                    Q[top].f=base;                    q[top].sum=q[base].sum+1;                     Q[top].ope=i;                    Mark[a1][b1]=1; }}} base++;}}    void print () {top1=-1;    int i=base,j;        while (1) {s[++top1]=q[i].ope;        J=Q[I].F;        I=j; if (i==0) BreAk        } for (j=top1;j>=0;j--) {if (s[j]==0) cout<< "FILL (1)" <<endl;        else if (s[j]==1) cout<< "FILL (2)" <<endl;        else if (s[j]==2) cout<< "pour" <<endl;        else if (s[j]==3) cout<< "pour (2,1)" <<endl;        else if (s[j]==4) cout<< "DROP (1)" <<endl;    else if (s[j]==5) cout<< "DROP (2)" <<endl;    }}int Main () {cin>>aa>>bb>>cc;    Top=-1;    memset (Mark,0,sizeof (Mark));    Mark[0][0]=1;    base=0;    q[++top].a=0;    q[top].b=0;    q[top].f=0;    q[top].sum=0;    q[top].ope=0;    Slove ();    if (flag==0) cout<< "Impossible" <<endl;    else{cout<<res<<endl;    Print ();} return 0;} </span>

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Poj3414 extensive search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.