nyoj-Three cups of water

Source: Internet
Author: User
Tags bool time limit
three cups of waterTime limit: Ms | Memory limit: 65535 KB Difficulty: 4 description give three cups, different sizes, and only the largest cup of water is full, the remaining two are empty cups. Three cups of water are poured between each other, and the cups are not identified and can only be calculated according to the volume of the cups given. You are now asked to write a program that outputs the minimum number of times that the initial state reaches the target State. Enter the first line an integer n (0<n<50) to represent the n set of test data
Next, each set of test data has two lines, the first line gives three integers V1 V2 V3 (v1>v2>v3 v1<100 v3>0) represents the volume of three cups.
The second line gives three integers E1 E2 E3 (volume less than equal to the corresponding cup volume) indicates that we need the final state output to output the corresponding test data in each row with the lowest number of water pours. If the target status output is not reached-1 sample input
2
6 3 1
4 1 1
9 3 2
7 1 1
Sample output
3
-1
SOURCE Classic Topic Uploader HZYQAZASDF


When I wrote this question, I thought it was a math problem.

So I tried to solve him with mathematical ideas.

So I got something to find out.

Three cups, if the final state of the three Cups is dissatisfied with no solution

............ The rest is irregular.

Or do you honestly use the wide search.

Why don't you search the deep.

We don't know when we should be able to stop.

Keep searching down, dead loop, always pour water on each other, Galaxy second also has to time out

If you set a border, if you can't find the first few times, you don't know where this border is going to fit, it's easy. Correct answer omitted long easy timeout

It's better to search, not so much to worry about.

#include <stdio.h> #include <string.h> int e1,e2,e3;
int i,j,k=0,l=1,need;
BOOL vis[100][100][100];
    struct Node {int v[3];
int step;
} sb[200];
    void ds (int a,int b) {if (Sb[k].v[a]!=0&&vis[sb[k].v[0]][sb[k].v[1]][sb[k].v[2]]==false)//Determine if water can be poured and if this state has occurred             {int sum=sb[k].v[a]+sb[k].v[b];
            Below start pour water if (sum>=sb[k].v[b]) {sb[l].v[b]=sb[0].v[b];
        SB[L].V[A]=SUM-SB[L].V[B];
            } else {sb[l].v[b]=sum;
        sb[l].v[a]=0;
        } sb[l].step=sb[k].step+1;
    l++; }} int BFs (int v1,int v2,int v3) {for (k=0;; k++)//traverse the queue from head to tail {//printf ("%d%d%d\n", sb[k].v[0],sb[k].v[
        1],SB[K].V[2]);
                for (i=0; i<3; i++)//traverse 6 kinds of inverted water conditions {for (j=0; j<3; i++) {if (i!=j)       {DS (I,J);
       Pour water function to pour water from I into J}}} if (sb[k].v[0]==e1&&sb[k].v[1]==e2&&sb[k].v[2])//If the process reaches the target State then return steps {retur
        n Sb[k].step;       }} return-1;
    If the queue has been traversed then no result is returned-1} int main () {int n;
    scanf ("%d", &n);
        while (n--) {int v1,v2,v3,e1,e2,e3;
        scanf ("%d%d%d%d%d%d", &sb[0].v[0],&sb[0].v[1],&sb[0].v[2],&e1,&e2,&e3);              K=0,sb[0].step=0;
        Initializes the memset (vis,false,sizeof (VIS));
    printf ("%d\n", BFS (Sb[0].v[0],sb[0].v[1],sb[0].v[2])); }
}
Direct error ...

for (j=0; j<3; i++)
More obvious mistakes ~

