NYOJ 213 Water Cup BFS

Source: Internet
Author: User

Click to open link

Idea: Writing this question is to record the idea of this problem, to tell the truth I really do not feel that it is a bfs .... Or Hao God said is a BFS, exchange a bit, a think is also really a BFS, because the requirements of the number of steps and BFS can also record the state of each step, or the least number of times ....

#include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std;
int v[3],e[3];
int book[111][111][111];
	struct Node {int w[3];
int s;
}q,p;
		void push (int a,int b) {if (V[b]-q.w[b]>=q.w[a]) {q.w[b]+=q.w[a];
	q.w[a]=0;
		} else {q.w[a]-=v[b]-q.w[b];
		
	Q.W[B]=V[B];
} return;
	} void BFs () {queue<node>q;
	memset (book,0,sizeof (book));
	Q.W[0]=V[0];
	q.w[1]=0;
	q.w[2]=0;
	q.s=0;
	Book[v[0]][0][0]=1;
	Q.push (q); while (!
		Q.empty ()) {//cout<<1<<endl;
		P=q.front ();
		Q.pop ();
		 if (P.w[0]==e[0]&&p.w[1]==e[1]&&p.w[2]==e[2]) {cout<<p.s<<endl;
		return;
		 	} for (int i=0;i<3;i++) {for (int j=0;j<3;j++) {if (i==j) continue;
		 	Q=p;
		 	Push (I,J);
		 		if (!book[q.w[0]][q.w[1]][q.w[2]]) {q.s++;
		 		Q.push (q);
			 Book[q.w[0]][q.w[1]][q.w[2]]=1;
}}}} cout<<-1<<endl;
	} int main () {int t;
	cin>>t; WhiLe (t--) {scanf ("%d%d%d", &v[0],&v[1],&v[2]);
		scanf ("%d%d%d", &e[0],&e[1],&e[2]);
	BFS ();
} return 0; }


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.