hdu 1252 Hike on a Graph(bfs)

本文介绍了一个基于图的游戏“HikeonaGraph”的算法解决方案,该游戏中玩家需将三个棋子移动到同一位置,每次移动需遵循特定的颜色规则。通过广度优先搜索(BFS)算法,文章详细阐述了如何找出最少移动次数的方法。

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

Hike on a Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 418    Accepted Submission(s): 189


Problem 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 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
题目分析:裸的bfs,题意比较难懂,当挪动一点点时,他只能走与另外两个点之间的边颜色相同的边
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAX 57

using namespace std;


int n,a,b,c;
int used[MAX][MAX][MAX];
char mp[MAX][MAX];

struct Node
{
    int x,y,z,t;
    Node ( int a , int b , int c , int d )
        :x(a),y(b),z(c),t(d){}
};

void bfs ( )
{
    queue<Node> q;
    memset ( used , -1 , sizeof ( used ) );
    q.push ( Node ( a , b , c , 0 ) );
    used[a][b][c] = 0;
    while ( !q.empty() )
    {
        int x = q.front().x , y = q.front().y , z = q.front().z;
        int t = q.front().t;
        q.pop();
        for ( int i = 1 ; i <= n ; i++ )
        {
            if ( i == x || mp[x][i] != mp[y][z] ) continue;
            if ( used[i][y][z] != -1 ) continue;
            q.push ( Node ( i , y , z , t+1 ) );
            used[i][y][z] = t+1;
        }
        for ( int i = 1 ; i <= n ; i++ )
        {
            if ( i == y || mp[y][i] != mp[x][z] ) continue;
            if ( used[x][i][z] != -1 ) continue;
            q.push( Node ( x , i , z , t+1 ) );
            used[x][i][z] = t+1;
        }
        for ( int i = 1 ; i <= n ; i++ )
        {
            if ( i == z || mp[z][i] != mp[x][y] ) continue;
            if ( used[x][y][i] != -1 ) continue;
            q.push ( Node ( x , y , i , t+1 ) );
            used[x][y][i] = t+1;
        }
    }
}

int main ( )
{
    char s[5];
    while ( ~scanf ( "%d" , &n ), n )
    {
        scanf ( "%d%d%d" , &a , &b , &c );
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= n ; j++ )
            {
                scanf ( "%s" , s );
                mp[i][j] = s[0];
            }
        bfs();
        int ans = 1000000;
        for ( int i = 1 ; i <= n ; i++ )
            if ( used[i][i][i] != -1 )
                ans = min ( ans , used[i][i][i] );
        if ( ans == 1000000 ) puts ( "impossible" );
        else printf ( "%d\n" , ans );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值