Description
"Hike on a Graph" was a game that was played on a board in which an undirected Graph is drawn. The graph is a complete and have all loops, i.e. to any, and locations there is exactly one arrow between them. The arrows is coloured. There is three players, and each of the them has a piece. At the beginning of the game, the three pieces is in a fixed locations on the graph. In turn, the players could do a move. A move consists of moving one ' s own piece along an arrow to a new location on the board. The following constraint is imposed on this:the piece could only be moved along arrows of the same colour as the arrow
between the opponents ' pieces.
The sixties ("Make Love Is not war") a one-person variant of the game emerged. In this variant one person moves all the three pieces, not necessarily one after the other, but for course only one at a ti Me. Goal of this game are to get all pieces onto the same location, using as few moves as possible. Find out the smallest number of moves that's necessary to get all three pieces onto the same location, for a given board Layout and starting positions.
Input
The input contains several test cases. Each test case starts with the number N. Input was terminated by n=0. Otherwise, 1<=n<=50. Then follow three integers p1, p2, p3 with 1<=pi<=n denoting the starting locations of the game pieces. The colours of the arrows is given next as a mxm matrix of whitespace-separated lower-case letters. The element mij denotes the colour of the arrow between the locations I and J. Since the graph is undirected, you can assume the matrix to be symmetrical.
Output
For each test case, output on a, the minimum number of moves required to get all three pieces onto the same loca tion, or the word "impossible" if that's not possible for the given board and starting locations.
Sample Input
3 1 2 3r b RB b BR b R2 1 2 2y GG y0
Sample Output
2impossible
The main idea: a complete picture, three people, the picture of the side of the color, three people are known, each time only one person can move, for a total of the minimum number of moves, three people can meet. The rule of movement is: the color of the edge that the person is going to go to is the same color as the edge of the other two people to move.
Topic Analysis: Flood problem!!! WA a few times because did not understand the rules of movement, put this problem down to give yourself a memory!!!
The code is as follows:
1# include<iostream>2# include<cstdio>3# include<queue>4# include<cstring>5# include<algorithm>6 using namespacestd;7 structnode8 {9 inta,b,c,t;TenNodeint_a,int_b,int_c,int_t): A (_a), B (_b), C (_c), T (_t) {} One BOOL operator< (ConstNode &a)Const { A returnT>A.T; - } - }; the intn,vis[ -][ -][ -]; - Charp[ -][ -]; - voidBFsintAintBintc) - { +Priority_queue<node>Q; -memset (Vis,0,sizeof(Vis)); +vis[a][b][c]=1; AQ.push (Node (a,b,c,0)); at while(!q.empty ()) - { -Node u=q.top (); - Q.pop (); - if(u.a==u.b&&u.b==u.c&&u.a==u.c) { -printf"%d\n", u.t); in return ; - } to for(intI=1; i<=n;++i) { + if(p[u.a][i]==p[u.b][u.c]&&!VIS[I][U.B][U.C]) { -vis[i][u.b][u.c]=1; theQ.push (Node (i,u.b,u.c,u.t+1)); * } $ }Panax Notoginseng for(intI=1; i<=n;++i) { - if(p[u.b][i]==p[u.a][u.c]&&!VIS[U.A][I][U.C]) { thevis[u.a][i][u.c]=1; +Q.push (Node (u.a,i,u.c,u.t+1)); A } the } + for(intI=1; i<=n;++i) { - if(p[u.c][i]==p[u.a][u.b]&&!Vis[u.a][u.b][i]) { $vis[u.a][u.b][i]=1; $Q.push (Node (u.a,u.b,i,u.t+1)); - } - } the } -printf"impossible\n");Wuyi } the intMain () - { Wu inta,b,c; - while(SCANF ("%d", &n) &&N) About { $scanf"%d%d%d",&a,&b,&c); - for(intI=1; i<=n;++i) - for(intj=1; j<=n;++j) -Cin>>P[i][j]; A BFS (a,b,c); + } the return 0; -}
POJ-2415 hike on a Graph (BFS)