HDU 1252Hike on a Graph
来源:互联网 发布:阿里云控制台中文乱码 编辑:程序博客网 时间:2024/05/29 10:06
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.
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 3r b rb b br b r2 1 2 2y gg y0
Sample Output
2impossible
简单bfs,读懂题目就行了。
#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int sz=55;int f[sz][sz][sz];int n;char s[sz],mp[sz][sz];struct point{ int x,y,z; point(int x=0,int y=0,int z=0):x(x),y(y),z(z){};};int main(){ while (~scanf("%d",&n),n) { point a; scanf("%d%d%d",&a.x,&a.y,&a.z); for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { scanf("%s",s); mp[i][j]=s[0]; } } memset(f,-1,sizeof(f)); queue<point> p; p.push(a); f[a.x][a.y][a.z]=0; bool flag=false; while (!p.empty()) { point q=p.front(); p.pop(); if (q.x==q.y&&q.y==q.z) { printf("%d\n",f[q.x][q.y][q.z]); flag=true; break; } for (int i=1;i<=n;i++) { if (mp[q.x][i]==mp[q.y][q.z]&&f[i][q.y][q.z]==-1) { f[i][q.y][q.z]=f[q.x][q.y][q.z]+1; p.push(point(i,q.y,q.z)); } if (mp[q.y][i]==mp[q.x][q.z]&&f[q.x][i][q.z]==-1) { f[q.x][i][q.z]=f[q.x][q.y][q.z]+1; p.push(point(q.x,i,q.z)); } if (mp[q.z][i]==mp[q.x][q.y]&&f[q.x][q.y][i]==-1) { f[q.x][q.y][i]=f[q.x][q.y][q.z]+1; p.push(point(q.x,q.y,i)); } } } if (!flag) printf("impossible\n"); } return 0;}
0 0
- HDU 1252 Hike on a Graph
- hdu 1252 Hike on a Graph(bfs)
- HDU 1252Hike on a Graph
- bfs hdu 1252 Hike on a Graph--easy
- HDU 1252 Hike on a Graph 题目读懂就能做
- [ZOJ1103] Hike on a graph
- HDU1252-Hike on a Graph
- Hike on a Graph--HDU1252
- HDU-1252 || ZOJ-1103 || POJ-2415 Hike on a Graph(bfs)
- POJ-2415 Hike on a Graph 搜索
- ZOJ 1103 Hike on a Graph(BFS)
- hdu1252 Hike on a Graph ----BFS
- BFS基础题 杭电2612 Find a way 1252 Hike on a Graph
- ZOJ 1103 Hike on a Graph(POJ 2415) (广搜)
- chapter05-Hike on a Graph(POJ 2415)
- POJ 2415 Hike on a Graph(bfs)
- ZOJ 1103 Hike on a Graph(bfs)
- HDU-5957 Query on a graph(线段树+树剖)
- 1043. Is It a Binary Search Tree (25)
- Python基础:格式化字符与转义字符
- gcc #pragma pack c++模板template
- linker command faild with exit code
- Android系统移植与平台开发(一)- 初识HAL
- HDU 1252Hike on a Graph
- 如何将所有的进程运行在一个指定的CPU上
- Java调用Telnet(windows版和Linux版)
- find
- PAT乙级练习题B1054. 求平均值
- hdu_1280_前m大的数
- 微服务设计-思维导图笔记
- hdu-3314-并查集
- js打开app应用或进入下载页面