ZOJ 1103 Hike on a Graph(bfs)

来源:互联网 发布:在哪里学淘宝美工 编辑:程序博客网 时间:2024/06/09 13:58

Hike on a Graph

Time Limit: 2 Seconds      Memory Limit: 65536 KB

"Hike on a Graph" is a game that is played on a board on which an undirected graph is drawn. The graph is complete and has all loops, i.e. for any two locations there is exactly one arrow between them. The arrows are coloured. There are three players, and each of them has a piece. At the beginning of the game, the three pieces are in fixed locations on the graph. In turn, the players may 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 may only be moved along arrows of the same colour as the arrow between the two opponents' pieces.

In the sixties ("make love 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 of course only one at a time. Goal of this game is to get all pieces onto the same location, using as few moves as possible. Find out the smallest number of moves that is necessary to get all three pieces onto the same location, for a given board layout and starting positions.

Input Specification

The input file contains several test cases. Each test case starts with the number n. Input is terminated by n=0. Otherwise, 1<=n<=50. Then follow three integers p1, p2, p3 with 1<=pi<=ndenoting the starting locations of the game pieces. The colours of the arrows are given next as a m×m 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 Specification

For each test case output on a single line the minimum number of moves required to get all three pieces onto the same location, or the word "impossible" if that is 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

Source: University of Ulm Local Contest 2000


思路:直接bfs即可,具体看代码。


AC代码:

#include <bits/stdc++.h>using namespace std; char g[51][51];unsigned int step[51][51][51];struct str{char a,b,c;}List[51*51*51];int n,a,b,c;int flag;//确定能否实现 int main(){while(scanf("%d", &n) && n){scanf("%d%d%d", &a, &b, &c);memset(step,255,sizeof(step));//初始化 char arrow;for(int i=1; i<=n; i++)for(int j=1; j<=n; j++){cin>>arrow;//cin不读取空格和换行 g[i][j]=arrow;}flag = 0;unsigned int head = 0;unsigned int tail = 0;//入队列 List[head].a = a;List[head].b = b;List[head].c = c;step[a][b][c] = 0;while(head<=tail){//出队列 a = List[head].a;b = List[head].b;c = List[head].c;if ((a==b)&&(a==c)) {flag = 1; break;}//a,b,c在同一位置,结束 unsigned int current = step[a][b][c];current++;for(int i=1; i<=n; i++)if(i!=a && g[a][i]==g[b][c] && step[i][b][c]>current){step[i][b][c]=current;tail++;List[tail].a=i;List[tail].b=b;List[tail].c=c;}for(int i=1; i<=n; i++)if(i!=b && g[b][i]==g[a][c] && step[a][i][c]>current){step[a][i][c]=current;tail++;List[tail].a=a;List[tail].b=i;List[tail].c=c;}for(int i=1; i<=n; i++)if(i!=c && g[c][i]==g[a][b] && step[a][b][i]>current){step[a][b][i]=current;tail++;List[tail].a = a;List[tail].b = b;List[tail].c = i;}head++;}if (flag) printf("%u\n", step[a][b][c]);else printf("impossible\n");}return 0;}