题目大意:一张无向连通图,有一个机器人,若干个石头,每次移动只能移向相连的节点,并且一个节点上只能有一样且一个东西(机器人或石头),找出一种使机器人从指定位置到另一个指定位置的最小步数方案,输出移动步骤。

题目分析:以机器人的所在位置和石头所在位置集合标记状态,状态数最多有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+状态压缩)的更多相关文章

  1. Uva 12569 Planning mobile robot on Tree (EASY Version)

    基本思路就是Bfs: 本题的一个关键就是如何判段状态重复. 1.如果将状态用一个int型数组表示,即假设为int state[17],state[0]代表机器人的位置,从1到M从小到大表示障碍物的位置 ...

  2. 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 ...

  3. UVA Planning mobile robot on Tree树上的机器人(状态压缩+bfs)

    用(x,s)表示一个状态,x表示机器人的位置,s表示其他位置有没有物体.用个fa数组和act数组记录和打印路径,转移的时候判断一下是不是机器人在动. #include<bits/stdc++.h ...

  4. 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)

    传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...

  5. UVA 810 A Dicey Promblem 筛子难题 (暴力BFS+状态处理)

    读懂题意以后还很容易做的, 和AbbottsRevenge类似加一个维度,筛子的形态,可以用上方的点数u和前面的点数f来表示,相对的面点数之和为7,可以预先存储u和f的对应右边的点数,点数转化就很容易 ...

  6. 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 ...

  7. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

  9. 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 ...

随机推荐

  1. 浅谈CORS

    浅谈CORS CORS全称"跨站资源共享"(Cross-Origin Resource Sharing),它允许浏览器克服浏览器同源策略向跨域服务器发出请求. 同源策略 概念 说到 ...

  2. (二)github的价值意义篇

    为什么需要社会化编程? 如果您是程序员面试官,两者之间你会选择哪一位呢? 能查看以前所写代码的程序员 or 无法查看的程序员 精通最新软件的程序员 or 不精通的程序员 对语言或软件差异带来的不同文化 ...

  3. ES6学习--对象属性的可枚举性( enumerable)

    可枚举性(enumerable)用来控制所描述的属性,是否将被包括在for...in循环之中.具体来说,如果一个属性的enumerable为false,下面三个操作不会取到该属性.* for..in循 ...

  4. phpstorm常用快捷键(自备不全)

    CTRL+N 查找类 CTRL+SHIFT+N 全局搜索文件 ,优先文件名匹配的文件 CTRL+SHIFT+ALT+N 查找php类名/变量名 ,js方法名/变量名, css 选择器 CTRL+G 定 ...

  5. Jsp中如何通过Jsp调用Java类中的方法

    Jsp中如何通过Jsp调用Java类中的方法 1.新建一个项目,在src文件夹下添加一个包:如:cn.tianaoweb.com; 2.再在包中添加一个类:如 package com; public ...

  6. 如何安装多个jdk

    1.首先去官网下载不同版本的jdk 下载地址:http://www.oracle.com/technetwork/java/javase/archive-139210.html 2.下载后我的安装路径 ...

  7. CF 316E3 Summer Homework(斐波那契矩阵+线段树)

    题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...

  8. No compatible targets were found Do you wish to a add new Android Virtual Device ?

    运行一个Android小程序时提示: No compatible targets were found Do you wish to a add new Android Virtual Device ...

  9. LightOJ 1393 Crazy Calendar(博弈)题解

    题意:r*c方格中,每个格子有一定石子,每次移动每格任意数量石子,只能向下或者向右动一格,不能移动为败 思路:显然是Nim,到右下曼哈顿距离为偶数的不用管,因为先手动一下后手动一下最后移到右下后还是先 ...

  10. HDU 3404 Switch lights(Nim积)题解

    题意:在一个二维平面中,有n个灯亮着并告诉你坐标,每回合需要找到一个矩形,这个矩形xy坐标最大的那个角落的点必须是亮着的灯,然后我们把四个角落的灯状态反转,不能操作为败 思路:二维Nim积,看不懂啊, ...