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数组稍微有一点改动,其中一维记录石头,再有一维记录机器人。水题。
 
 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = ;
int n, m, s, t;
int ori; struct Edge {
int to, next;
}edge[maxn << ]; struct Node {
int sit, robot;
int time;
Node(int sit = , int robot = , int time = ) :
sit(sit), robot(robot), time(time) {}
}; int tot, head[maxn];
pair<int,int> pre[][maxn];
bool vis[][maxn]; void init() {
tot = ;
memset(head, -, sizeof(head));
memset(pre, -, sizeof(pre));
memset(vis, false, sizeof(vis));
} void AddEdge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} inline int get_pos(int x) {
return << x;
} int bfs(pair<int,int> &res) {
queue<Node> que;
que.push(Node(ori, s, ));
vis[ori][s] = true; while (!que.empty()) {
Node first = que.front();
que.pop();
if (first.robot == t) {
res.first = first.sit, res.second = first.robot;
return first.time;
}
int ssit = first.sit, rrob = first.robot;
//printf("%d %d\n", ssit, rrob); for (int i = head[rrob]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (ssit&get_pos(v) || vis[ssit][v]) continue;
vis[ssit][v] = true;
que.push(Node(ssit, v, first.time + ));
pre[ssit][v] = make_pair(ssit, rrob);
} for (int i = ; i < n; i++) {
if (ssit&(get_pos(i))) {
for (int j = head[i]; j != -; j = edge[j].next) {
int v = edge[j].to;
if (v == rrob || (ssit & get_pos(v))) continue;
int tmp = ssit ^ get_pos(v);
tmp ^= get_pos(i);
if (vis[tmp][rrob]) continue;
vis[tmp][rrob] = true;
que.push(Node(tmp, rrob, first.time + ));
pre[tmp][rrob] = make_pair(ssit, rrob);
}
}
}
}
return -;
} void output(pair<int,int> a) {
if (a.first == ori && a.second == s) return;
output(pre[a.first][a.second]);
int ppre = pre[a.first][a.second].first, now = a.first; if (ppre^now) {
int b = -, c = -;
for (int i = ; i < n; i++) {
if (((ppre & ( << i)) == ( << i)) && ((now & ( << i)) == )) {
b = i;
}
else if (((ppre & ( << i)) == ) && ((now & ( << i)) == ( << i))) {
c = i;
}
}
printf("%d %d\n", b + , c + );
}
else {
printf("%d %d\n", pre[a.first][a.second].second + , a.second + );
}
} int con = ; int main()
{
int iCase;
scanf("%d", &iCase);
while (iCase--) {
init();
scanf("%d%d%d%d", &n, &m, &s, &t);
s--, t--;
ori = ;
int x;
for (int i = ; i <= m; i++) {
scanf("%d", &x);
x--;
ori ^= get_pos(x);
} int u, v;
for (int i = ; i <= n - ; i++) {
scanf("%d%d", &u, &v);
u--, v--;
AddEdge(u, v);
AddEdge(v, u);
} pair<int, int> res;
int ans = bfs(res);
printf("Case %d: %d\n", con++, ans);
if (ans != -) output(res);
printf("\n");
}
return ;
}

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. python基础学习(二)注释和算术运算符

    注释 1. 注释的作用 注释就是对某些代码进行标注说明,以增强代码的可读性.我们在写程序的时候,编写的某一部分代码的意图不太明显,这时候就需要对这一部分代码加以说明,来明确这一部分到的意图.一般的编程 ...

  2. python基础学习(一) 第一个python程序

    1. 使用python/python3解释器的方式 按照惯例,我们都是以Hello world作为一门程序语言的开始,进行如下的操作: 在桌面上新建一个hello-python文件夹 进入hello- ...

  3. webpack4 系列教程(十三):自动生成HTML文件

    作者按:因为教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十三):自动生成 HTML 文件>原文地址.更欢迎来我的小站看更多原创内容:go ...

  4. 什么是Docker Volume?

    摘要:Docker Volume,通常翻译为数据卷,用于保存持久化数据.当我们将数据库例如MySQL运行在Docker容器中时,一般将数据通过Docker Volume保存在主机上,这样即使删除MyS ...

  5. 前端入门9-JavaScript语法之运算符

    声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...

  6. 年会抽奖 抽奖系统 抽奖软件 C# Winform

    年会抽奖软件: Q.Q 358189777 C#.  数据库Access: 1.系统启动,自动全屏展示. 2.背景随心切换. 3.快捷键方便自如: F1:弹出设置界面 F2:查询人员名单.中奖名单 F ...

  7. JS之表单提交时编码类型enctype详解

    简介 form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x- ...

  8. PlugNT CMS v4.6.3 调用文章上一页和下一页及点击数加1

    using System; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebContr ...

  9. Dynamics 365 Online-Virtual Entities

    转载来源https://blogs.technet.microsoft.com/lystavlen/2017/09/08/virtual-entities/,使用当前Dynamics 365环境,亲测 ...

  10. wap2app(二)-- 设置APP系统状态栏

    准备 工具:HBuilder 一.全屏设置,不显示系统状态栏 这里所说的系统状态栏就是包括了:信号.运营商.电量等信息手机屏幕最顶部信息. 全屏并非状态栏透明或变色,而是没有状态栏,也就是看不见电量. ...