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 ...
随机推荐
- MySQL数据库----事务
事务 -- 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,-- 即可回滚到原来的状态,从而保证数据库数据完整性.-- 事务也就是要么都成功,要么都不成功-- 事务就是由一堆sql ...
- echart知识点、常用图形
原文地址:https://www.cnblogs.com/kewenxin/p/9338272.html 本文是自己在项目中需要运用到的echarts图形进行整理,都有完整的代码.echarts原型, ...
- Git 的安装步骤
Git 的安装步骤 一.下载Git Git 的官网:https://git-scm.com/ 在 Git 的官网中点击Downloads,进入如下页面: 选择对应的操作系统,以博主为例,点击Windo ...
- 05: 配置yum源
1.1 将镜像复制到本地创建yum源 1.将准备好的系统镜像放到指定的目录,本次目录指定在:/dawnfs/sourcecode 2.创建挂载目录:mkdir /mnt/yum 3.挂载镜像: mou ...
- windows 上安装redis和windows上redis与php扩展
1.下载redis压缩包(自己选择想要的版本,1,2地址任意选一个) 下载window版本地址1:https://github.com/dmajkic/redis/downloads 下载window ...
- 20145315《网络对抗》——注入shellcode以及 Return-to-libc攻击实验
shellcode 准备一段Shellcode 我用的老师的shellcode:\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3 ...
- Java实现心跳机制
一.心跳机制简介 在分布式系统中,分布在不同主机上的节点需要检测其他节点的状态,如服务器节点需要检测从节点是否失效.为了检测对方节点的有效性,每隔固定时间就发送一个固定信息给对方,对方回复一个固定信息 ...
- SQLException: com.mchange.v2.c3p0.ComboPooledDataSource [ java.beans.IntrospectionException: java.lang.reflect.InvocationTargetException [numThreadsAwaitingCheckoutDefaultUser] ] has been closed()
问题:Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mchange.v2.c3p0.Com ...
- linux下如何查看当前机器提供了哪些服务
答:使用netstat工具 在命令行下输入netstat -atun即可列出当前机器提供的服务 netstat各选项解析: -a 列出所有服务 -t 列出tcp相关 -u 列出udp相关 -n 以数字 ...
- POJ 2348 Euclid's Game(博弈)题解
题意:有a,b两个数字,两人轮流操作,每次可以选择两个之中较小的数字,然后另一个数字减去选择数字的任意倍数(不能减到负数),直到其中一个为0,不能操作为败 思路:这题用博弈NP思想,必败点和必胜点之间 ...