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更新UI的几种方式
之前做过一个Android采集心电图数据的程序,那才是真正的多线程,之前写的小程序:比如下载个文件,从socket接受大一点的数据流然后在ui上更新进度,我都感觉这就叫做多线程了,其实这啥都不算,用个 ...
- Android(java)学习笔记214:开源框架的文件上传(只能使用Post)
1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...
- android SDK开发 -- TitleBar封装(一)
假设app的title 统一的都是这种左中右结构的 代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- visual studio中的一些实用的快捷键
重置开发环境:使得开发工具恢复默认状态 方法:工具->导入和导出设置导向->重置所有设置->不保存 显示行号: 方法:工具->选项->所有语言->行号 在编程过程中 ...
- (转)PHP中extract()函数的妙用
近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...
- linux下系统定时任务配置----crontab(mysql定时备份)
crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任 ...
- .net中XML的创建01(传统方法)
XML传统的创建: 传统的创建主要是依据XmlDocument的对象展开的,通过XmlDocument对象可以创建元素(XmlElement).属性(XmlAttribute)以及文本节点(Creat ...
- Eclipse怎么忽略掉报错的js文件
第一步,我们要先定位错误在哪里,选择菜单里window——show view——other,选择Problems. 第二步,点击有红叉的项目,在Problems视图中,可以看到是什么错,哪个文件夹中的 ...
- Javascript模块化编程:模块的写法
声明:本文转载自:阮一峰的网络日志,原文地址http://www.ruanyifeng.com/blog/2012/10/javascript_module.html,http://www.ruany ...
- 谈谈PHP、Python与Ruby
假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成为一个高效工程师,推荐 Python. 假如你想让他爱上他的工作,推荐 Ruby. 语言的选择 编程语言非常重要,不要认为他们都图灵等价, ...