ZOJ 1103 Hike on a Graph(bfs)

本文介绍了一款名为'HikeonaGraph'的游戏,并详细解析了其背后的算法逻辑。通过BFS算法,解决了如何用最少步骤使三个棋子在同一位置会合的问题。文章提供了完整的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 3
r b r
b b b
r b r
2 1 2 2
y g
g y
0

Sample Output

2
impossible

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值