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 ...
随机推荐
- C/C++之标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完 成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花 ...
- angular Js 回车处理
不说多的,就一个代码: <input type="search" class="am-form-field" placeholder="输入搜索 ...
- web前端----JavaScript(JS)简单介绍
JavaScript(JS) 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEa ...
- (一)github之基础概念篇
1.github: 一项为开发者提供git仓库的托管服务, 开发者间共享代码的场所.github上公开的软件源代码全都由git进行管理. 2.git: 开发者将源代码存入名为git仓库的资料库中,而g ...
- 20145329 《网络对抗技术》浏览器MS11_050安全漏洞攻击
两台虚拟机: kali ip:192.168.96.130 windows xp sp3(包含IE7)ip:192.168.96.128 1.在kali终端中开启msfconsole. 2.进入漏洞模 ...
- Android 手机小闹钟
Android 手机小闹钟 一.这一篇主要使用系统为我们提供的一个服务AlarmManager来制作一个Android小闹钟,同时还涉及到了自定义主题.判断第一次启动应用.自定义动画.对话框.制作关闭 ...
- Python3基础 父,子类普通方法重名 子类方法覆盖父类方法
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- bootstrap5
列表组的使用 ul.list-group > li.list-group-item *5... 列表组中可以放置徽标: 在li中放置 span.badge. bootstrap中的情景类: 实际 ...
- JavaScript:正则表达式 分组2
继续上一篇的写,这篇复杂点. 分组+范围 var reg=/([abcd]bc)/g; var str="abcd bbcd cbcd dbcd"; console.log(str ...
- JVM类加载机制总结
1.运行时加载优点 提高灵活性,可以在运行时动态加载,连接.例子:面向接口编程,动态绑定实现类(但C++也有动态绑定,说明动态绑定不一定通过运行时加载Class字节码实现,也可能是机器码支持的) 2. ...