zoukankan      html  css  js  c++  java
  • 题解 UVA12569 【Planning mobile robot on Tree (EASY Version)】

    题目链接:Link

    Solution

    做这题时一开始我想用IDA*,后来发现需要判断是否有解,便改用状态压缩bfs。事实上,移动时石头和机器人是一样的,所以可以很容易的压缩到一个变量里,判重时可以将树的状态(压缩过)和机器人的位置分两个维度。
    代码如下:

    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct node
    {
    	int pos,st,step,pre;
    	node(){}
    	node(int _p,int _st,int _step)
    	{
    		pos=_p;
    		st=_st;
    		step=_step;
    	}
    };
    bool had_answer,G[20][20],vis[20][1<<17];
    int n,m,s,t,res,head,tail,kase;
    node st;
    node que[20<<17];
    void init()
    {
    	memset(&st,0,sizeof(st));
    	memset(G,0,sizeof(G));
    	memset(vis,0,sizeof(vis));
    	head=tail=1;
    	scanf("%d%d%d%d",&n,&m,&s,&t);
    	int x,y;
    	for(int i=0;i<m;i++)
    	{
    		scanf("%d",&x);
    		st.st|=1<<x;//压缩
    	}
    	st.pos=s;
    	st.st|=1<<s;
    	for(int i=0;i<n-1;i++)
    	{
    		scanf("%d%d",&x,&y);
    		G[x][y]=G[y][x]=1;
    	}
    }
    int stk[10000],top;
    inline int lowbit(int n)
    {
    	return n&(-n);
    }
    inline int count(int n)
    {
    	int res=0;
    	while(n)
    	{
    		res++;
    		n>>=1;
    	}
    	return res;
    }
    void print(int n)//用异或算出动了那俩个位置
    {
    	top=0;
    	while(n!=0)
    	{
    		stk[top++]=que[n].st;
    		n=que[n].pre;
    	}
    	while(top>1)
    	{//用异或算出动了那俩个位置
    		int x=stk[top-1]^stk[top-2],a=count(lowbit(x))-1,b=count(lowbit(x&~lowbit(x)))-1;//分离出两个位置
    		if(stk[top-1]&1<<a) printf("%d %d
    ",a,b);
    		else printf("%d %d
    ",b,a);
    		top--;
    	}
    }
    void solve()
    {
    	if(s==t)//特判
    	{
    		printf("Case %d: 0
    
    ",++kase);
    		return;
    	}
    	que[tail++]=st;
    	vis[st.pos][st.st]=1;
    	while(tail-head>0)
    	{
    		int st=que[head].st;
    		for(int i=1;i<=n;i++)
    			for(int j=1;j<=n;j++)
    				if(i!=j&&G[i][j]&&((st&1<<i)>>i)&&!((st&1<<j)>>j))
    				{//从i移动到j所要满足的规则
    					que[tail].pos=(i==que[head].pos?j:que[head].pos);//判断一下机器人动了没有。
    					que[tail].pre=head;
    					que[tail].step=que[head].step+1;
    					que[tail].st=(st&(~(1<<i)))|1<<j;//用0覆盖旧位,再添上新位。
    					if(vis[que[tail].pos][que[tail].st]) continue;//判重
    					vis[que[tail].pos][que[tail].st]=1;
    					if(que[tail].pos==t)
    					{
    						printf("Case %d: %d
    ",++kase,que[tail].step);
    						print(tail);
    						printf("
    ");
    						return;
    					}
    					tail++;
    				}
    		head++;
    	}
    	printf("Case %d: -1
    
    ",++kase);
    }
    int main()
    {
    #ifdef local
    	freopen("pro.in","r",stdin);
    #endif
    	int t,kase=0;
    	scanf("%d",&t);
    	while(t-->0)
    	{
    		init();
    		solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    [tarjan][树上差分] Codeforces 555E Case of Computer Network
    [线段树] Jzoj P1214 项链工厂
    [矩阵乘法] Jzoj P2288 沼泽鳄鱼
    [状压dp][dfs] Jzoj P2679 跨时代
    [spfa][差分约束] Bzoj 2330 糖果
    [spfa] Bzoj 2118 墨墨的等式
    [倍增][Floyd] Bzoj 2165 大楼
    [虚树][树形dp] Bzoj P3611 大工程
    [虚树][树形dp] Bzoj P2286 消耗战
    [数位dp] Jzoj P3316 非回文数字
  • 原文地址:https://www.cnblogs.com/happyZYM/p/11380039.html
Copyright © 2011-2022 走看看