#include <stdio.h> #include <string.h> int v0[3],e1,e2,e3;
int i,j,k=0,l=1,need;
BOOL vis[100][100][100];
    struct Node {int v[3];
int step;
} sb[200]; void ds (int a,int b) {if (sb[k].v[a]!=0)//Determine if the water can be poured and if this status has occurred {sb[l].v[0]=sb[k].v[0],sb[l].v[1]=sb[k].v[1],
        SB[L].V[2]=SB[K].V[2];             int sum=sb[k].v[a]+sb[k].v[b];
            Below start pour water if (sum>=v0[b]) {sb[l].v[b]=v0[b];
        SB[L].V[A]=SUM-V0[B];
            } else {sb[l].v[b]=sum;
        sb[l].v[a]=0;
            } if (Vis[sb[k].v[0]][sb[k].v[1]][sb[k].v[2]]==false) {//printf ("%d\n", L);
            sb[l].step=sb[k].step+1;
            l++;
            Vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]=true;
        rintf ("%d%d%d^-^\n", sb[l-1].v[0],sb[l-1].v[1],sb[l-1].v[2]); }}} int bfs (int v1,int v2,int v3) {for (k=0;; k++)//traverse the queue from head to tail {//printf ("%d%d%d$\n", sb[k].v[0],s B[k].v[1],SB[K].V[2]); for (i=0; i<3; i++)//traverse 6 kinds of inverted water conditions {for (j=0; j<3; J + +) {//printf ("
                %d%d***\n ", i,j);       if (i!=j) {DS (I,J); The Pour water function will pour water from I into J}}} if (Sb[k].v[0]==e1&&sb[k].v[1]==e2&&s
        B[K].V[2])//If the process reaches the target State then return steps {return sb[k].step;       }} return-1;
    If the queue has been traversed then no result is returned-1} int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%d%d%d%d%d%d", &v0[0],&v0[1],&v0[2],&e1,&e2,&e3);
        K=0,sb[0].step=0;              sb[0].v[0]=v0[0],sb[0].v[1]=0,sb[0].v[2]=0;
        Initializes the memset (vis,false,sizeof (VIS));
    printf ("%d\n", BFS (sb[0].v[0],0,0));
 }
}
Obviously, this row of weight does not work ....

All right, let's change the order of the platoon steps.

#include <stdio.h> #include <string.h> int v0[3],e1,e2,e3;
int i,j,k=0,l=1,need;
int vis[105][105][105];
    struct Node {int v[3];
int step;
} sb[50000]; void ds (int a,int b) {if (sb[k].v[a]!=0)//Determine if water can be poured {sb[l].v[0]=sb[k].v[0],sb[l].v[1]=sb[k].v[1],sb[l].v[2
        ]=SB[K].V[2];             int sum=sb[k].v[a]+sb[k].v[b];
            Below start pour water if (sum>=v0[b]) {sb[l].v[b]=v0[b];
        SB[L].V[A]=SUM-V0[B];
            } else {sb[l].v[b]=sum;
        sb[l].v[a]=0;
        }//printf ("%d 0.0\n", vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]);
            if (vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]==0)//Determine if this state has occurred {//printf ("%d\n", L);
            sb[l].step=sb[k].step+1;
            Vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]=1;
            l++;
        printf ("%d%d%d^-^\n", sb[l-1].v[0],sb[l-1].v[1],sb[l-1].v[2]); }}} int bfs (int v1,int v2,int v3) {for (k=0;; k+ +)//traverse the queue from head to tail {//printf ("%d%d%d$\n", sb[k].v[0],sb[k].v[1],sb[k].v[2]); for (i=0; i<3; i++)//traverse 6 kinds of inverted water conditions {for (j=0; j<3; J + +) {//printf ("
                %d%d***\n ", i,j);       if (i!=j) {DS (I,J); The Pour water function will pour water from I into J}}} if (Sb[k].v[0]==e1&&sb[k].v[1]==e2&&s
        B[K].V[2])//If the process reaches the target State then return steps {return sb[k].step;   } if (k>l) {return-1;
    If the queue has been traversed then there will be no result return-1}} int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%d%d%d%d%d%d", &v0[0],&v0[1],&v0[2],&e1,&e2,&e3);
        if (E1+e2+e3!=v0[0]) {printf (" -1\n");
        } else if (v0[0]==e1&&v0[1]==e2&&v0[2]==e3) {printf ("0\n");
   } else {         K=0,sb[0].step=0;              sb[0].v[0]=v0[0],sb[0].v[1]=0,sb[0].v[2]=0;
            Initializes the memset (vis,0,sizeof (VIS));
        printf ("%d\n", BFS (sb[0].v[0],0,0));
 }
    }
}
Hey

if (sb[k].v[0]==e1&&sb[k].v[1]==e2&&sb[k].v[2])    //If the process reaches the target State  then return the  number of steps
I've been looking for this mistake for a long time

