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 题目大意:一张完全图,三个人,图的边上有颜色,三个人的位置已知,每次只能让一个人移动一次,求总共最少移动多少次,三个人能碰面。移动的规则是:当前人要走的边的颜色与其他两个人之间的边的颜色相同时,才能移动。
题目分析:大水题!!!wa了几次都是因为没有搞懂移动规则,把这道题记下来给自己涨个记性!!! 代码如下:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,c,t;
node(int _a,int _b,int _c,int _t):a(_a),b(_b),c(_c),t(_t){}
bool operator < (const node &a) const {
return t>a.t;
}
};
int n,vis[][][];
char p[][];
void bfs(int a,int b,int c)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
vis[a][b][c]=;
q.push(node(a,b,c,));
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.a==u.b&&u.b==u.c&&u.a==u.c){
printf("%d\n",u.t);
return ;
}
for(int i=;i<=n;++i){
if(p[u.a][i]==p[u.b][u.c]&&!vis[i][u.b][u.c]){
vis[i][u.b][u.c]=;
q.push(node(i,u.b,u.c,u.t+));
}
}
for(int i=;i<=n;++i){
if(p[u.b][i]==p[u.a][u.c]&&!vis[u.a][i][u.c]){
vis[u.a][i][u.c]=;
q.push(node(u.a,i,u.c,u.t+));
}
}
for(int i=;i<=n;++i){
if(p[u.c][i]==p[u.a][u.b]&&!vis[u.a][u.b][i]){
vis[u.a][u.b][i]=;
q.push(node(u.a,u.b,i,u.t+));
}
}
}
printf("impossible\n");
}
int main()
{
int a,b,c;
while(scanf("%d",&n)&&n)
{
scanf("%d%d%d",&a,&b,&c);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
cin>>p[i][j];
bfs(a,b,c);
}
return ;
}

POJ-2415 Hike on a Graph (BFS)的更多相关文章

  1. [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  2. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  3. 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

  4. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  5. Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph)

    Leetcode之广度优先搜索(BFS)专题-133. 克隆图(Clone Graph) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tree ...

  6. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. SNMP学习笔记之SNMP报文以及不同版本(SNMPv1、v2c、v3)的区别

    本篇文章将重点分析SNMP报文,并对不同版本(SNMPv1.v2c.v3)进行区别! 四.SNMP协议数据单元 在SNMP管理中,管理站(NMS)和代理(Agent)之间交换的管理信息构成了SNMP报 ...

  2. Jsoup简介

    Jsoup简介 一.概述 Jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来 ...

  3. 安装使用composer基本流程

    composer工作原理: 这里经过几个步骤:1.composer读取composer.json(这个文件手动建立,官网有格式),这个json是在当前执行composer目录的,如果目录下没有这个js ...

  4. 20145127《java程序设计》第一次实验

    <java程序设计>第一次实验 实验内容及其步骤 1.使用JDK编写简单的Java小程序: Java编译的方法有很多,最基础最简单的就是使用命令行,记事本,Java虚拟机直接进行编译,下面 ...

  5. 关于Session的概念和测试点

    Session概要 Session 是用于保持状态的基于 Web 服务器的方法,在 Web 服务器上保持用户的状态信息供在任何时间从任何页访问. Session 允许通过将对象存储在 Web 服务器的 ...

  6. 【前端】纯html+css+javascript实现楼层跳跃式的页面布局

    实现效果演示: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>楼层跳跃式的页面布局</title&g ...

  7. Python3基础 函数 无参数无返回值 调用会输出hello world的函数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. linux下递归列出目录下的所有文件名(不包括目录)

    1.linux下递归列出目录下的所有文件名(不包括目录) ls -lR |grep -v ^d|awk '{print $9}'2.linux下递归列出目录下的所有文件名(不包括目录),并且去掉空行 ...

  9. 再谈vim中多窗口的编辑 ctrl+w+H窗口位置最大化和互换等操作

    参考:http://blog.csdn.net/shuangde800/article/details/11430659 很好 鼠标在各个窗口间循环移动: ctrl+w+(小写的 hjkl), &qu ...

  10. java必背面试题

    JAVA必背面试题和项目面试通关要点 一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选 ...