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; }