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. SpringBoot之集成WebSocket

    websocket是什么不做介绍.开发环境:jdk1.8,win7_64旗舰版,idea   1.初始化一个springboot项目   2.加入websocket依赖 <!-- springb ...

  2. [转载]论asp.net out、ref、return

      论asp.net out.ref.return ref(引用类型) ref引用类型进出都必须赋值,赋值后会改变类型原来的指针.   out(值类型) out值类型进可以不赋值,出必须赋值.   r ...

  3. web前端----css选择器样式

    一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...

  4. java程序初始化顺序

    使用场景:  在java程序中,当实例化对象时,对象的所在类的所有成员变量首先要进行初始化,只有当所有类成员完成初始化后, 才会调用对象所在类的构造函数创建对象. 初始化的原则: (1)静态对象优先于 ...

  5. DBCP、c3p0、Druid三大连接池区别

    DBCP.c3p0.Druid三大连接池区别 一.连接池优势 如果一个项目中如果需要多个连接,如果一直获取连接,断开连接,这样比较浪费资源: 如果创建一个池,用池来管理Connection,这样就可以 ...

  6. 微信小程序新闻列表功能(读取文件、template模板使用)

    微信小程序新闻列表功能(读取文件.template) 不忘初心,方得始终.初心易得,始终难守. 在之前的项目基础上进行修改,实现读取文件内容作为新闻内容进行展示. 首先,修改 post.wxml 文件 ...

  7. 20145321《网络对抗技术》逆向与Bof基础

    20145321<网络对抗技术>逆向与Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...

  8. 20165310java_blog_week6

    2165310 <Java程序设计>第6周学习总结 教材学习内容总结 String 构造 String str=new String() String (char a[]) String ...

  9. script 加载顺序问题的延展研究

    今天群里有人问为什么会出现脚本的加载顺序与定义脚本顺序不一致的问题,这个问题引起了我的好奇,经过一番调研,有了这篇文章. 这是一个伪命题吗? 首先,W3C 推荐 script 脚本应该被立即加载和执行 ...

  10. 【第二十八章】 springboot + zipkin(brave定制-AsyncHttpClient)

    brave本身没有对AsyncHttpClient提供类似于brave-okhttp的ClientRequestInterceptor和ClientResponseInterceptor,所以需要我们 ...