#include <stdio.h> #include <string.h> int v0[3],e1,e2,e3;
int i,j,k=0,l=1,need;
int vis[105][105][105];
    struct Node {int v[3];
int step; } sb[100000] void ds (int a,int b) {if (sb[k].v[a]!=0)//Determine if water can be poured {SB[L].V[0]=SB[K].V[0],SB[L].V[1]=SB[K].V
        [1],sb[l].v[2]=sb[k].v[2];             int sum=sb[k].v[a]+sb[k].v[b];
            Below start pour water if (sum>=v0[b]) {sb[l].v[b]=v0[b];
        SB[L].V[A]=SUM-V0[B];
            } else {sb[l].v[b]=sum;
        sb[l].v[a]=0;
        }//printf ("%d 0.0\n", vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]);
            if (vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]==0)//Determine if this state has occurred {//printf ("%d\n", L);
            sb[l].step=sb[k].step+1;
            Vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]=1;
            l++;
        printf ("%d%d%d^-^\n", sb[l-1].v[0],sb[l-1].v[1],sb[l-1].v[2]); }}} int bfs (int v1,int v2,int v3) {for (k=0;; k+ +)//traverse the queue from head to tail {//printf ("%d%d%d$\n", sb[k].v[0],sb[k].v[1],sb[k].v[2]); for (i=0; i<3; i++)//traverse 6 kinds of inverted water conditions {for (j=0; j<3; J + +) {//printf ("
                %d%d***\n ", i,j);       if (i!=j) {DS (I,J); The Pour function will pour water from I into J if (SB[L-1].V[0]==E1&AMP;&AMP;SB[L-1].V[1]==E2&AMP;&AMP;SB[L-1].V[2]==E3)//if the process
                    Reach the target state then return steps {return sb[l-1].step;   }}}} if (k>l) {return-1;
    If the queue has been traversed then there will be no result return-1}} int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%d%d%d%d%d%d", &v0[0],&v0[1],&v0[2],&e1,&e2,&e3);
        if (E1+e2+e3!=v0[0]) {printf (" -1\n");
  } else if (v0[0]==e1&&v0[1]==e2&&v0[2]==e3) {          printf ("0\n");
            } else {k=0,sb[0].step=0;              sb[0].v[0]=v0[0],sb[0].v[1]=0,sb[0].v[2]=0;
            Initializes the memset (vis,0,sizeof (VIS));
        printf ("%d\n", BFS (sb[0].v[0],0,0)); }
    }
}
.... There's one more question.

Sample Example

6 4 3

3 3 0

The output should be 2.

But it's 3.

Not the smallest case.

If you join a min to find the bottom, then it is likely to time out.


Wow, Kerala. Finally AC.

#include <stdio.h> #include <string.h> int v0[3],e1,e2,e3;
int i,j,k=0,l=1,need;
int vis[105][105][105];
    struct Node {int v[3];
int step;
} sb[100000]; void ds (int a,int b) {if (sb[k].v[a]!=0)//Determine if water can be poured {sb[l].v[0]=sb[k].v[0],sb[l].v[1]=sb[k].v[1],sb[l].v[2
        ]=SB[K].V[2];             int sum=sb[k].v[a]+sb[k].v[b];
            Below start pour water if (sum>=v0[b]) {sb[l].v[b]=v0[b];
        SB[L].V[A]=SUM-V0[B];
            } else {sb[l].v[b]=sum;
        sb[l].v[a]=0;
        }//printf ("%d 0.0\n", vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]);
            if (vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]==0)//Determine if this state has occurred {//printf ("%d\n", L);
            sb[l].step=sb[k].step+1;
            Vis[sb[l].v[0]][sb[l].v[1]][sb[l].v[2]]=1;
            l++;
        printf ("%d%d%d%d^-^\n", sb[l-1].v[0],sb[l-1].v[1],sb[l-1].v[2],sb[l-1].step); }}} int bfs (int v1,int v2,int v3) {
    for (k=0;; k++)//traverse the queue from head to tail {//printf ("%d%d%d$\n", sb[k].v[0],sb[k].v[1],sb[k].v[2]); for (i=0; i<3; i++)//traverse 6 kinds of inverted water conditions {for (j=0; j<3; J + +) {//printf ("
                %d%d***\n ", i,j);       if (i!=j) {DS (I,J); The Pour function will pour water from I into J if (SB[L-1].V[0]==E1&AMP;&AMP;SB[L-1].V[1]==E2&AMP;&AMP;SB[L-1].V[2]==E3)//if the process
                    Reach the target state then return steps {return sb[l-1].step;   }}}} if (k>l) {return-1;
    If the queue has been traversed then there will be no result return-1}} int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%d%d%d%d%d%d", &v0[0],&v0[1],&v0[2],&e1,&e2,&e3);
        if (E1+e2+e3!=v0[0]) {printf (" -1\n"); } else if (v0[0]==e1) {printf ("0\n");
            } else {k=0,sb[0].step=0,l=1;              sb[0].v[0]=v0[0],sb[0].v[1]=0,sb[0].v[2]=0;
            Initializes the memset (vis,0,sizeof (VIS));
        printf ("%d\n", BFS (sb[0].v[0],0,0)); }
    }
}
I dbug this question for several days may be I am too water ...

But I always insisted that I dbug ...

Do not look at the online code.

The excitement is broken ... 1





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.