Hike on a Graph

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

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
 

题意:现在我们有一个图,这个图里面每一个点都有连接(包括自己),现在给你三个点 p1 p2 p3 ,这三个点要移动到同一个点,移动的规则是如果 p1 和 p2 之间的边颜色是 b ,那么p3 只能走颜色为 b的边,现在给你一个图,问三个点到一起最短的时间,如果不可能到同一点输出 impossible

 
题解:看懂题意后根据题意进行广搜就OK。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <map>
using namespace std;
const int N=;
int n,a,b,c;
bool vis[N][N][N];
map<string ,int> mp;
char str[];
int graph[N][N];
struct Node
{
int a,b,c;
int step;
};
int bfs()
{
memset(vis,false,sizeof(vis));
queue<Node> q;
Node s;
s.a = a,s.b = b,s.c = c,s.step = ;
vis[s.a][s.b][s.c] = true;
q.push(s);
while(!q.empty())
{
Node now = q.front();
q.pop();
if(now.a==now.b&&now.b==now.c) return now.step;
Node next;
for(int i=; i<=n; i++)
{
if(graph[now.a][i]==graph[now.b][now.c]) ///a->(b,c)
{
next = now;
next.a = i;
next.step+=;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c] = true;
q.push(next);
}
}
if(graph[now.b][i]==graph[now.a][now.c]) ///b->(a,c)
{
next = now;
next.b = i;
next.step+=;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c] = true;
q.push(next);
}
}
if(graph[now.c][i]==graph[now.a][now.b]) ///c->(a,b)
{
next = now;
next.c = i;
next.step+=;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c] = true;
q.push(next);
}
}
}
}
return -;
}
int main()
{
while(scanf("%d",&n)!=EOF,n)
{
mp.clear();
scanf("%d%d%d",&a,&b,&c);
int tot = ;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%s",str);
if(mp[str]==)
{
mp[str] = ++tot;
graph[i][j] = tot;
}
else graph[i][j] = mp[str];
}
}
int ans = bfs();
if(ans==-)
{
printf("impossible\n");
}
else printf("%d\n",ans);
}
}

hdu 1252(BFS)的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  4. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  5. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  6. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  7. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  8. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  9. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

随机推荐

  1. bzoj2216: [Poi2011]Lightning Conductor(分治决策单调性优化)

    每个pi要求 这个只需要正反DP(?)一次就行了,可以发现这个是有决策单调性的,用分治优化 #include<iostream> #include<cstring> #incl ...

  2. apache和IIS共享80端口解决办法

    第一步:把iis所发布的网站默认端口由80改为8080:第二步:修改apache的httpd.conf配置文件. 首先,要让apache支持转发也就是做iis的代理那么就要先启 用apache的代理模 ...

  3. JavaScript Date的原型方法扩展

    在JavaScript开发中,经常需要对Date类型的对象进行各种验证或格式化,但是js并没有提供那么多的那么细的函数,所以只好自己去用 prototype 扩充了,下面是我自己实现的Date类型常用 ...

  4. PhoneGap API介绍:Camera

    本文将介绍PhoneGap API——Camera:使用设备的摄像头采集照片,对象提供对设备默认摄像头应用程序的访问. 方法: camera.getPicture 参数: cameraSuccess ...

  5. SqlServer中循环和条件语句示例!

    -- ╔════════╗ -- =============================== ║ if语句使用示例 ║ -- ╚════════╝ declare @a int set @a=12 ...

  6. [Android问答] px、dp和sp,这些单位有什么区别?

    相信每个Android新手都会遇到这个问题,希望这篇帖子能让你不再纠结. px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小 ...

  7. 如何把自己写的python程序给别人用

    这里讲的给别人用,不是指将你的代码开源,也不是指给另一个程序员用..... 前段时间写了个程序,输入URP学生系统的账号和密码,输出课表.绩点之类的信息,想给同学用,但是总不能叫别人也去装python ...

  8. 使用 html2canvas 实现浏览器截图

    基于上一篇<h5 本地上传图片预览 源码下载>,今天分享一个图片上传后, 根据所上传的图片颜值随机生成一个答案, 并且可以生成一张专属于自己的名片. 首先上传预览我们已经实现了, 所以接下 ...

  9. 读取txt内容放入set中

    package pingbi; /** * 将txt文本读入导入到set中 * 问题: * 第一个地方有会多一个 ?--解决问题很简单,但不知道问题的原因 */ import java.io.Buff ...

  10. [洛谷P2571] [SCOI2010]传送带

    题目描述 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段.两条传送带分别为线段AB和线段CD.lxhgww在AB上的移动速度为P,在CD上的移动速度为Q,在平面上的移动速度R.现在lxh ...