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. 06: Pymysql

    1.1 Pymysql安装与简介 1.安装 pip3 install pymysql 2.介绍(支持python3) 1. pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  2. SQL、SQL Server、MySQL与Oracle

    SQL (Structured Query Language),结构化查询语言,用来与多种数据库建立联系,根据ANSI(美国国家标准协会)的规定,SQL为RDBMS(关系型数据库)的标准语言. --- ...

  3. 实验二 Java 面向对象程序设计

    实验内容 1 初步掌握单元测试和TDD 2 理解并掌握面向对象三要素:封面,继承,多态 3 初步掌握UML建模 4 熟悉SOLID原则 5 了解设计模式 (一)单元测试 D

  4. 在一个activity中销毁指定activity

    通过静态变量的方法: 1.在Aactivity中设置一个Activity静态变量 static Activity activity; 2.在onCreate中: activity=this: 3.在B ...

  5. PHP开发者的路书

    初学者 作为初学者,通常情况下,我们都会买一本PHP教材,或者在网上看免费教程,这当然是学习的好途径.因为,这些书籍和网上的免费教程,基本上都是由浅入深的渐进式教学方式,基础知识居多,高级知识占少量的 ...

  6. bootstrap的carousel图片轮播

    整个轮播是放在一个div .carousel和.slide的div中的, 包括3个部分: 1. 第一个部分indicator位于下方的指示器部分. 结构是一个ol和li, ol的类是carousel- ...

  7. RabbitMQ延时任务

    概念: 消息的TTL(Time To Live)消息的TTL就是消息的存活时间.RabbitMQ可以对队列和消息分别设置TTL.对队列设置就是队列没有消费者连着的保留时间,也可以对每一个单独的消息做单 ...

  8. [微信开发] - 微信支付 JSAPI 形式

    微信官方的JSAPI文档 微信官方的JSAPI支付SDK与DEMO下载 查看JSAPI的API可以从这里看 下载了支付DEMO其实有些地方不对的,比如如果做沙盒测试的时候,需要使用getsignkey ...

  9. Unity3D学习笔记(十二):2D模式和异步资源加载

    2D模式和3D模式区别:背景纯色,摄像机2D,没有深度轴 精灵图片设置 Normal map,法线贴图,更有立体感 Sprite (2D and UI),2D精灵贴图,有两种用途 1.当做UI贴图 2 ...

  10. OpenFlow protocol version 1.0 通信过程

    参考 李呈:[原创]OpenFlow通信流程解读 OpenFlow protocol version 1.0 通信过程 通信双方: OpenFlow控制器,OpenFlow交换机. 通信模块: Sec ...