NYOJ21 Three Water cups (classic problem BFS)

Source: Internet
Author: User

Title Description:
Http://acm.nyist.net/JudgeOnline/problem.php?pid=21

give three cups, different sizes, and only the largest cups 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.
input
The first line an integer n (0<n<50) represents the N group of test data
The next two rows of test data, the first row gives three integers V1 V2 V3 (v1>v2>v3 v1<100 v3>0) Represents the volume of a three cup of water.
The second line gives three integers E1 E2 E3 (volume less than equal to the corresponding Cup volume) represents the final state we need
output
26 3 1 3 1 1  
sample output Out
3-1  
br>

Topic Analysis:

Classic topics, BFS search, without backtracking, direct violence can be.

AC Code:

 /** *bfs, search for implicit graphs */#include <iostream> #include <cstdio> #include <map> #include <cstring># include<string> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <cstdlib> #include <cctype> #include <cstring> #include <cmath>using namespace Std;int vis[101][101][101];//record is accessed by struct statenode{int cur[3];//record current state int v[3];//record State int step;//steps};queue<stateno De> q;int sucess (statenode a,statenode b) {//Compare equals return (a.cur[0] = = B.cur[0] && a.cur[1] = = B.cur[1] &amp ;& a.cur[2] = = b.cur[2]);}    int main () {int t,res;    Statenode b,e;    cin>>t;        while (t--) {res=-1;        while (!q.empty ()) {//empties queue Q.pop ();        } cin>>b.v[0]>>b.v[1]>>b.v[2]; Below begin to simulate pour water, and search b.cur[0]=b.v[0];//first give the largest water cup full water b.cur[1]=b.cur[2]=0;//small cup for 0 b.step=0;//record steps cin&        gt;>e.cur[0]>>e.cur[1]>>e.cur[2]; Intok=0;//whether the record can reach the end state memset (vis,0,sizeof (VIS)); Q.push (b);//Join Queue vis[b.cur[0]][b.cur[1]][b.cur[2]]=1;//has been accessed or has been accessed while (!q.empty ()) {//For wide search state            Node U=q.front ();            cout<<u.cur[0]<< "" <<u.cur[1]<< "" <<u.cur[2]<<endl;            Q.pop ();                if (sucess (u,e)) {//Success and end Res=u.step;            Break                    }//else simulated pour water for (int i=0;i<=2;i++) {//with each quilt to the other two quilts pour water for (int j=0;j<=2;j++) { if (i!=j) {int minv=u.v[j]-u.cur[j];//from current state to quilt full, required water if (u.c                        UR[I]&LT;MINV) {//The current water is less than the water required, then the water can be used into the current water minv=u.cur[i];                        }//New node appears Statenode V=u;                        v.cur[i]-=minv;//must first subtract V.CUR[J]+=MINV; v.step=u.step+1;//Update Step number//cout<<u.step<<endl; if (!vis[v.cur[0]][v.cur[1]][v.cur[2]]) {//The node is not accessed Q.push (v);//Join Queue V        is[v.cur[0]][v.cur[1]][v.cur[2]]=1;//has been visited}}}}    } cout<<res<<endl;         }return 0;}



NYOJ21 Three Water cups (classic problem BFS)

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.