UVA-12569 Planning mobile Robot on the Tree (Easy Version) (bfs+ State compression)

Source: Internet
Author: User

The main idea: an undirected graph, a robot, a number of stones, each move can only move to the connected node, and only one node can have the same and one thing (robot or stone), to find a robot from the specified position to another specified position of the minimum number of steps, the output move step.

Analysis of the problem: the position of the robot and the location of the stone to mark the status of the state, the number of States has a maximum of 15*2^15. Wide search of the.

The code is as follows:

# 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&Lt;<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&LT;&LT;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 the Tree (Easy Version) (bfs+ State compression)

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.