• HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash


    这个速度比分步快一点,内存占的稍微多一点

    Problem Description
    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively. There are four identical  pieces on the board. In one move it is allowed to: > move a piece to  an empty neighboring field (up, down, left or right), > jump over one  neighboring piece to an empty field (up, down, left or right).
    There are 4 moves  allowed for each piece in the configuration shown above. As an example let's  consider a piece placed in the row 4, column 4. It can be moved one row up, two  rows down, one column left or two columns right. Write a program  that: > reads two chessboard configurations from the standard  input, > verifies whether the second one is reachable from the first  one in at most 8 moves, > writes the result to the standard  output.
     
    Input
    Each of two input lines contains 8 integers a1, a2,  ..., a8 separated by single spaces and describes one configuration of pieces on  the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position  of one piece - the row number and the column number respectively. Process to the  end of file.
     
    Output
    The output should contain one word for each test case -  YES if a configuration described in the second input line is reachable from the  configuration described in the first input line in at most 8 moves, or one word  NO otherwise.
     
    Sample Input
    4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6
     
    Sample Output
    YES
      1 #include <iostream>
      2 #include <algorithm>
      3 #include <queue>
      4 #include <map>
      5 using namespace std;
      6 
      7 const int dir[4][2] = {1,0,-1,0,0,1,0,-1};
      8 
      9 struct point
     10 {
     11     int x,y;
     12 };
     13 struct node
     14 {
     15     point chess[4];
     16     
     17     bool check(int j)
     18     {
     19         if(chess[j].x>=0 && chess[j].x<=7 && chess[j].y>=0 && chess[j].y<=7)
     20         {
     21             for(int i=0;i<4;i++)
     22             {
     23                 if(i!=j && chess[i].x==chess[j].x && chess[i].y==chess[j].y)
     24                 {
     25                     return false;
     26                 }
     27             }
     28             return true;
     29         }
     30         return false;
     31     }
     32 }s,e;
     33 
     34 bool cmp(const struct point& a,const struct point& b)
     35 {
     36     if(a.x == b.x)
     37     {
     38         return a.y<b.y;
     39     }
     40     return a.x<b.x;
     41 }
     42 int gethash(node& a)
     43 {
     44     sort(a.chess, a.chess+4, cmp);
     45     int hash = 0;
     46     for(int i=0; i<4; i++)
     47     {
     48         hash |= a.chess[i].x << (6*i);
     49         hash |= a.chess[i].y << (6*i+3);
     50     }
     51     return hash;
     52 }
     53 
     54 map<int,int>mapint;
     55 map<int,int>::iterator it_1,it_2;
     56 
     57 bool BFS(void)
     58 {
     59     queue<node>que[2];
     60     
     61     que[0].push(s);
     62     que[1].push(e);
     63     mapint[gethash(s)] = 10*0+0;
     64     mapint[gethash(e)] = 10*1+0;
     65     
     66     int sign;
     67     while(!que[0].empty() || !que[1].empty())
     68     {
     69         if(que[0].size() < que[1].size())
     70         {
     71             sign = que[0].empty() ? 1:0;
     72         }
     73         else
     74         {
     75             sign = que[1].empty() ? 0:1;
     76         }
     77         
     78         node temp = que[sign].front();
     79         que[sign].pop();
     80         
     81         it_1 = mapint.find(gethash(temp));
     82         
     83         if((it_1->second)%10 >= 4)    //移动步数超过4步 
     84         {
     85             continue;
     86         }
     87         for(int i=0;i<4;i++)
     88         {
     89             for(int j=0;j<4;j++)
     90             {
     91                 node next = temp;
     92                 next.chess[i].x += dir[j][0];
     93                 next.chess[i].y += dir[j][1];
     94                 
     95                 if(!next.check(i))        //重叠或者越界 
     96                 {
     97                     next.chess[i].x += dir[j][0];
     98                     next.chess[i].y += dir[j][1];
     99                     if(!next.check(i))    //重叠或者越界 
    100                     {
    101                         continue;
    102                     }
    103                 }
    104                 
    105                 int hash = gethash(next);
    106                 it_2 = mapint.find(hash);
    107                     
    108                 if(it_2 == mapint.end())
    109                 {
    110                     mapint[hash] = it_1->second + 1;
    111                     que[sign].push(next);
    112                 }
    113                 else if(it_2->second/10 == 1-sign)
    114                 {
    115                     return true;
    116                 }
    117             }
    118         }
    119     }
    120     return false;
    121 }
    122 
    123 int main()
    124 {
    125     while(cin>>s.chess[0].x>>s.chess[0].y)
    126     {
    127         for(int i=1; i<4; i++)
    128         {
    129             cin>>s.chess[i].x>>s.chess[i].y;
    130         }
    131         for(int i=0; i<4; i++)
    132         {
    133             cin>>e.chess[i].x>>e.chess[i].y;
    134         }
    135         for(int i=0;i<4;i++)
    136         {
    137             s.chess[i].x--; s.chess[i].y--;
    138             e.chess[i].x--; e.chess[i].y--;
    139         }
    140         
    141         if(BFS())
    142         {
    143             cout<<"YES"<<endl;
    144         }
    145         else
    146         {
    147             cout<<"NO"<<endl;
    148         }
    149         mapint.clear();
    150     }
    151     return 0;
    152 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    ajax标准写法
    javascript序列化表单追加参数
    javascript获取url参数的方式
    jmeter实现跨线程组传递参数
    代码扫描工具SonarQube+Scanner的基本安装与应用
    给定一个非空正整数的数组,把数值与出现的次数存在一个新的字典中,并按照出现的次数排序
    定义一个函数,实现整数列表中偶数在左边,奇数在右边(不借助其它列表)
    python-leetcode1在一个整数数组中找出两数之和等于目标值,并把两数下标存在list中返回
    python根据key对字典进行排序
    python实现对浮点型的四舍五入
  • 原文地址:https://www.cnblogs.com/pingge/p/3195846.html
Copyright © 2020-2023  润新知