HDU1401(双向BFS)
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401
给你8*8的棋盘和4个棋子初始位置、最终位置,问你能否在8次操作后达到该状态。
思路:
双向BFS,起点开始正搜4步,终点倒搜4步,map标记。
- #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
- #include <cstdio>//sprintf islower isupper
- #include <cstdlib>//malloc exit strcat itoa system("cls")
- #include <iostream>//pair
- #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
- #include <bitset>
- //#include <map>
- #include<unordered_map>
- #include <vector>
- #include <stack>
- #include <set>
- #include <string.h>//strstr substr strcat
- #include <string>
- #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
- #include <cmath>
- #include <deque>
- #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
- #include <vector>//emplace_back
- //#include <math.h>
- #include <cassert>
- #include <iomanip>
- //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
- #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
- using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
- //******************
- clock_t __START,__END;
- double __TOTALTIME;
- void _MS(){__START=clock();}
- void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
- //***********************
- #define rint register int
- #define fo(a,b,c) for(rint a=b;a<=c;++a)
- #define fr(a,b,c) for(rint a=b;a>=c;--a)
- #define mem(a,b) memset(a,b,sizeof(a))
- #define pr printf
- #define sc scanf
- #define ls rt<<1
- #define rs rt<<1|1
- typedef pair<int,int> PII;
- typedef vector<int> VI;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef double db;
- const double E=2.718281828;
- const double PI=acos(-1.0);
- const ll INF=(1LL<<);
- const int inf=(<<);
- const double ESP=1e-;
- const int mod=(int)1e9+;
- const int N=(int)1e6+;
- int mp[][];
- unordered_map<ull,bool>mark;
- struct node
- {
- ull id;
- int step;
- };
- ull get()
- {
- ull now=,temp=;
- int cnt=-;
- for(int i=;i<=;++i)
- {
- for(int j=;j<=;++j)
- {
- ++cnt;
- if(mp[i][j])
- now|=temp<<cnt;
- }
- }
- return now;
- }
- bool ok(int x,int y)
- {
- return x>=&&x<=&&y>=&&y<=;
- }
- bool ans;
- void bfs(ull start,bool f)
- {
- queue<node>q;
- q.push({start,});
- while(!q.empty())
- {
- node now=q.front();q.pop();
- if(!f)
- {
- if(mark[now.id])continue;
- mark[now.id]=;
- }
- else
- {
- if(mark[now.id])
- {
- ans=;
- return;
- }
- }
- if(now.step==)continue;
- int cnt=-;
- for(int i=;i<=;++i)
- {
- for(int j=;j<=;++j)
- {
- ++cnt;
- mp[i][j]=(int)(now.id>>cnt)&;
- }
- }
- for(int i=;i<=;++i)
- {
- for(int j=;j<=;++j)
- {
- if(mp[i][j]&&ok(i-,j)&&mp[i-][j]==&&ok(i-,j)&&mp[i-][j]==)
- {
- mp[i-][j]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i-][j]=,mp[i][j]=;
- }
- if(mp[i][j]&&ok(i+,j)&&mp[i+][j]==&&ok(i+,j)&&mp[i+][j]==)
- {
- mp[i+][j]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i+][j]=,mp[i][j]=;
- }
- if(mp[i][j]&&ok(i,j-)&&mp[i][j-]==&&ok(i,j-)&&mp[i][j-]==)
- {
- mp[i][j-]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i][j-]=,mp[i][j]=;
- }
- if(mp[i][j]&&ok(i,j+)&&mp[i][j+]==&&ok(i,j+)&&mp[i][j+]==)
- {
- mp[i][j+]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i][j+]=,mp[i][j]=;
- }
- //--------------------------------------------------------------
- if(mp[i][j]==&&ok(i-,j)&&mp[i-][j]==)
- {
- mp[i-][j]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i-][j]=;mp[i][j]=;
- }
- if(mp[i][j]==&&ok(i+,j)&&mp[i+][j]==)
- {
- mp[i+][j]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i+][j]=;mp[i][j]=;
- }
- if(mp[i][j]==&&ok(i,j-)&&mp[i][j-]==)
- {
- mp[i][j-]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i][j-]=;mp[i][j]=;
- }
- if(mp[i][j]==&&ok(i,j+)&&mp[i][j+]==)
- {
- mp[i][j+]=,mp[i][j]=;
- q.push({get(),now.step+});
- mp[i][j+]=;mp[i][j]=;
- }
- }
- }
- }
- }
- int main()
- {
- int x,y;
- while(~sc("%d%d",&x,&y))
- {
- ans=;
- mark.clear();
- mem(mp,);
- mp[x][y]=;
- for(int i=;i<=;++i)
- {
- sc("%d%d",&x,&y);
- mp[x][y]=;
- }
- bfs(get(),);
- mem(mp,);
- for(int i=;i<=;++i)
- {
- sc("%d%d",&x,&y);
- mp[x][y]=;
- }
- bfs(get(),);
- if(ans)
- pr("YES\n");
- else
- pr("NO\n");
- }
- return ;
- }
- /**************************************************************************************/
HDU1401(双向BFS)的更多相关文章
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
随机推荐
- Monkey测试感想
monkey测试主要做随机的黑盒测试,通过不断输入伪随机的事件流来测试应用的稳定性,但是由于monkey太过皮,太过随机,最后根本无法控制,很容易陷于一个页面无法出来,或者陷入某个无关紧要的地方无法出 ...
- HTML页面预览表格文件内容
背景简介 在将一个表格文件上传到服务器上之前,JS读取表格文件并将文件内容输出到页面中 vue项目 第三方 exceljs 安装 npm install exceljs 插件使用 github 中文文 ...
- Java操作Cookie方法
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- MySQL 创建和删除数据表
创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (column_name col ...
- OpenCL如何判定一个work-group的最大Local Memory大小
最近有不少朋友提及到如何能在运行时获悉一个GPU的最大local memory的尺寸.由于OpenCL对各类处理器开放,因此不同处理器所拥有的local memory大小也各不相同.即便是GPU,甚至 ...
- Eclipse项目修改编译jdk版本(Failed to read candidate component class: file 处理)
转: Failed to read candidate component class: file 处理 2018年03月09日 07:15:54 爱萨萨 阅读数 10041 出错现象: org. ...
- React开发环境配置
本文以上一篇文章继续配置:npm安装及环境配置<https://www.cnblogs.com/hzb462606/p/11565275.html> 使用 create-react-app ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_4.RabbitMQ研究-安装RabbitMQ
RabbitMQ由Erlang语言开发,Erlang语言用于并发及分布式系统的开发,在电信领域应用广泛,OTP(Open Telecom Platform)作为Erlang语言的一部分,包含了很多基于 ...
- 电力项目十一--js添加浮动框
1.添加浮动窗口样式 <!-- 浮动窗口样式css begin --> <style type="text/css"> #msg_win{border:1p ...
- iOS——plist的创建,数据写入与读取
iOS中plist的创建,数据写入与读取 Documents:应用将数据存储在Documents中,但基于NSuserDefaults的首选项设置除外Library:基于NSUserDefaults的 ...