HDU_1401——分步双向BFS,八进制乘权值压缩,map存放hash
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.
2 4 3 3 3 6 4 6
#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的更多相关文章
- HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash
Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...
- Number Clicker CodeForces - 995E(双向bfs)
双向bfs 注意数很大 用map来存 然后各种难受....
- 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 ...
- UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)
Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后 ...
- 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】
题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...
- 带权值的图 BFS
用bfs遍历最图求最短路径时通常借用优先队列即优先考虑最大的或者最小的权值 方法1 优先队列:(内置函数,优先考虑较小的权值) #include<iostream> #include< ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
随机推荐
- C#泛型类的简单创建与使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- centos 安装 lnmp
直接yum install nginx不行,要先处理下源,下面是安装完整流程 1. rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/ng ...
- Android四大图片缓存(Imageloader,Picasso,Glide,Fresco)原理、特性对比
四大图片缓存基本信息 Universal ImageLoader 是很早开源的图片缓存,在早期被很多应用使用. Picasso 是 Square 开源的项目,且他的主导者是 JakeWharton,所 ...
- visual studio中的一些实用的快捷键
重置开发环境:使得开发工具恢复默认状态 方法:工具->导入和导出设置导向->重置所有设置->不保存 显示行号: 方法:工具->选项->所有语言->行号 在编程过程中 ...
- [转帖]了解AmbiLight知识
了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...
- javascript基础学习(十五)
javascript之cookie 学习要点: cookie介绍 创建与获取cookie cookie的编码 cookie的生存期 cookie的路径 cookie的domain cookie的sec ...
- IIS7、IIS6 web.config注册HttpModule
IIS6注册HttpModule <system.web> <httpModules> <add name="..." type= ...
- gui线程
package thread; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.Acti ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- js 判断url的?后参数是否包含某个字符串
function GetQueryString(name){ var reg=eval("/"+name+"/g"); var r = window. ...