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,求到达终点的最少耗时. 解题思路: ...
随机推荐
- tomcat7 access log设置
位置:${tomcat_home}/conf/server.xml <Valve className="org.apache.catalina.valves.AccessLogValv ...
- SDUT 3930 线段树
皮卡丘的梦想2 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一天,一只住在 501 实验 ...
- 轮廓算法的结果验证工具/How to validate the outline output
因为轮廓算法的结果通过直接观察输出很难判断结果的正确性. 但是如果把输入和输出同时绘制出来,用眼睛判别则相对简单许多. 输入建筑的文件内容格式为,粗体格式为建筑高度: 10 110 5020 60 7 ...
- emqtt新版升级一些事项和操作
注解 Erlang/OTP R19依赖lksctp-tools库 yum install lksctp-tools 控制台地址: http://127.0.0.1:18083,默认用户: admin, ...
- iOS 点击cell上的按钮获取行数
-(void)btnClick:(UIButton *)button{ UITableViewCell *cell = (UITableViewCell *)[[button superview] s ...
- btrace 常见问题
执行btrace命令报错:Unable to open socket file: target process not responding or HotSpot VM not loaded ---- ...
- 【BZOJ4720】【NOIP2016】换教室 [期望DP]
换教室 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行四个整数n,m,v ...
- hdu 2157 How many ways?? ——矩阵十题第八题
Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, ...
- Spring Boot提供的特性
一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...
- python中正则用法举例
一.根据正则表达式替换字符串 import re text='abc123' text=re.sub(r'\d','-',text) print(text) 输出:abc---将每个数字替换为-,如果 ...