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[][] = {,,-,,,,,-};
const int pow[] = {,,,,,,,}; 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 * pow[*i];
hash += a.chess[i].y * pow[*i+];
}
return hash;
} map<int,int>mapint;
map<int,int>::iterator it_1,it_2; bool BFS(int flag,node temp)
{
queue<node>que;
que.push(temp);
while(!que.empty())
{
temp = que.front();
que.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_next = gethash(next);
it_2 = mapint.find(hash_next); if(it_2 == mapint.end())
{
mapint[hash_next] = it_1->second + ;
que.push(next);
}
else if(it_2->second/ == -flag)
{
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--;
}
mapint[gethash(s)] = *+;
mapint[gethash(e)] = *+; if(BFS(,s) || BFS(,e))
{
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 an ...

  3. Number Clicker CodeForces - 995E(双向bfs)

    双向bfs  注意数很大  用map来存 然后各种难受....

  4. CodeForces - 995E Number Clicker (双向BFS)

    题意:给出u,v,p,对u可以进行三种变化: 1.u=(u+1)%p ; 2.u = (u+p-1)%p;  3.u = 模p下的逆元.问通过几步可以使u变成v,并且给出每一步的操作. 分析:朴素的b ...

  5. UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

    Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...

  6. 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】

    题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...

  7. 带权值的图 BFS

    用bfs遍历最图求最短路径时通常借用优先队列即优先考虑最大的或者最小的权值 方法1 优先队列:(内置函数,优先考虑较小的权值) #include<iostream> #include< ...

  8. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  9. NOIp 2014 #2 联合权值 Label:图论 !!!未AC

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

随机推荐

  1. Android图片异步加载之Android-Universal-Image-Loader(转)

    今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...

  2. Linux磁盘管理:lvcreate 常用命令

    查看当前LV及PV信息: [root@rusky ~]# hostnamectl Static hostname: localhost.localdomain Transient hostname: ...

  3. NYOJ 1107 最高的奖励(贪心+优先队列)

    最高的奖励 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 请问:挖掘机技术哪家强?AC了告诉你! 给你N(N<=3*10^4)个任务,每个任务有一个截止完成时 ...

  4. PHP微信公众号 access_token缓存

    PHP创建access_token.json文件,将access_token 和 生成时间expires 保存在其中, {"access_token":"xxxx&quo ...

  5. cancel-ng-swipe-right-on-child

    <!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...

  6. photoshop mac版下载及破解

    1.下载 直接百度photoshop,就可以找到百度的下载源: 2.破解 http://zhidao.baidu.com/question/581955095.html

  7. Python局部变量和全局变量global

    当你在函数定义声明变量的时候,它们与函数外具有相同名称的其它变量没有任何关系,即变量名称对于函数来说是 局部  的.这称为变量的 作用域 .所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开 ...

  8. JavaScript 之 关键内容

    词法作用域.调用对象.作用域链.闭包.构造函数.原型.类.继承 局部变量查找路径 属性查找路径

  9. Hibernate中session的产生的方式

     *  session的产生的方式      * 1. sessionFactory.openSession         每次都会新创建一个session,只要新创建一个session,hiber ...

  10. javascript 常用array类型方法

    concat:基于当前数组中的所有项创建一个新数据,会创建当前数组一个副本,然后将接受到的参数放到数组末尾,最后返回新数组.如果没有参数,则复制当前数组并返回副本. slice:基于当前数组中一个或多 ...