POJ 2415 Hike on a Graph(bfs)

本文介绍了一个基于图的游戏'HikeonaGraph'的一人版挑战,玩家需移动三个棋子至同一位置,遵循特定的颜色规则。文章提供了算法解决方案,使用BFS遍历所有可能的状态,以找出最少的移动步骤。

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

Description

"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 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

Source


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
/*
    题意:
        给出n个点的图,一个完全图,任意两点之间都有边
        边都是有颜色的
        给出3个点,问能否将3个点移动到同一个点
        能则输出最小步数
        某点沿边移动的要求是另外2个点之间边的颜色等于正在移动的点要走的颜色
    分析:
        3个点,因为任意两个点之间都是有边的,所以bfs的每一步中
        枚举3个点下一个点,若可行且该状态未访问过则入队
        其中vis[i][j][k]表示三个点分别在i,j,k位置的状态是否访问过

*/
const int N = 55;
char mp[N][N];
bool vis[N][N][N];
int n;
struct Node
{
    int p[3];
    int t;
};
int p[3];
bool ok(Node nx)
{
    return nx.p[0] == nx.p[1]&&nx.p[0] == nx.p[2];
}
int bfs()
{
    queue<Node>q;
    mem(vis,0);
    Node h;
    for (int i = 0;i < 3;++i) h.p[i] = p[i];
    h.t = 0;vis[h.p[0]][h.p[1]][h.p[2]] = 1;
    q.push(h);
    while (!q.empty())
    {
        h = q.front();
        q.pop();int a = h.p[0],b = h.p[1],c = h.p[2];//a,b,c三个点
        for (int i = 1;i <= n;++i)//枚举下一步可到达的点
        {
            if (mp[a][i] == mp[b][c]&&!vis[i][b][c])//a走到i点
            {
                Node nx = h;
                nx.p[0] = i;nx.t++;
                if (ok(nx)) return nx.t;
                vis[i][b][c] = 1;
                q.push(nx);
            }
            if (mp[b][i] == mp[a][c]&&!vis[a][i][c])//b走到i点
            {
                Node nx = h;
                nx.p[1] = i;nx.t++;
                if (ok(nx)) return nx.t;
                vis[a][i][c] = 1;
                q.push(nx);
            }
            if (mp[c][i] == mp[a][b]&&!vis[a][b][i])//c走到i点
            {
                Node nx = h;
                nx.p[2] = i;nx.t++;
                if (ok(nx)) return nx.t;
                vis[a][b][i] = 1;
                q.push(nx);
            }
        }
    }
    return -1;
}
int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        cin>>p[0]>>p[1]>>p[2];
        for (int i = 1;i <= n;++i)
        {
            for (int j = 1;j <= n;++j)
            {
                cin>>mp[i][j];
            }
        }
        if (p[0] == p[1]&&p[0] == p[2])
        {
            puts("0");
            continue;
        }
        int ans = bfs();
        if (ans == -1)
        {
            puts("impossible");
        }
        else printf("%d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值