This question is the topic of the search for BFS. The essence of BFS is to find the next all possible and then store it up, a bit of a violent feeling, the problem is that every step of all may be in the queue, and then judge. The test instructions of this problem is:
Give you a full picture, and then give you three disk, the purpose is to move the three disk to a point, output a minimum number of steps! Disk movement when there are requirements, such as moving the first disk, the 1 plate moved to 2 this position, (to) this point has a color mark, and another two disks such as 3, 42 points, then 1 plate can move to 2 this point of the condition is (3,4) the color of this point is the same as the color!!
#include"iostream"#include"stdio.h"#include"algorithm"#include"string.h"#include"Cmath"#include"string"#include"Queue"#defineMX 105using namespacestd;intn,p1,p2,p3;structnode{intp[3]; intstep;};CharG[MX][MX];BOOLVIS[MX][MX][MX];//flag If a situation has occurredvoidSet (Node A) {vis[a.p[0]][a.p[1]][a.p[2]]=false;}BOOLJudge (Node A)//determine if this condition has occurred{ returnvis[a.p[0]][a.p[1]][a.p[2]];}BOOLEND (Node A)//determine if the operation is complete{ if(a.p[0]==a.p[1]&&a.p[1]==a.p[2])return true; return false;}voidBFs () {Queue<node>Q; while(!q.empty ()) Q.pop (); Node Cur,next; cur.p[0]=p1;cur.p[1]=p2;cur.p[2]=p3;cur.step=0; Q.push (cur); Set (cur); inti; while(!Q.empty ()) {cur=Q.front (); Q.pop (); if(End (cur)) {cout<<cur.step<<Endl; return; } for(i=1; i<=n;i++) {Next=cur; next.p[0]=i; Next.step++; if(Judge (next) &&g[cur.p[0]][i]==g[cur.p[1]][cur.p[2]]) {Set (next); Q.push (next); } } for(i=1; i<=n;i++) {Next=cur; next.p[1]=i; Next.step++; if(Judge (next) &&g[cur.p[1]][i]==g[cur.p[0]][cur.p[2]]) {Set (next); Q.push (next); } } for(i=1; i<=n;i++) {Next=cur; next.p[2]=i; Next.step++; if(Judge (next) &&g[cur.p[2]][i]==g[cur.p[1]][cur.p[0]]) {Set (next); Q.push (next); } }} cout<<"Impossible"<<Endl;}intMain () {inti,j; while(cin>>n,n) {memset (Vis,true,sizeof(VIS)); CIN>>p1>>p2>>P3; for(i=1; i<=n;i++) { for(j=1; j<=n;j++) {cin>>g[i][j];//input for a single character, does not read into spaces, and has been mistakenly thought to be read together with a space}} BFS (); } return 0;}
View Code
Hdu hike on a Graph