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 ...
随机推荐
- SQL学习之Can't connect to MySQL server on localhost (10061)
最近升级 了系统,开机后连接MySQL报错,Can't connect to MySQL server on localhost (10061): 估计是升级系统清除了以前的缓存设置,网上很多方法是命 ...
- Python Web学习笔记之并发和并行的区别和实现
你吃饭吃到一半,电话来了,你一直到吃完了以后才去接,这就说明你不支持并发也不支持并行.你吃饭吃到一半,电话来了,你停了下来接了电话,接完后继续吃饭,这说明你支持并发.你吃饭吃到一半,电话来了,你一边打 ...
- Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED”
Python3 打开 https 链接,异常:“SSL: CERTIFICATE_VERIFY_FAILED” 一.问题 Python2.7.9 之后,当使用urllib.urlopen打开一个 ht ...
- Tomcat分windows版和linux版
Tomcat分windows版和linux版 官方下载链接:http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.37/bin/ windows下载e ...
- Python3基础 time 索引值访问元组中的年月日时分秒
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 螺旋折线|2018年蓝桥杯B组题解析第七题-fishers
标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次. 对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0, ...
- [js] - 关于js的排序sort
js的排序sort并不能一次排序好 function solution(nums){ return nums.sort(sortNumber); } function sortNumber(a, b) ...
- Unity3D学习笔记(十七):IK动画、粒子系统和塔防
新动画系统: 反向动力学动画(IK功能): 魔兽世界(头部动画),神秘海域(手部动画),人类一败涂地(手部动画) 如何启用(调整) 1.必须是新动画系统Animator 设置头.手.肘的目标点 2.动 ...
- 数据库与hadoop与分布式文件系统的区别和联系
转载一篇关系数据库与Hadoop的关系的文章 1. 用向外扩展代替向上扩展 扩展商用关系型数据库的代价是非常昂贵的.它们的设计更容易向上扩展.要运行一个更大的数据库,就需要买一个更大的机器.事实上,往 ...
- BZOJ 3992 【SDOI2015】 序列统计
题目链接:序列统计 我来复习板子了……这道题也是我写的第一发求原根啊? 求原根方法: 从小到大依次枚举原根.设当前枚举的原根为\(x\),模数为\(p\),\(p-1\)的质因数分别为\(p_1,p_ ...