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,求到达终点的最少耗时. 解题思路: ...
随机推荐
- eclipse里配置Android ndk环境,用eclipse编译.so文件
做Android NDK开发时,c代码需要用ndk-build来进行编译,而java代码则需要用Android sdk编译. 编译c代码有两种方法: 一.写好c代码后,然后用cygwin搭建ndk-b ...
- 【图论-最短路】【P3393】逃离僵尸岛
传送门 Description 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被僵 ...
- 【区间DP】【lgP3146】248
传送门 Description 给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少.注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3. In ...
- ACM1198Farm Irrigation
这个题目好吓人呀!嘿嘿--- 不过仔细分析下就可以啦! #include<iostream> #include<cstring> using namespace std; ; ...
- java加载驱动
加载驱动方法 1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.r ...
- NOJ/HUST 1095 校赛 Just Go 线段树模板题
Description There is a river, which contains n stones from left to right. These stones are magic, ea ...
- sqlserver xml转表 及(cross apply与outer apply)
一. 需求是需要把','分割的字符串转为表,便于做关联查询,于是发现可以通过xml转为表,如下: declare @XXX xml set @XXX = ' <v> <aa>1 ...
- Tomcat报错java.lang.ClassNotFoundException: 2localhost.org.apache.juli.FileHandler
Can't load log handler "1catalina.org.apache.juli.FileHandler" java.lang.ClassNotFoundExce ...
- AWK文本分析工具-常用场景(持续更新中)
AWK help document:http://www.gnu.org/software/gawk/manual/gawk.html 问题 awk命令 备注 对请求IP统计分组排序? 显示列 ...
- 【NOIP】提高组2015 子串
[题意]求从字符串A中取出k个互不重叠的非空子串顺序拼接形成B的方案数.n<=1000,m<=100,k<=m. [算法]动态规划 [题解]这题主要是将从i-l转移变成从i-1转移, ...