HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
这个速度比分步快一点,内存占的稍微多一点
#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的更多相关文章
- 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 and columns of the ches ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...
- poj2965(位运算压缩+bfs+记忆路径)
题意:有个4*4的开关,里面有着16个小开关 -+-- ---- ---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开.现在有一种操作,只要改变某个开关, ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- 【BFS】【位运算】解药还是毒药
[codevs2594]解药还是毒药 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别 ...
- HDU5627--Clarke and MST (bfs+位运算)
http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...
- POJ 1753 bfs+位运算
T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...
随机推荐
- Android常用功能代码块(转)
1.设置activity无标题,全屏 // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow(). ...
- 洛谷 1503 鬼子进村 (set)
/*set加速维护*/ #include<iostream> #include<cstdio> #include<cstring> #include<set& ...
- 解析JavaScript中apply和call以及bind
函数调用方法 在谈论JavaScript中apply.call和bind这三兄弟之前,我想先说下,函数的调用方式有哪些: 作为函数 作为方法 作为构造函数 通过它们的call()和apply()方法间 ...
- HTML 学习笔记
1HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 并且只有这6种标题,标题中加多个空格,和一个空格没区别,标题文字前后加默认空格会被去除. ...
- 使用EditText搜索listview里面的内容,实现Listview跟随变动的情况
1.布局的XML文件里面添加EditText控件(省略)控件id=mSearch ListView的id=admin_lv; 2.一.获取ListView展示的数据(通过适配器获取) 二.这个是我要说 ...
- oracle批量转库工作,比较快捷的方式
select 'create table ' || t.TABLE_NAME || ' as select * from ODS_ZMGLXT.'|| t.TABLE_NAME ||'; ' fr ...
- oracle特殊字符的ascii值
- 重新开始学习javase_IO
一,认识IO 通过数据流.序列化和文件系统提供系统输入和输出. 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数据源可以是文件,内存,或是网络连接.类似的,当程序需要 ...
- 使用wireshark抓本机之间的包(转)
所转地址:http://www.chinadmd.com/file/oc6evrwtzieitexvoupppisr_1.html 在进行通信开发的过程中,我们往往会把本机既作为客户端又作为服务器端来 ...
- SxsTrace工具使用方法(转)
http://blog.sina.com.cn/s/blog_494e45fe0102dtt3.html Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库 ...