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 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

Source

 
题目大意:有一张图,上面的路径都是着色的,开始的时候有3个盘子在确定的点上,现在让你按要求沿图中的路径移动盘子(一步只能移动一只盘子),问是否能将3个盘子都移到同一个点上,如果可以,输出需要的最少步数,否则输出“impossible”。>_<移动条件是:每个盘子只能沿着这样一条路移动,这条路的颜色和另外的两个盘子之间的路径上标记的颜色是一样的。
解题思路:因为这道题给的是完全图,也就是说图上每两个点之间都有路径存在,它们标记的都有颜色。所以直接BFS就AC啦!
相关链接:过程详解 http://www.cnblogs.com/pcoda/archive/2012/09/02/2667987.html
 #include<iostream>
#include<queue>
#include<string>
#include<string.h>
using namespace std;
int n,p1,p2,p3;
char map[][];//存储图
int ans[][][];//保存a[i][j][k]表示3个盘子在i,j,k位置时的最小步数
int ok;//记录最终3个盘子的位置,如果为0则imposible
struct state{
int a,b,c;
}temp;//3个盘子的位置
void read(){
cin>>p1>>p2>>p3;
for(int i=;i<=n;i++){
map[i][]='#';//在这填充个字符,不然就报错
for(int j=;j<=n;j++)
cin>>map[i][j];
map[i][n+]='\0';//在行尾加一个结束符,便于后面操作
}
}
void bfs(){
ok=;
fill(&ans[][][],&ans[][][]+**,);//将ans初始化很大
ans[p1][p2][p3]=;//令刚开始位置为0(不要少了)
queue<state> Q;
temp.a=p1;temp.b=p2;temp.c=p3;
Q.push(temp);
while(!Q.empty()){
state top=Q.front();Q.pop();
int x=top.a,y=top.b,z=top.c;
if(x==y && y==z){//如果3盘到一点就跳出
ok=x;
break;
}else{
int cur_ans=ans[x][y][z];
cur_ans++; char bc_color=map[y][z];
string str_a=map[x];//(与a连的所有路径)
for(int i=;i<=n;i++){
//遍历所有路径,如果不是自己,且满足移动条件,且ans[i][y][z]>cur_ans就移动
if(i!=x && str_a[i]==bc_color && ans[i][y][z]>cur_ans){
ans[i][y][z] = cur_ans;
temp.a=i;temp.b=y;temp.c=z;
Q.push(temp);
}
}//a盘的移动 char ac_color=map[x][z];
string str_b=map[y];
for(int i=;i<=n;i++){
if(i!=y && str_b[i]==ac_color && ans[x][i][z]>cur_ans){
ans[x][i][z]=cur_ans;
temp.a=x;temp.b=i;temp.c=z;
Q.push(temp);
}
}//b盘的移动 char ab_color=map[x][y];
string str_c=map[z];
for(int i=;i<=n;i++){
if(i!=z && str_c[i]==ab_color && ans[x][y][i]>cur_ans){
ans[x][y][i]=cur_ans;
temp.a=x;temp.b=y;temp.c=i;
Q.push(temp);
}
}//c盘的移动
}
}
}
int main(){
while(cin>>n && n){
read();
bfs();
if(ok)cout<<ans[ok][ok][ok]<<'\n';
else cout<<"impossible\n";
}return ;
}

 

[ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)的更多相关文章

  1. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

  2. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  3. hdu Hike on a Graph

    此题是道bfs搜索的题目.bfs的精髓就是找到下一步的所有可能然后存储起来,有点暴力的感觉,这题就是每步中 所有的可能都入队,然后一一 判断.这道题的题意是 : 给你一幅完全图,再给你三个盘,目的是把 ...

  4. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  5. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  6. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  7. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  8. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

随机推荐

  1. VC++ 中简单操作MP3音乐的方法,小结

    #include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellap ...

  2. js 构造函数

    //构造函数  //使自己的对象多次复制,同时实例根据设置的访问等级可以访问其内部的属性和方法  //当对象被实例化后,构造函数会立即执行它所包含的任何代码  function myObject(ms ...

  3. CentOS 安装 chrome 浏览器

    安装 google-chrome 浏览器,由于众所周知的原因,一直安装不了,下面介绍一种新方法. cd 到 /etc/yum.repos.d 创建一个yum新源 vi chromium-el6.rep ...

  4. jQuery的append和appendTo

    这两个关键词,Insus.NET刚开始学习jQuery时,也被它弄得不好理解.现用得多了,运行与理解也不难了. 查了英文词典append的意思是“添加,附加”: 而后者appendTo意思是“ 添加至 ...

  5. CentOS 7将网卡名称eno16777736改为eth0

    http://www.linuxidc.com/Linux/2015-09/123396.htm Linux系统版本:CentOS_7(64位) 一.前言: 今天将CentOS 6.5装回了CentO ...

  6. ABP的Zero Sample

    下载自:https://github.com/aspnetboilerplate/module-zero 打开D:\ABP\module-zero-master\sample里的ModuleZeroS ...

  7. Web开发中的主要概念

    一.Web开发中的主要概念1.静态资源:一成不变的.html.js.css2.动态资源:JavaWeb.输出或产生静态资源.(用户用浏览器看到的页面永远都是静态资源) 3.JavaEE:十三种技术的集 ...

  8. Hadoop中HDFS的管理

    本文讲述怎么在Linux Shell中对HDFS进行操作. 三种命令格式: hadoop fs适用于任何不同的文件系统,比如本地文件系统和HDFS文件系统 hadoop dfs只能适用于HDFS文件系 ...

  9. 中国排名前100的IT公司 (转)

    排序 单位名称 软件收入 1 华为技术有限公司 622360  2 中兴通讯股份有限公司 601331  3 海信集团有限公司 448641  4 UT斯达康通讯有限公司 386763  5 海尔集团 ...

  10. 使用adb 查询data/data下的数据库

    1.用cmd打开adb 2.输入adb shell 3.cd到数据库所在目录 4.输入sqlite3 person.db(person.db为要操作的db名称,根据需要修改) 5.输入sql语句(每个 ...