zoukankan      html  css  js  c++  java
  • [HAOI2008] 移动玩具

    搜索水题。

    洛谷 P4289 传送门

    bzoj 1054 传送门

    bfs就行了,显然第一次搜到的就是最优的。

    手写队列上瘾......

     1 #include<cstdio>
     2 
     3 unsigned int st,ed;
     4 bool vis[150000];
     5 
     6 struct data
     7 {
     8     unsigned int s,c;
     9 };
    10 
    11 struct queue
    12 {
    13     data buf[100000];
    14     unsigned int hd,tl;
    15     void pre()
    16     {
    17         hd=1;
    18     }
    19     void push(data x)
    20     {
    21         if(!vis[x.s])buf[++tl]=x,vis[x.s]=1;
    22     }
    23     data front()
    24     {
    25         return buf[hd++];
    26     }
    27     bool empty()
    28     {
    29         return tl+1==hd;
    30     }
    31 }qq;
    32 
    33 unsigned int bfs()
    34 {
    35     qq.pre();
    36     qq.push((data){st,0});
    37     while(!qq.empty())
    38     {
    39         data nw=qq.front();
    40         unsigned int s=nw.s;
    41         if(s==ed)return nw.c;
    42         nw.c++;
    43         for(unsigned int i=0;i<16;i++)
    44         {
    45             unsigned int p=1<<i;
    46             unsigned int t=s^p;
    47             if(!(s&p))continue;
    48             if((i>>2)&&(!(s&(1<<i-4))))
    49                 qq.push((data){t^(1<<i-4),nw.c});
    50             if((i<12)&&(!(s&(1<<i+4))))
    51                 qq.push((data){t^(1<<i+4),nw.c});
    52             if((i&3)&&(!(s&(1<<i-1))))
    53                 qq.push((data){t^(1<<i-1),nw.c});
    54             if(((i&3)^3)&&(!(s&(1<<i+1))))
    55                 qq.push((data){t^(1<<i+1),nw.c});
    56         }
    57     }
    58 }
    59 
    60 char in[5][5];
    61 char out[5][5];
    62 
    63 int main()
    64 {
    65     for(int i=0;i<4;i++)scanf("%s",in[i]);
    66     for(int i=0;i<4;i++)scanf("%s",out[i]);
    67     for(int i=0;i<4;i++)
    68         for(int j=0;j<4;j++)
    69             st|=((in[i][j]-'0')<<(i*4+j));
    70     for(int i=0;i<4;i++)
    71         for(int j=0;j<4;j++)
    72             ed|=((out[i][j]-'0')<<(i*4+j));
    73     unsigned int ans=bfs();
    74     printf("%u",ans);
    75     return 0;
    76 }
  • 相关阅读:
    Java编译期和运行期
    深入理解重载和重写及与之相关的多态性 Overloading and Overriding(转)
    Java编译期优化与运行期优化技术浅析
    JAVA反射
    JSP笔记(二)
    JSP笔记(一)
    字符串之String类
    JAVA的Random类介绍
    (转)详细分析css float 属性
    协议与委托
  • 原文地址:https://www.cnblogs.com/cervusy/p/10009755.html
Copyright © 2011-2022 走看看