Hike on a Graph HDU - 1252(bfs)

“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
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<=n denoting 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
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 3
r b r
b b b
r b r
2 1 2 2
y g
g y
0
Sample Output
2
impossible
题意挺难理解的。
题意:给定一个n*n的二维数组,(i,j)代表的是第i个位置到第j个位置的路径颜色。有三个点一开始位于三个坐标上(有可能相同),每一次移动只能移动一个点,移动的条件是:这个点到移向点的路径颜色要等于另外两个点之间的路径颜色。要使得这三个点在同一坐标上,求最少移动次数。如果不行就输出impossible。
思路:每次只能移动一个点,我们用一个三维数组记录这三个点到达的坐标,如果之前这三个坐标同时到达过,就不要再重复走了。然后bfs去枚举这三个点的移动路径,如果三个点坐标相同,直接返回就好了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=51;
struct node{
	int x,y,z;
	int num;
	node(int as,int bs,int cs,int d)
	{
		x=as,y=bs,z=cs,num=d;
	}
};
int vis[maxx][maxx][maxx];
string s[maxx];
int n,as,bs,cs;

inline int bfs()
{
	queue<node> q;
	vis[as][bs][cs]=1;
	q.push(node(as,bs,cs,0));
	while(q.size())
	{
		node u=q.front();
		q.pop();
		if(u.x==u.y&&u.y==u.z) return u.num;
		for(int i=1;i<=n;i++)
		{
			if(s[i][u.x]==s[u.y][u.z]&&vis[i][u.y][u.z]==0)
			{
				vis[i][u.y][u.z]=1;
				q.push(node(i,u.y,u.z,u.num+1));
			}
			if(s[i][u.y]==s[u.x][u.z]&&vis[u.x][i][u.z]==0)
			{
				vis[u.x][i][u.z]=1;
				q.push(node(u.x,i,u.z,u.num+1));
			}
			if(s[i][u.z]==s[u.x][u.y]&&vis[u.x][u.y][i]==0)
			{
				vis[u.x][u.y][i]=1;
				q.push(node(u.x,u.y,i,u.num+1));
			}
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d",&n),n)
	{
		scanf("%d%d%d",&as,&bs,&cs);
		char c;
		for(int i=1;i<=n;i++)
		{
			s[i]="";
			for(int j=1;j<=n;j++) cin>>c,s[i]=s[i]+c;
			s[i]=' '+s[i];
		}
		memset(vis,0,sizeof(vis));
		int ans=bfs();
		if(ans==-1) cout<<"impossible"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

发布了652 篇原创文章 · 获赞 101 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105310908