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

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. 文件标准IO的mode

    1. r / r+ 是不创建文件,前者只读,后者可读写 2. w / w+ 是能创建文件,并且把文件置空,前者只写,后者可读写 3. a / a+ 是能创建文件,但不置空文件,在文件末尾写,前者只写, ...

  2. PHPMailer中文说明

    PHPMailer中文说明 A开头: $AltBody --属性出自:PHPMailer ::$AltBody文件:class.phpmailer .php说明:该属性的设置是在邮件正文不支持HTML ...

  3. Bernese安装及使用

    一.安装: 伯尔尼软件的安装很简单,但是在64位下,可能perl解释器安装不成功,我找了一个,并且可用,下载地址: 链接:http://pan.baidu.com/s/1hr8fgEC 密码:fj8b ...

  4. 抓取锁的sql语句-第三次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句CUR_LOCK SYS_R ...

  5. iOS-点击视图,视图背景颜色随机更改

    一.效果图 二.代码 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the v ...

  6. linux术语解析(持续更新)

    1.linux内核有个版本,分别是 longterm: 提供长期支持的内核版本 stable: 稳定版本 Beta 测试版

  7. hive 桶相关特性分析

    1. hive 桶相关概念     桶(bucket)是指将表或分区中指定列的值为key进行hash,hash到指定的桶中,这样可以支持高效采样工作.     抽样( sampling )可以在全体数 ...

  8. jQuery获取select、checkbox、radio的方法总结

    在进行网页的前后台交互的时候,经常需要获取单选框(radio).复选框(checkbox)以及下拉列表(select/dropdownlist)选中的值. 总结了一下,方便以后复习查看: 1.获取选中 ...

  9. 2.2.1 创建一个 Path

    Demo: import java.nio.file.Path; import java.nio.file.Paths; /** * @author jinxing * @系统 MAC OS X * ...

  10. C# .net 如何根据访问者IP获取所在地区

    第一步:在根目录添加新项(类),新建一个类文件,把以下文件粘贴到该类文件下:   using System; using System.Collections.Generic; using Syste ...