UVA--12569 (State compression + bfs), ultraviolet-12366bfs

Source: Internet
Author: User

UVA--12569 (State compression + bfs), ultraviolet-12366bfs

Problem PPlanning [m] obile robot on Tree (EASY Version)

We are given a connected, undirected graph G on n vertices. there is a mobile robot on one of the vertices; this vertex is labeled s. each of several other vertices contains a single movable obstacle. the robot and the obstacles may only reside at vertices, although they may be moved into SS edges. no vertex may ever contain more than one movable entity (robot or obstacles ).

In one step, we may move either the robot or one of the obstacles from its current position v to a vacant vertex adjacent to v. our goal is to move the robot to a designated vertex t using the smallest number of steps possible.

Let us call this graph motion planning with one robot, or GMP1R for short. In this problem, we restrict the graph G to be a tree, namely TMP1R.

Here are some examples (gray circles represent obstacles ).

Example 1 (s = 1, t = 3 ):

Move the obstacle 2-4, and then move the robot 1-2-3. Total: 3 moves.

Example 2 (s = 1, t = 4 ):

Move obstacle 2-5, then 3-2-6, and then move the robot 1-2-3-4. Total: 6 moves.

Example 3 (s = 1, t = 5 ):

Move the robot 1-6, then obstacle 2-1-7, then robot 6-1-2-8, then obstacle 3-2-1-6, then 4-3-2-1, and finally robot 8-2-3-4-5. total: 16 moves.

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 contains two integers a and B, that means to move the robot/obstacle from a to B. if there is no solution, print-1. if there are multiple solutions, any will do. print a blank line after each test case.

Sample Input
34 1 1 321 22 32 46 2 1 42 31 22 33 42 52 68 3 1 52 3 41 22 33 44 51 61 72 8
Output for the Sample Input
Case 1: 32 41 22 3Case 2: 62 53 22 61 22 33 4Case 3: 161 62 11 76 11 22 83 22 11 64 33 22 18 22 33 4
4 5
 
* ** It took so long because N was accidentally reduced by 1 during initialization, and the code writing was not simplified. In fact, the robot and common obstacles do not need to be processed separately, just make a slight judgment. Question: A tree is provided with a robot and several obstacles on it. Each time an obstacle or robot can be moved to an adjacent node. You need to perform some operations to get the robot from s to t, ask the minimum number of steps, and output the solution. Vis first represents the location of the robot, and the last dimension uses state compression to represent the location of each obstacle. The change is dying.
# Include <cstdio> # include <cstdlib> # include <cstring> # include <iostream> # include <algorithm> # include <string> # include <cmath> # include <queue> # include <vector> # include <map> # include <set> # define INF 0x3f3f3f # define mem (, b) memset (a, B, sizeof (a) using namespace std; const int maxd = 200000 + 5; const int maxn = 10000 + 5; # define lson l, m, rt <1 # define rson m + 1, r, rt <1 | 1 typedef long ll; typedef pair <int, int> pii ;// --------------------------- Int vis [20] [1 <16]; int n, m, s, t, tmp; vector <int> g [20]; typedef struct info {int pos, state, step ;}; typedef struct info2 {int pos, state, step; int x, y ;}; info2 pre [20] [1 <16]; void init () {scanf ("% d", & n, & m, & s, & t); tmp = 0; mem (vis, 0 ); for (int I = 0; I <n; ++ I) g [I]. clear (); for (int I = 0; I <m; ++ I) {int x; scanf ("% d", & x ); tmp | = (1 <(x-1);} -- s, -- t; tmp | = (1 <s); vis [s] [tmp] = 1; (Int I = 0; I <n-1; ++ I) {int u, v; scanf ("% d", & u, & v ); g [U-1]. push_back (v-1); g [V-1]. push_back (U-1) ;}} void print (int pos, int sta) {vector <info2> v; info2 tmp = pre [pos] [sta]; while (tmp. step) {v. push_back (tmp); tmp = pre [tmp. pos] [tmp. state];} v. push_back (tmp); for (int I = v. size ()-1; I> = 0; -- I) {printf ("% d \ n", v [I]. x + 1, v [I]. y + 1);} printf ("\ n");} void bfs () {queue <info> q; q. push ({s, tmp, 0}); while (! Q. empty () {info now = q. front (); q. pop (); int tpos = now. pos; int tstate = now. state; int tstep = now. step; if (tpos = t) {printf ("% d \ n", tstep); print (tpos, tstate); return ;}for (int I = 0; I <n; ++ I) if (1 <I) & tstate) = (1 <I )) /// an object can be moved here for (int j = 0; j <g [I]. size (); ++ j) {if (1 <g [I] [j]) & tstate) = (1 <g [I] [j]) continue; // int temp = (tstate &(~ (1 <I) | (1 <g [I] [j]); if (I = tpos) /// if the robot is moved {if (vis [g [I] [j] [temp]) continue; // if q exists. push ({g [I] [j], temp, tstep + 1}); vis [g [I] [j] [temp] = 1; pre [g [I] [j] [temp] = {tpos, tstate, tstep, I, g [I] [j]};} else {if (vis [tpos] [temp]) continue; // q exists in this status. push ({tpos, temp, tstep + 1}); vis [tpos] [temp] = 1; pre [tpos] [temp] = {tpos, tstate, tstep, I, g [I] [j] };}} printf ("-1 \ n"); return ;}int main () {int kase; freopen ("1.txt ", "r", stdin); scanf ("% d", & kase); for (int I = 1; I <= kase; ++ I) {printf ("Case % d:", I); init (); bfs ();} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.