zoukankan      html  css  js  c++  java
  • 【BFS】bzoj1054 [HAOI2008]移动玩具

    暴搜吧,可以哈希一下,但是懒得写哈希了,所以慢得要死。

    Code:

     1 #include<cstdio>
     2 #include<queue>
     3 #include<set>
     4 #include<algorithm>
     5 using namespace std;
     6 const int di[]={0,0,-1,1},dj[]={-1,1,0,0};
     7 struct Squ{char S[4][4];};
     8 struct Node{Squ v;int d;Node(const Squ &a,const int &b){v=a;d=b;}Node(){}};
     9 bool operator < (const Squ &a,const Squ &b)
    10 {
    11     for(int i=0;i<4;i++)
    12       for(int j=0;j<4;j++)
    13         if(a.S[i][j]<b.S[i][j])
    14           return true;
    15         else if(a.S[i][j]>b.S[i][j])
    16           return false;
    17     return false;
    18 }
    19 bool operator == (const Squ &a,const Squ &b)
    20 {
    21     for(int i=0;i<4;i++)
    22       for(int j=0;j<4;j++)
    23         if(a.S[i][j]!=b.S[i][j])
    24           return false;
    25     return true;
    26 }
    27 Squ from,to;
    28 set<Squ>Set;
    29 queue<Node>q;
    30 inline bool check(const int &x,const int &y)
    31 {
    32     if(x>=0&&x<4&&y>=0&&y<4&&q.front().v.S[x][y]=='0')
    33       return true;
    34     return false;
    35 }
    36 inline void work(const Squ &tmp)
    37 {
    38     if(Set.find(tmp)==Set.end())
    39       {
    40           if(tmp==to)
    41             {
    42                 printf("%d
    ",q.front().d+1);
    43                 exit(0);
    44             }
    45           Set.insert(tmp);
    46           q.push(Node(tmp,q.front().d+1));
    47       }
    48 }
    49 int main()
    50 {
    51     for(int i=0;i<4;i++)scanf("%s",from.S[i]);
    52     for(int i=0;i<4;i++)scanf("%s",to.S[i]);
    53     if(from==to){printf("0
    ");return 0;}
    54     q.push(Node(from,0));
    55     Set.insert(from);
    56     while(!q.empty())
    57       {
    58           for(int i=0;i<4;i++)
    59             for(int j=0;j<4;j++)
    60               if(q.front().v.S[i][j]=='1')
    61                 {
    62                     for(int k=0;k<4;k++)
    63                       {
    64                           int ti=i+di[k],tj=j+dj[k];
    65                           if(check(ti,tj))
    66                             {
    67                                 Squ tmp=q.front().v;
    68                                 swap(tmp.S[i][j],tmp.S[ti][tj]);
    69                                 work(tmp);
    70                             }
    71                       }
    72                 }
    73           q.pop();
    74       }
    75     return 0;
    76 }
    ——The Solution By AutSky_JadeK From UESTC 转载请注明出处:http://www.cnblogs.com/autsky-jadek/
  • 相关阅读:
    实现控件的拖拽
    自定义控件——安卓旋转动画
    MD5简单实例
    TextView来实现跑马灯的效果
    Intent的简单使用
    SharedPreferences的封装
    ViewPager+fragment的使用
    安卓定时器
    2020重新出发,MySql基础,MySql视图&索引&存储过程&触发器
    2020重新出发,MySql基础,MySql表数据操作
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/3960403.html
Copyright © 2011-2022 走看看