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,求到达终点的最少耗时. 解题思路: ...
随机推荐
- YBT 5.3 数位动态规划
记忆化搜索的专题 题解在代码中 Amount of Degrees[loj 10163] /* 此题可以转换成将10进制转成b进制后有k个1其他都为0的个数 所以用记忆化dfs dp[pos][sum ...
- POJ3694:Network(并查集+缩点+lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 4774 题目链接:htt ...
- POJ 3111 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 10507 Accepted: 2709 Case Time ...
- HDU 2686 / NYOJ 61 DP
传纸条(一) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...
- 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow
Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Yes ...
- Linux网络监控工具nethogs
Linux网络监控工具nethogs 标签: 监控工具linux 2015-12-17 22:06 448人阅读 评论(0) 收藏 举报 分类: linux(40) 版权声明:本文为博主原创文章, ...
- sshSSH Secure Shell Client root用户无法登录解决办法
最近使用这个工具,普通用户可以登录root用户不可以登录.将vi /etc/ssh/sshd_config按照下述配置解决问题 修改sshd配置文件:vi /etc/ssh/sshd_config P ...
- [LeetCode] 15. 3Sum ☆☆
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- Axis2 WebService(配置、发布、调用)
准备工作 1.下载:axis2-1.5.4-bin.zip,axis2-1.5.4-war.zip 下载地址:http://axis.apache.org/axis2/java/core/ 2.环境变 ...
- HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...