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

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
 #include <iostream>
#include <algorithm>
#include <queue>
#include <map>
using namespace std; const int dir[][] = {,,-,,,,,-}; struct point
{
int x,y;
};
struct node
{
point chess[]; bool check(int j)
{
if(chess[j].x>= && chess[j].x<= && chess[j].y>= && chess[j].y<=)
{
for(int i=;i<;i++)
{
if(i!=j && chess[i].x==chess[j].x && chess[i].y==chess[j].y)
{
return false;
}
}
return true;
}
return false;
}
}s,e; bool cmp(const struct point& a,const struct point& b)
{
if(a.x == b.x)
{
return a.y<b.y;
}
return a.x<b.x;
}
int gethash(node& a)
{
sort(a.chess, a.chess+, cmp);
int hash = ;
for(int i=; i<; i++)
{
hash |= a.chess[i].x << (*i);
hash |= a.chess[i].y << (*i+);
}
return hash;
} map<int,int>mapint;
map<int,int>::iterator it_1,it_2; bool BFS(void)
{
queue<node>que[]; que[].push(s);
que[].push(e);
mapint[gethash(s)] = *+;
mapint[gethash(e)] = *+; int sign;
while(!que[].empty() || !que[].empty())
{
if(que[].size() < que[].size())
{
sign = que[].empty() ? :;
}
else
{
sign = que[].empty() ? :;
} node temp = que[sign].front();
que[sign].pop(); it_1 = mapint.find(gethash(temp)); if((it_1->second)% >= ) //移动步数超过4步
{
continue;
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
node next = temp;
next.chess[i].x += dir[j][];
next.chess[i].y += dir[j][]; if(!next.check(i)) //重叠或者越界
{
next.chess[i].x += dir[j][];
next.chess[i].y += dir[j][];
if(!next.check(i)) //重叠或者越界
{
continue;
}
} int hash = gethash(next);
it_2 = mapint.find(hash); if(it_2 == mapint.end())
{
mapint[hash] = it_1->second + ;
que[sign].push(next);
}
else if(it_2->second/ == -sign)
{
return true;
}
}
}
}
return false;
} int main()
{
while(cin>>s.chess[].x>>s.chess[].y)
{
for(int i=; i<; i++)
{
cin>>s.chess[i].x>>s.chess[i].y;
}
for(int i=; i<; i++)
{
cin>>e.chess[i].x>>e.chess[i].y;
}
for(int i=;i<;i++)
{
s.chess[i].x--; s.chess[i].y--;
e.chess[i].x--; e.chess[i].y--;
} if(BFS())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
mapint.clear();
}
return ;
}

HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash的更多相关文章

  1. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  2. HDU_1401——分步双向BFS,八进制乘权值压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  3. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  4. uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)

    这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...

  5. poj2965(位运算压缩+bfs+记忆路径)

    题意:有个4*4的开关,里面有着16个小开关 -+-- ---- ---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开.现在有一种操作,只要改变某个开关, ...

  6. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

  7. 【BFS】【位运算】解药还是毒药

    [codevs2594]解药还是毒药 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别 ...

  8. HDU5627--Clarke and MST (bfs+位运算)

    http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...

  9. POJ 1753 bfs+位运算

    T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...

随机推荐

  1. Dhroid框架笔记(DhNet、Adapter)

    3.1.1 DhNet用于获取网络中的数据 DhNet net=new DhNet("路劲"); net.addParam("key", "参数&qu ...

  2. editplus双击单词语法高亮显示设置

    view=>Word Highlighting

  3. ecshop首页调用指定商品分类下的商品品牌列表

    转之--http://www.16css.com/ecshop/735.html 通过二次开发可以实现ECSHOP首页调用指定分类下的品牌列表. 第一步: 打开根目录下的index.php 在最后面 ...

  4. c#隐式转换之有符号位转换

    有符号位类型的转换,额外的高位用源表达式的符号位填充.这样就维持了被转换的值的正确符号和大小. 特别注意的是负数的转换,额外的高位用1填充,因为负数的二进制表示是对应正数的二进制取反加1,所以高位用1 ...

  5. dom4j 笔记【转】

    SAXReader reader = new SAXReader(); Document doc = reader.read(...); List childNodes = doc.selectNod ...

  6. Objective-C基础知识点总结

    一.#import 和 #include 的区别,@class代表什么?@class 和 #import 的区别?#import<> 和 #import""的区别 答: ...

  7. NPM环境搭建

    1. NPM全局路径:配置npm包的安装位置,在你的用户文件夹下,新建.npmrc文件:cache=D:\NodeJs\nvm\npm-cache 表示缓存文件夹 prefix=D:\NodeJs\n ...

  8. Linux CentOS6.4下Mysql数据库的安装与配置

    一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...

  9. JavaScript-学习一获取表单的值

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>he ...

  10. uva 725 Division(除法)暴力法!

    uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...