hdu 1252(BFS)
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
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 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.
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.
r b r
b b b
r b r
2 1 2 2
y g
g y
0
impossible
题意:现在我们有一个图,这个图里面每一个点都有连接(包括自己),现在给你三个点 p1 p2 p3 ,这三个点要移动到同一个点,移动的规则是如果 p1 和 p2 之间的边颜色是 b ,那么p3 只能走颜色为 b的边,现在给你一个图,问三个点到一起最短的时间,如果不可能到同一点输出 impossible
#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)的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
随机推荐
- 51nod 1274 最长递增路径(DP)
一开始自己想了一种跑的巨慢..写了题解的做法又跑的巨快..一脸懵逼 显然要求边权递增就不可能经过重复的边了,那么设f[i]为第i条边出发能走多远就好了,这是我一开始的写法,可能dfs冗余状态较多,跑的 ...
- 题解【bzoj2427 [HAOI2010]软件安装】
Description 现在我们的手头有\(N\)个软件,对于一个软件\(i\),它要占用\(W_i\)的磁盘空间,它的价值为\(V_i\).我们希望从中选择一些软件安装到一台磁盘容量为\(M\)计算 ...
- Redis 模糊匹配 SearchKeys
语法:KEYS pattern说明:返回与指定模式相匹配的所用的keys.该命令所支持的匹配模式如下:(1)?:用于匹配单个字符.例如,h?llo可以匹配hello.hallo和hxllo等:(2)* ...
- 函数式编程--响应式编程 ---android应用例子
RxJava implements this operator as create. It is good practice to check the observer’s isUnsubscribe ...
- HihoCoder 重复旋律
あの旋律を何度も繰り返しでも.あの日見た光景を再現できない 无论将那段旋律重复多少次,也无法重现那一日我们看到的景象 もし切ないならば.時をまきもどしてみるかい? 若是感到惆怅的话,要试着让时光倒流吗 ...
- bzoj 2730: [HNOI2012]矿场搭建——tarjan求点双
Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...
- MySQL查询 45道练习题
SQL查询45道练习题 1.查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from student2.查询教师所有的单位即不重复 ...
- HDU 1574 RP问题 (dp)
题目链接 Problem Description 在人类社会中,任何个体都具有人品,人品有各种不同的形式,可以从一种形式转换为另一种形式,从一个个体传递给另一个个体,在转换和传递的过程中,人品不会消失 ...
- 24、redis中的sentinel的作用?
redis中的sentinel的作用? Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Re ...
- 如何升级nodejs版本 安装n模块报错 npm ERR! notsup Unsupported platform
如何升级nodejs版本 首先安装n模块, 输入npm install -g n n模块专门用来管理nodejs的版本. 如果出现npm ERR! notsup Unsupported platfor ...