[ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)
Description
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
Output

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盘子到同一位置的最少步数 广搜)的更多相关文章
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- hdu Hike on a Graph
此题是道bfs搜索的题目.bfs的精髓就是找到下一步的所有可能然后存储起来,有点暴力的感觉,这题就是每步中 所有的可能都入队,然后一一 判断.这道题的题意是 : 给你一幅完全图,再给你三个盘,目的是把 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3984 迷宫问题 记录路径的广搜
主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...
随机推荐
- ado.net access oracle dataset via store procedure
使用存储过程返回结果集,并绑定到ado.net对象中在sql server里面是非常直观的. 1: create procedure GetAllRecords 2: as 3: select * f ...
- HTML第二天
针对html做了一个知识点的思维导图
- 【python3】collections系列介绍
文章来源:http://www.jb51.net/article/48771.htm (http://www.cnblogs.com/wushank/p/5122786.html) 修改人:天马流行拳 ...
- 一款好用的杀毒软件-pchunter64
360真是猛,用system权限运行了一坨后台进程.今天清理系统盘想删掉360发现根本没权限.进程又杀不掉. 最后使用 pchuter64 强行干掉了360 避免了 重装系统的尴尬.360作为世界第一 ...
- python第十二天-----RabbitMQ
有一位小伙伴说让我去A站写博客可能会有很多人喜欢,真是搞不懂,北方哪里有卖萌?北方默认状态就是这么萌的!再者说了,这明明就是很专注于技术的博客嘛,能不能严肃点!知不知道什么叫帧? 学习到了数据库的相关 ...
- oracle 同时更新(update)多个字段多个值
--创建表A,B: create table A (a1 varchar2(33),a2 varchar2(33),a3 varchar2(33)); create table B (b1 varch ...
- 基于阿里云服务器的git服务器搭建
使用阿里云Ubuntu 12.0.4 64位操作系统做git服务器. 首先git服务器有两种访问方式可以选择:http方式和ssh的方式,http方式更容易使用. 1.http方式的git服务器搭建以 ...
- 爬虫---request+++urllib
网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...
- [转]VS 2013 连接数据库报错 未能加载文件或程序集 Microsoft.SqlServer.Management.Sdk.Sfc
原文链接:http://stackoverflow.com/questions/16906686/could-not-load-file-or-assembly-microsoft-sqlserver ...
- 自定义ImageView回调实现手动改变圆环大小
//-----------------自定义MyView继承Imageview------------------------------- package com.bw.yuanhuan; impo ...