题意:有一颗n个节点的树,其中一个节点有机器人,指定m个节点有障碍物,指定终点,问最少需要移动多少步才能到达,移动过程中遇到障碍物需要将其移动到空地。

分析:bfs搜索,是每次都对障碍物进行移动,将障碍物移动到空闲地方,如果是机器人移动就需要更新位置。

障碍物位置用二进制储存,方便查找,更新。

# include<iostream>
# include<cstdio>
# include<cmath>
# include<map>
# include<queue>
# include<string>
# include<string.h>
#include<set>
#include<list>
# include<algorithm>
using namespace std;
const int maxn = 1 << 20;
struct node {int state, pos, pre, len;node(){}node(int _state,int _pos,int _pre,int _len):state(_state),pos(_pos),pre(_pre),len(_len){}
};
vector<int>G[20];
node q[maxn];
int vis[20][maxn];
int n, obnum, s, t;
int start,step;
void bfs(int ob) {int front = 0, rear = 1;node temp(ob, s, -1, 0);q[front] = temp;vis[s][ob] = 1;while (front < rear) {node u = q[front];if (u.pos == t) {//移动到终点就退出start = front;step = u.len;break;}for (int i = 0; i < n; i++) {if ((1 << i)&u.state) {//找到障碍物位置for (int j = 0; j < G[i].size(); j++) {int next = G[i][j];if ((1 << next)&u.state)continue;//找到空闲位置node ttd = u;ttd.pos = u.pos; ttd.state = ((u.state|(1<<next))^(1<<i));if (ttd.pos == i)ttd.pos = next;//如果是机器人移动,就更新位置if (!vis[ttd.pos][ttd.state]) {vis[ttd.pos][ttd.state] = 1;ttd.len++;ttd.pre = front;q[rear++] = ttd;}}}}front++;}
}
void result(int u) {if (q[u].pre != 0)result(q[u].pre);//从前往后输出int last = q[q[u].pre].state;int cur = q[u].state;;int a, b;a = last ^ (last&cur);//因为每次只移动一步b = cur ^ (last&cur);int aa, bb;for (int i = 0; i < n; i++) {if ((1 << i)&a)aa = i;if ((1 << i)&b)bb = i;}cout << aa + 1 << " " << bb + 1 << endl;
}
int main() {int kase;cin >> kase;for (int k = 0; k < kase; k++) {for (int i = 0; i < 20; i++)G[i].clear();memset(vis, 0, sizeof(vis));step = -1;cin >> n >> obnum >> s >> t;int obstacle = 0;s--, t--;for (int i = 0; i < obnum; i++) { int l;cin >> l;l--;obstacle |= (1<<l);}for (int i = 0; i < n - 1; i++) { int temp,temp1; cin >> temp>>temp1;G[temp-1].push_back(temp1-1);G[temp1-1].push_back(temp-1);}obstacle |= (1 << s);bfs(obstacle);cout << "Case " << k + 1 << ": ";if (step == -1) {cout << "-1" << endl;}else {cout << step << endl;result(start);}}return 0;
}

UVA - 12569 Planning mobile robot on Tree (EASY Version) BFS相关推荐

  1. leetcode (Tree easy)

    leetcode Tree easy # leetcode Tree easy problem class TreeNode(object):def __init__(self,val):self.v ...

  2. 【论文笔记】Integrate Point-Cloud Segmentation with 3D LiDAR Scan-Matching for Mobile Robot Localization a

    [论文笔记]Integrate Point-Cloud Segmentation with 3D LiDAR Scan-Matching for Mobile Robot Localization a ...

  3. 一些使用ROS的移动机器人(mobile robot)或platform的官网网址

    ROS官网介绍各种位于地面且使用ROS的机器人: https://robots.ros.org/category/ground 一些使用ROS的移动机器人(mobile robot)或platform ...

  4. CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)

    题目链接:点击查看 题目大意:交互题猜密码,设原密码为 xxx,猜的密码为 yyy,如果没猜到,密码会自适应变成 zzz,满足 x⊕z=yx \oplus z=yx⊕z=y ,最多猜 nnn 次 题目 ...

  5. PTA 06-图2 Saving James Bond - Easy Version (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/672 5-10 Saving James Bond - Easy Version   ( ...

  6. Codeforces Round #579 (Div. 3) F1. Complete the Projects (easy version) 排序 + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 比较直观的想法就是对于bi≥0b_i\ge0bi​≥0的项目,我们将aia_iai​从小到大排序,让后依次加bib_ibi​,如果有取不到的,显然就无 ...

  7. CF1497E1 Square-free division (easy version)

    CF1497E1 Square-free division (easy version) 题意: 这是简单版,此题中 k=0 给出一串长为 n 的序列 a1,a2,a3...ana_1,a_2,a_3 ...

  8. 1560F1. Nearest Beautiful Number (easy version)

    F1. Nearest Beautiful Number (easy version) 预处理加二分 #include <bits/stdc++.h> using namespace st ...

  9. 1282B1. K for the Price of One (Easy Version)

    B1. K for the Price of One (Easy Version):题目 两种情况,前面取一或者前面不取 #include <bits/stdc++.h> using na ...

最新文章

  1. consul服务下线通知_Consul微服务的配置中心体验篇-阿里云开发者社区
  2. python --time()函数
  3. 哪些模块可用于python性能分析_Python调用C模块以及性能分析
  4. cocos2dx 2.2.5 hitWidget-onTouchEnded(pTouch, pEvent); 异常
  5. [转]轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender
  6. presto-docker运行
  7. [JLOI2011]不重复数字
  8. 一般服务器崩了要多久能修复,阴阳师服务器崩了修复了吗?阴阳师最新4月15日更新内容...
  9. SVN报Previous operation has not finished; run 'cleanup' if it was interrupted错误的解决方法
  10. ES6基础-字符串的新特性
  11. [INS-32025] 所选安装与指定 Oracle 主目录中已安装的软件冲突
  12. 小小c语言贪吃蛇思路,【图片】C语言小游戏~贪吃蛇【c语言吧】_百度贴吧
  13. PyTorch实战福利从入门到精通之七——卷积神经网络(LeNet)
  14. 添加多个button或其他的控件
  15. linux怎么更改rpcbind端口号,rpcbind_rpcbind服务是什么_rpcbind 端口
  16. chrome 常用快捷键(可以摆脱鼠标哦)(转载)
  17. 浏览器工作模式之标准模式/怪异模式/近似标准模式
  18. 图片存档和通信系统(PACS)的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  19. 一个完整的项目管理流程!
  20. cdr文字内容显示不出来_电脑装的字体cdr里面不显示怎么办

热门文章

  1. RabbitMQ死信队列代码架构图
  2. RabbitMQ各个名词介绍
  3. Hystrix仪表盘解释
  4. 校验用户手机号是否合法
  5. ConcurrentHashMap的源码分析-CounterCells解释
  6. 任务调度之Quartz1
  7. Ant Design入门之布局
  8. 数据库-优化-pt-kill-授权-数据
  9. 异常-主动抛出异常案例演练
  10. 迭代器模式coding