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

    双向bfs。

    尼玛,比单向都慢。。。抑郁。。

    View Code
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstdio>
     6 
     7 #define N 77777
     8 #define INF 0x3f3f3f3f
     9 
    10 using namespace std;
    11 
    12 int n,st,ed;
    13 int map[5][5];
    14 int q[2][N];
    15 int sp[2][N];
    16 int dx[4]={-1,0,1,0};
    17 int dy[4]={0,1,0,-1};
    18 
    19 inline int pack(int a[5][5])
    20 {
    21     int rt=0;
    22     for(int i=0;i<4;i++)
    23         for(int j=0;j<4;j++)
    24             if(a[i][j]) rt|=1<<(i*4+j);
    25     return rt;
    26 }
    27 
    28 inline void unpack(int a[5][5],int zt)
    29 {
    30     for(int i=0;i<4;i++)
    31         for(int j=0;j<4;j++)
    32         {
    33             if(zt&(1<<(i*4+j))) a[i][j]=true;
    34             else a[i][j]=0;
    35         }
    36 }
    37 
    38 inline void read()
    39 {
    40     memset(sp,0x3f,sizeof sp);
    41     char str[10];
    42     for(int i=0;i<4;i++)
    43     {
    44         scanf("%s",str);
    45         for(int j=0;j<4;j++)
    46             map[i][j]=str[j]-'0';
    47     }
    48     st=pack(map);
    49     for(int i=0;i<4;i++)
    50     {
    51         scanf("%s",str);
    52         for(int j=0;j<4;j++)
    53             map[i][j]=str[j]-'0',ed|=map[i][j]<<(i*4+j);
    54     }
    55     ed=pack(map);
    56 }
    57 
    58 inline void go()
    59 {
    60     if(st==ed) {puts("0");return;}
    61     int h[2]={1,1},t[2]={1,1};
    62     q[0][t[0]++]=st; q[1][t[1]++]=ed;
    63     sp[0][st]=0; sp[1][ed]=0;
    64     int fg=0;
    65     while(1)
    66     {
    67         int step=sp[fg][q[fg][h[fg]]],sta,nx,ny,zt;
    68         while(h[fg]<t[fg]&&sp[fg][q[fg][h[fg]]]==step)
    69         {
    70             sta=q[fg][h[fg]++];
    71             unpack(map,sta);
    72             for(int i=0;i<4;i++)
    73                 for(int j=0;j<4;j++)
    74                 {
    75                     if(!map[i][j]) continue;
    76                     for(int k=0;k<4;k++)
    77                     {
    78                         nx=i+dx[k]; ny=j+dy[k];
    79                         if(nx<0||ny<0||nx>=4||ny>=4||map[nx][ny]) continue;
    80                         swap(map[i][j],map[nx][ny]);
    81                         zt=pack(map);
    82                         swap(map[i][j],map[nx][ny]);
    83                         if(sp[fg][zt]<=sp[fg][sta]+1) continue;
    84                         if(sp[!fg][zt]!=INF) {printf("%d\n",sp[fg][sta]+sp[!fg][zt]+1);return;}
    85                         q[fg][t[fg]++]=zt; sp[fg][zt]=sp[fg][sta]+1;
    86                     }
    87                 }
    88         }
    89         fg=!fg;
    90     }
    91 }
    92 
    93 int main()
    94 {
    95     read(),go();
    96     return 0;
    97 }
    没有人能阻止我前进的步伐,除了我自己!
  • 相关阅读:
    poj 3436 ACM Computer Factory 夜
    poj 1182 食物链 夜
    poj 2299 UltraQuickSort 夜
    E. Printer 夜
    poj 3083 Children of the Candy Corn 夜
    sdut 2500 0\'s 夜
    1776. Anniversary Firework sdut 2507 焰火表演 夜
    删除上传文件中可能包含的空行
    ALV的fieldcat属性
    ALV显示红绿灯(FM&nbsp;ALV&nbsp;和&nbsp;OO&nbsp;ALV两…
  • 原文地址:https://www.cnblogs.com/proverbs/p/2956836.html
Copyright © 2011-2022 走看看