Problem UVA12569-Planning mobile robot on Tree (EASY Version)

Accept:138  Submit:686

Time Limit: 3000 mSec

 Problem Description

 Input

The first line contains the number of test cases T (T ≤ 340). Each test case begins with four integers n, m, s, t (4 ≤ n ≤ 15, 0 ≤ m ≤ n−2, 1 ≤ s,t ≤ n, s ̸= t), the number of vertices, the number of obstacles and the label of the source and target. Vertices are numbered 1 to n. The next line contains m different integers not equal to s, the vertices containing obstacles. Each of the next n − 1 lines contains two integers u and v (1 ≤ u < v ≤ n), that means there is an edge u−v in the tree.

 Output

For each test case, print the minimum number of moves k in the first line. Each of the next k lines containstwointegers a and b,thatmeanstomovetherobot/obstaclefrom a to b. Ifthereisnosolution, print ‘-1’. If there are multiple solutions, any will do. Print a blank line after each test case.

 Sample Input

3
4 1 1 3
2
1 2
2 3
2 4
6 2 1 4
2 3
1 2
2 3
3 4
2 5
2 6
8 3 1 5
2 3 4
1 2
2 3
3 4
4 5
1 6
1 7
2 8
 

 Sample Ouput

Case 1: 3
2 4
1 2
2 3
Case 2: 6
2 6
3 2
2 5
1 2
2 3
3 4
Case 3: 16
1 7
2 1
1 6
7 1
1 2
2 8
3 2
2 1
1 7
4 3
3 2
2 1
8 2
2 3
3 4
4 5
 
题解:看到n的范围状压是比较正的思路,二进制储存,BFS搜索,vis数组稍微有一点改动,其中一维记录石头,再有一维记录机器人。水题。
 
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = ;
  6. int n, m, s, t;
  7. int ori;
  8.  
  9. struct Edge {
  10. int to, next;
  11. }edge[maxn << ];
  12.  
  13. struct Node {
  14. int sit, robot;
  15. int time;
  16. Node(int sit = , int robot = , int time = ) :
  17. sit(sit), robot(robot), time(time) {}
  18. };
  19.  
  20. int tot, head[maxn];
  21. pair<int,int> pre[][maxn];
  22. bool vis[][maxn];
  23.  
  24. void init() {
  25. tot = ;
  26. memset(head, -, sizeof(head));
  27. memset(pre, -, sizeof(pre));
  28. memset(vis, false, sizeof(vis));
  29. }
  30.  
  31. void AddEdge(int u, int v) {
  32. edge[tot].to = v;
  33. edge[tot].next = head[u];
  34. head[u] = tot++;
  35. }
  36.  
  37. inline int get_pos(int x) {
  38. return << x;
  39. }
  40.  
  41. int bfs(pair<int,int> &res) {
  42. queue<Node> que;
  43. que.push(Node(ori, s, ));
  44. vis[ori][s] = true;
  45.  
  46. while (!que.empty()) {
  47. Node first = que.front();
  48. que.pop();
  49. if (first.robot == t) {
  50. res.first = first.sit, res.second = first.robot;
  51. return first.time;
  52. }
  53. int ssit = first.sit, rrob = first.robot;
  54. //printf("%d %d\n", ssit, rrob);
  55.  
  56. for (int i = head[rrob]; i != -; i = edge[i].next) {
  57. int v = edge[i].to;
  58. if (ssit&get_pos(v) || vis[ssit][v]) continue;
  59. vis[ssit][v] = true;
  60. que.push(Node(ssit, v, first.time + ));
  61. pre[ssit][v] = make_pair(ssit, rrob);
  62. }
  63.  
  64. for (int i = ; i < n; i++) {
  65. if (ssit&(get_pos(i))) {
  66. for (int j = head[i]; j != -; j = edge[j].next) {
  67. int v = edge[j].to;
  68. if (v == rrob || (ssit & get_pos(v))) continue;
  69. int tmp = ssit ^ get_pos(v);
  70. tmp ^= get_pos(i);
  71. if (vis[tmp][rrob]) continue;
  72. vis[tmp][rrob] = true;
  73. que.push(Node(tmp, rrob, first.time + ));
  74. pre[tmp][rrob] = make_pair(ssit, rrob);
  75. }
  76. }
  77. }
  78. }
  79. return -;
  80. }
  81.  
  82. void output(pair<int,int> a) {
  83. if (a.first == ori && a.second == s) return;
  84. output(pre[a.first][a.second]);
  85. int ppre = pre[a.first][a.second].first, now = a.first;
  86.  
  87. if (ppre^now) {
  88. int b = -, c = -;
  89. for (int i = ; i < n; i++) {
  90. if (((ppre & ( << i)) == ( << i)) && ((now & ( << i)) == )) {
  91. b = i;
  92. }
  93. else if (((ppre & ( << i)) == ) && ((now & ( << i)) == ( << i))) {
  94. c = i;
  95. }
  96. }
  97. printf("%d %d\n", b + , c + );
  98. }
  99. else {
  100. printf("%d %d\n", pre[a.first][a.second].second + , a.second + );
  101. }
  102. }
  103.  
  104. int con = ;
  105.  
  106. int main()
  107. {
  108. int iCase;
  109. scanf("%d", &iCase);
  110. while (iCase--) {
  111. init();
  112. scanf("%d%d%d%d", &n, &m, &s, &t);
  113. s--, t--;
  114. ori = ;
  115. int x;
  116. for (int i = ; i <= m; i++) {
  117. scanf("%d", &x);
  118. x--;
  119. ori ^= get_pos(x);
  120. }
  121.  
  122. int u, v;
  123. for (int i = ; i <= n - ; i++) {
  124. scanf("%d%d", &u, &v);
  125. u--, v--;
  126. AddEdge(u, v);
  127. AddEdge(v, u);
  128. }
  129.  
  130. pair<int, int> res;
  131. int ans = bfs(res);
  132. printf("Case %d: %d\n", con++, ans);
  133. if (ans != -) output(res);
  134. printf("\n");
  135. }
  136. return ;
  137. }

UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)的更多相关文章

  1. UVA-12569 Planning mobile robot on Tree (EASY Version) (BFS+状态压缩)

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

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

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

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

  6. Ping-Pong (Easy Version)(DFS)

    B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. ZOJ 3868 - Earthstone: Easy Version

    3868 - Earthstone: Easy Version Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld ...

  8. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

  9. Coffee and Coursework (Easy version)

    Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...

随机推荐

  1. Spring核心——设计模式与IoC

    “Spring”——每一个Javaer开发者都绕不开的字眼,从21世纪第一个十年国内异常活跃的SSH框架,到现在以Spring Boot作为入口粘合了各种应用.Spring现在已经完成了从web入口到 ...

  2. 基于redis的分布式锁(不适合用于生产环境)

    基于redis的分布式锁 1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分 ...

  3. ajax知识点

    什么是AJAX? AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. ...

  4. 一次断电引发的svn数据库故障

    作者:朱金灿 来源:http://blog.csdn.net/clever101 昨天办公室停电了.然后今天更新svn数据库时出现一个不能读取文件:End of file found的错误,具体如下图 ...

  5. Retrieve OpenGL Context from Qt 5.5 on OSX

    In the latest Qt 5.5, the QOpenGLWidget is much better and has less bugs than the QGLWidget, but it ...

  6. OkHttpUtils简单的网络去解析使用

    先添加依赖: implementation 'com.google.code.gson:gson:2.2.4' implementation 'com.zhy:okhttputils:2.0.0' 网 ...

  7. (后端)mybatis 模糊查询 mapper.xml的写法(转)

    原文地址:https://blog.csdn.net/sc6231565/article/details/46412765 1. sql中字符串拼接 SELECT * FROM tableName W ...

  8. Appium+java 获取元素状态

    元素的属性我们经常会用到,当定位到某个元素后,有时会需要用到这个元素的text值.className.resource-id.checked等.  一般标准的属性我们都可以通过get_attribut ...

  9. Jmeter和Loadrunner的异同

    1.jmeter的架构跟loadrunner原理一样,都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务器反馈的结果的一个过程. 2.分布式中间代 ...

  10. HTTP Authentication

    PS:近期看了一本思维导图的书,实践一下.