“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
题意挺难理解的。
题意:给定一个n*n的二维数组,(i,j)代表的是第i个位置到第j个位置的路径颜色。有三个点一开始位于三个坐标上(有可能相同),每一次移动只能移动一个点,移动的条件是:这个点到移向点的路径颜色要等于另外两个点之间的路径颜色。要使得这三个点在同一坐标上,求最少移动次数。如果不行就输出impossible。
思路:每次只能移动一个点,我们用一个三维数组记录这三个点到达的坐标,如果之前这三个坐标同时到达过,就不要再重复走了。然后bfs去枚举这三个点的移动路径,如果三个点坐标相同,直接返回就好了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=51;
struct node{int x,y,z;int num;node(int as,int bs,int cs,int d){x=as,y=bs,z=cs,num=d;}
};
int vis[maxx][maxx][maxx];
string s[maxx];
int n,as,bs,cs;inline int bfs()
{queue<node> q;vis[as][bs][cs]=1;q.push(node(as,bs,cs,0));while(q.size()){node u=q.front();q.pop();if(u.x==u.y&&u.y==u.z) return u.num;for(int i=1;i<=n;i++){if(s[i][u.x]==s[u.y][u.z]&&vis[i][u.y][u.z]==0){vis[i][u.y][u.z]=1;q.push(node(i,u.y,u.z,u.num+1));}if(s[i][u.y]==s[u.x][u.z]&&vis[u.x][i][u.z]==0){vis[u.x][i][u.z]=1;q.push(node(u.x,i,u.z,u.num+1));}if(s[i][u.z]==s[u.x][u.y]&&vis[u.x][u.y][i]==0){vis[u.x][u.y][i]=1;q.push(node(u.x,u.y,i,u.num+1));}}}return -1;
}
int main()
{while(scanf("%d",&n),n){scanf("%d%d%d",&as,&bs,&cs);char c;for(int i=1;i<=n;i++){s[i]="";for(int j=1;j<=n;j++) cin>>c,s[i]=s[i]+c;s[i]=' '+s[i];}memset(vis,0,sizeof(vis));int ans=bfs();if(ans==-1) cout<<"impossible"<<endl;else cout<<ans<<endl;}return 0;
}

努力加油a啊,(o)/~

Hike on a Graph HDU - 1252(bfs)相关推荐

  1. hdu 2612(bfs)Find a way

    题意:就是Y和M在@相遇的最短的时间. 思路:基本的广搜题,先Y搜一次,然后M搜一次,最后求出Y和M在@相遇的最短的时间. 代码实现: #include<iostream> #includ ...

  2. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  3. 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)

    [HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...

  4. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)

    一.图的基本介绍 为什么要有图 前面我们学了线性表和树 线性表局限于一个直接前驱和一个直接后继的关系 树也只能有一个直接前驱也就是父节点 当我们需要表示多对多的关系时, 这里我们就用到了图. 图的举例 ...

  5. 广度优先搜索(BFS)与深度优先搜索(DFS)

    一.广度优先搜索(BFS) 1.二叉树代码 # 实现一个二叉树 class TreeNode:def __init__(self, x):self.val = xself.left = Nonesel ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)算法详解

    深度优先搜索(DFS)与广度优先搜索(BFS)详解 1.广度优先搜索算法 1.1.前言 和树的遍历类似,图的遍历也是从图中某点出发,然后按照某种方法对图中所有顶点进行访问,且仅访问一次. 但是图的遍历 ...

  7. 广度优先算法(BFS)

    根据访问节点的顺序与方式,可以分为广度优先算法(BFS)和深度优先算法(DFS),本文我打算先介绍广度优先算法: 广度优先算法 1. 算法概述 广度优先搜索算法(又称宽度优先搜索.BFS)是最简便的图 ...

  8. 【算法】广度优先搜索(BFS)和深度优先搜索(DFS)

    https://blog.csdn.net/raphealguo/article/details/7523411 https://blog.csdn.net/qq_41681241/article/d ...

  9. python扫雷 广度优先_Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)...

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

最新文章

  1. 关于jQuery在Asp.Net Mvc 框架下Ajax文件上传的实现
  2. 理解Vue 2.5的Diff算法
  3. 微信小程序php后台支付,微信小程序 支付功能实现PHP实例详解
  4. select ... into ... 与 insert into .... select .... 语句
  5. ios 扫码枪外设 键盘模式_多平台连接:雷柏XK100蓝牙键盘评测
  6. 设计模式的C语言应用-建造者模式-第七章
  7. 将科学计数法的数值转化为字符
  8. Objects as Points论文总结
  9. 继续教育计算机组成试卷,计算机继续教育考试模拟练习.doc
  10. Python网络爬虫《九》
  11. 低版本的iphone 无法跑在xcode8上
  12. 统计学习方法第一章:概述
  13. OpenERP中多币种处理(外币处理)
  14. 【小工具】- Ubuntu如何查看cpu支持的指令集
  15. 【计算机毕业设计】133在线课程管理系统
  16. Kafka消费组rebalance原理
  17. vc++6.0/使用VisualC++6.0创建MFC基本对话框程序制作数字钟表教程
  18. 一些方法:JQ: append() 、appendTo() || JS:appendChild():
  19. Java+Selenium3自动化入门9-Selenium中截图方法-TakeScreenshot
  20. 每日微软面试题——day 3

热门文章

  1. TCP/UDP网络编程入门教程之二:TCP Server端——socket与文件描述符
  2. IOS开发基础之网易新闻环境搭建异步请求json,AFN网络封装第1天
  3. gitlab 构建tag_Gitlab详细操作
  4. 程序员基本功10栈和队列
  5. (个人总结)Linux命令——任意目录查看穿越
  6. 17年三月计算机二级,2017年3月计算机二级考试攻略
  7. 解决在非Activity中使用startActivity
  8. Java基础之HashMap流程分析
  9. 计算机博士论文答谢,这篇博士论文《致谢》刷屏,句句扎心
  10. c语言vi运行编译文件,VC++6.0中如何编译运行及调试C语言程序文件.docx