UVA-12569 Planning mobile robot on Tree (EASY Version) (BFS+状态压缩)
题目大意:一张无向连通图,有一个机器人,若干个石头,每次移动只能移向相连的节点,并且一个节点上只能有一样且一个东西(机器人或石头),找出一种使机器人从指定位置到另一个指定位置的最小步数方案,输出移动步骤。
题目分析:以机器人的所在位置和石头所在位置集合标记状态,状态数最多有15*2^15个。广搜之。
代码如下:
# include<iostream>
# include<cstdio>
# include<string>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; struct Edge
{
int to,nxt;
}; struct node
{
int t,u,sta;
string path;
node(int _t,int _u,int _s,string _p):t(_t),u(_u),sta(_s),path(_p){}
bool operator < (const node &a) const {
return t>a.t;
}
};
Edge e[32];
int n,cnt,head[16],vis[15][1<<15]; void add(int u,int v)
{
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void bfs(int s,int st,int ed)
{
priority_queue<node>q;
memset(vis,0,sizeof(vis));
vis[s][st]=1;
q.push(node(0,s,st,""));
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.u==ed){
printf("%d\n",u.t);
for(int i=0;i<u.path.size();i+=2)
printf("%d %d\n",u.path[i]-'A'+1,u.path[i+1]-'A'+1);
return ;
}
for(int i=0;i<n;++i){
if(u.sta&(1<<i)){
for(int j=head[i];j!=-1;j=e[j].nxt){
int v=e[j].to;
if(u.sta&(1<<v))
continue;
if(v==u.u)
continue;
int ns=u.sta^(1<<i);
ns|=(1<<v);
if(!vis[u.u][ns]){
vis[u.u][ns]=1;
string p=u.path;
p+=(char)(i+'A'),p+=(char)(v+'A');
q.push(node(u.t+1,u.u,ns,p));
}
}
}
}
for(int i=head[u.u];i!=-1;i=e[i].nxt)
{
int v=e[i].to;
if(u.sta&(1<<v))
continue;
if(!vis[v][u.sta]){
vis[v][u.sta]=1;
string p=u.path;
p+=(char)(u.u+'A'),p+=(char)(v+'A');
q.push(node(u.t+1,v,u.sta,p));
}
}
}
printf("-1\n");
}
int main()
{
int T,a,b,s,t,st,m,cas=0;
scanf("%d",&T);
while(T--)
{
st=cnt=0;
memset(head,-1,sizeof(head));
scanf("%d%d%d%d",&n,&m,&s,&t);
--s,--t; while(m--)
{
scanf("%d",&a);
st|=(1<<(a-1));
}
for(int i=1;i<n;++i){
scanf("%d%d",&a,&b);
add(a-1,b-1);
add(b-1,a-1);
}
printf("Case %d: ",++cas);
bfs(s,st,t);
if(T)
printf("\n");
}
return 0;
}
UVA-12569 Planning mobile robot on Tree (EASY Version) (BFS+状态压缩)的更多相关文章
- Uva 12569 Planning mobile robot on Tree (EASY Version)
基本思路就是Bfs: 本题的一个关键就是如何判段状态重复. 1.如果将状态用一个int型数组表示,即假设为int state[17],state[0]代表机器人的位置,从1到M从小到大表示障碍物的位置 ...
- UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)
Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138 Submit:686 Time Limit: 300 ...
- UVA Planning mobile robot on Tree树上的机器人(状态压缩+bfs)
用(x,s)表示一个状态,x表示机器人的位置,s表示其他位置有没有物体.用个fa数组和act数组记录和打印路径,转移的时候判断一下是不是机器人在动. #include<bits/stdc++.h ...
- 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)
传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...
- UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)
读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...
随机推荐
- 使用 SSH 和 SFTP 协议
通过 SSH 和 SFTP 协议,我们能够访问其他设备,有效而且安全的传输文件等等. 几年前,我决定配置另外一台电脑,以便我能在工作时访问它来传输我所需要的文件.要做到这一点,最基本的一步是要求你的网 ...
- Python3 异常: name 'basestring' is not defined
Python3 异常: name 'basestring' is not defined 问题分析: python3 里已经没有basestring 类型,用str代替了basestring : 解决 ...
- Eclipse启动Tomcat时,45秒超时解决方式
Eclipse启动Tomcat时,45秒超时解决方式 在Eclipse中启动Tomcat服务器时,经常由于系统初始化项目多,导致出现45秒超时的Tomcat服务器启动错误. 一般通过找到XML配置文 ...
- vc++之stdafx.h
关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...
- 20145322 Exp5 利用nmap扫描
20145322 Exp5 利用nmap扫描 实验过程 使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsole开启m ...
- 20145328 《网络对抗技术》MSF基础应用
20145328 <网络对抗技术>MSF基础应用 --------------先提交,后续完成------------------
- vs下C++内存泄露检测
本文原链接: http://www.cnblogs.com/zouzf/p/4152279.html 参考文章: http://msdn.microsoft.com/zh-cn/library/x98 ...
- C# 给某个方法设定执行超时时间
ManualResetEvent.WaitOne 方法 https://msdn.microsoft.com/en-us/library/system.threading.manualreseteve ...
- 51nod 1021 石子归并 区间DP
1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆 ...
- Unity 之 transform
transform.Translate 1.function Translate (translation : Vector3, relativeTo : Space = Space.Self) : ...