HDU-1252 || ZOJ-1103 || POJ-2415 Hike on a Graph(bfs)
来源:互联网 发布:恋夜秀场怎样抓取源码 编辑:程序博客网 时间:2024/06/03 05:45
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.
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 3
r b r
b b b
r b r
2 1 2 2
y g
g y
0
Sample Output
2
impossible
题目大意:有一张图,上面的路径都是着色的,开始的时候有3个盘子在确定的点上,现在让你按要求沿图中的路径移动盘子(一步只能移动一只盘子),问是否能将3个盘子都移到同一个点上,如果可以,输出需要的最少步数,否则输出“impossible”。移动条件是:每个盘子只能沿着这样一条路移动,这条路的颜色和另外的两个盘子之间的路径上标记的颜色是一样的。
#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <queue>#include <algorithm>#define N 52const int mm = 1000000007;using namespace std;struct node{ int a, b, c;}s;int n, p1, p2, p3, ret;char map[N][N];int ans[N][N][N];void bfs(){ int i, j, k; ret = n; queue<node> q; node t1, t2; s.a--; s.b--; s.c--; for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) for (k = 0; k <= n; k++) ans[i][j][k] = mm; q.push(s); ans[s.a][s.b][s.c] = 0; while(!q.empty()) { t1 = q.front(); q.pop(); if (t1.a == t1.b && t1.b == t1.c) { ret = t1.a; return ; } int cur = ans[t1.a][t1.b][t1.c] + 1; char bc = map[t1.b][t1.c]; //结点b、c的颜色 for (i = 0; i < n; i++) { if (i != t1.a && bc == map[t1.a][i] && ans[i][t1.b][t1.c] > cur) { ans[i][t1.b][t1.c] = cur; t2 = t1; t2.a = i; q.push(t2); } } char ac = map[t1.a][t1.c]; //结点a、c之间的颜色 for (i = 0; i < n; i++) { if (i != t1.b && ac == map[t1.b][i] && ans[t1.a][i][t1.c] > cur) { ans[t1.a][i][t1.c] = cur; t2 = t1; t2.b = i; q.push(t2); } } char ab = map[t1.a][t1.b]; //结点a、b之间的颜色 for (i = 0; i < n; i++) { if (i != t1.c && ab == map[t1.c][i] && ans[t1.a][t1.b][i] > cur) { ans[t1.a][t1.b][i] = cur; t2 = t1; t2.c = i; q.push(t2); } } }}int main(){#ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin);#endif ios::sync_with_stdio(false); cin.tie(0); int i, j, k; while(cin >> n, n) { cin >> s.a >> s.b >> s.c; for (i = 0; i < n; i++) { cin >> map[i][0]; for (j = 1; j < n; j++) cin >> map[i][j]; } bfs(); if (ans[ret][ret][ret] == mm) cout << "impossible" << endl; else cout << ans[ret][ret][ret] << endl; } return 0;}
- HDU-1252 || ZOJ-1103 || POJ-2415 Hike on a Graph(bfs)
- ZOJ 1103 Hike on a Graph(BFS)
- hdu 1252 Hike on a Graph(bfs)
- ZOJ 1103 Hike on a Graph(POJ 2415) (广搜)
- ZOJ 1103 Hike on a Graph(bfs)
- POJ 2415 Hike on a Graph(bfs)
- bfs hdu 1252 Hike on a Graph--easy
- POJ-2415 Hike on a Graph 搜索
- HDU 1252 Hike on a Graph
- HDU 1252Hike on a Graph
- hdu1252 Hike on a Graph ----BFS
- chapter05-Hike on a Graph(POJ 2415)
- HDU 1252 Hike on a Graph 题目读懂就能做
- BFS基础题 杭电2612 Find a way 1252 Hike on a Graph
- [ZOJ1103] Hike on a graph
- HDU1252-Hike on a Graph
- Hike on a Graph--HDU1252
- ZOJ 1103(POJ 2415)(HDU 1252)Hike…
- 【Maven权威指南】Apache Maven简介、安装和运行
- 1455: 罗马游戏 左偏树
- Java学习笔记(49)------------异常处理机制
- 【js】数据的轮播展示(模拟Marquee,无间断滚动内容)
- html注意事项
- HDU-1252 || ZOJ-1103 || POJ-2415 Hike on a Graph(bfs)
- Service绑定形式启动过程
- Java学习笔记(50)-----------JDBC
- 阻塞和非阻塞套接字一次可以发送多大的数据量
- Android数据存储简单介绍
- arcgis Silverlight添加箭头
- 学习/记忆力的增强
- IOS开发中(null)与<null>的处理
- 从0到1:微信后台系统的演进之路