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. iOS 如何优雅的处理“回调地狱Callback hell”(一) (上)

    前言 最近看了一些Swift关于封装异步操作过程的文章,比如RxSwift,RAC等等,因为回调地狱我自己也写过,很有感触,于是就翻出了Promise来研究学习一下.现将自己的一些收获分享一下,有错误 ...

  2. 把C#对象转换为json字符串

    下面的代码写在ashx一般处理程序中 声明context.Response.ContentType = "application/json";代表服务器端返回的数据为json字符串 ...

  3. 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口

    //继承thread类 class PrimeThread extends Thread{ long minPrime; PrimeThread(long minPrime) { this.minPr ...

  4. datazen logo修改

    第一步:进入cp 页面 第二步: P161说明文档的P161开始,这里有说有版本的图片命名 第三步:再次强调第二步的命名,否则 如果你直接复制这个名字,不但效果出不来,还删除不掉这个包,我上次是把da ...

  5. SQL Server主键自动生成_表and存储过程

    主键表: CREATE TABLE [dbo].[KEYCODE]( [KeyName] [varchar](12) NOT NULL, [KeyTableName] [varchar](40) NU ...

  6. js 函数参数形式

    1. var a = function(b,c){ console.log(arguments);}a("1","cc"); ->  ["1&q ...

  7. C++封装常用对象和对头文件探索

    在C++实际开发中,难免会使用到一些你极为常用的算法(比如笔者经常使用的多线程技术),实现这些算法的类或是全局函数或是命名空间等等经常都要被使用多次,你会有哪些办法来使用呢?笔者有4个办法. 第一个方 ...

  8. 文件操作-php

    <?php /* 建立缓存 可以用文件长时间保存数据 文件是以liunux为模型的 在Windows下只能获取file ,dir unknow linux 下可以获取block char dir ...

  9. 初涉JavaScript模式 (11) : 模块模式

    引子 这篇算是对第9篇中内容的发散和补充,当时我只是把模块模式中的一些内容简单的归为函数篇中去,在北川的提醒下,我才发觉这是非常不严谨的,于是我把这些内容拎出来,这就是这篇的由来. 什么是模块模式 在 ...

  10. [Git]Git安装

    1.什么是Git Git是一个分布式版本控制/软件配置管理软件, git是用于Linux内核开发的版本控制工具, 与CVS.Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法 ...