博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1915解题报告
阅读量:4204 次
发布时间:2019-05-26

本文共 2806 字,大约阅读时间需要 9 分钟。

广度搜索,从起始点开始,每次向8个方向探索。直到到达终点。

这里面类似于树按层次遍历输出每一层的节点。cur记录本层的节点数目,next记录下层节点的数目。当cur==0即为本层处理完了,cur = next。处理下层。

另外,用一个checked数组记录该节点是否访问,避免重复计算。一个节点(x, y)到数组的index的映射使用一个简单的hash函数:index = x*len+y。

重复代码挺多的,不过AC后懒得改了。。。364k memory, 188ms.

有个,看着很高级的样子,但没去实现,不知道效率能高多少。

#include 
#include
using namespace std;const int maxsize = 301 * 301;bool checked[maxsize];//int xstep[8] = {-1, -2, -2, -1, 1, 2, 2, 1};//int ystep[8] = {-2, -1, 1, 2, -2, -1, 1, 2};int knightmove(int sx, int sy, int ex, int ey, int len){ int move = 0; queue
> que; que.push(make_pair
(sx, sy)); int cur = 1, next = 0; while(!que.empty()) { pair
now = que.front(); que.pop(); cur--; //move = checked[now.first * len + now.second]; if(now.first == ex && now.second == ey) { return move; } if(!checked[(now.first - 1) * len + (now.second - 2)] && now.first - 1 >= 0 && now.second - 2 >= 0) { que.push(make_pair
(now.first - 1, now.second - 2)); checked[(now.first - 1) * len + (now.second - 2)] = true; next++; } if(!checked[(now.first - 2) * len + (now.second - 1)] && now.first - 2 >= 0 && now.second - 1 >= 0) { que.push(make_pair
(now.first - 2, now.second - 1)); checked[(now.first - 2) * len + (now.second - 1)] = true; next++; } if(!checked[(now.first - 2) * len + (now.second + 1)] && now.first - 2 >= 0 && now.second + 1 < len) { que.push(make_pair
(now.first - 2, now.second + 1)); checked[(now.first - 2) * len + (now.second + 1)] = true; next++; } if(!checked[(now.first - 1) * len + (now.second + 2)] && now.first - 1 >= 0 && now.second + 2 < len) { que.push(make_pair
(now.first - 1, now.second + 2)); checked[(now.first - 1) * len + (now.second + 2)] = true; next++; } if(!checked[(now.first + 1) * len + (now.second - 2)] && now.first + 1 < len && now.second - 2 >= 0) { que.push(make_pair
(now.first + 1, now.second - 2)); checked[(now.first + 1) * len + (now.second - 2)] = true; next++; } if(!checked[(now.first + 2) * len + (now.second - 1)] && now.first + 2 < len && now.second - 1 >= 0) { que.push(make_pair
(now.first + 2, now.second - 1)); checked[(now.first + 2) * len + (now.second - 1)] = true; next++; } if(!checked[(now.first + 2) * len + (now.second + 1)] && now.first + 2 < len && now.second + 1 < len) { que.push(make_pair
(now.first + 2, now.second + 1)); checked[(now.first + 2) * len + (now.second + 1)] = true; next++; } if(!checked[(now.first + 1) * len + (now.second + 2)] && now.first + 1 < len && now.second + 2 < len) { que.push(make_pair
(now.first + 1, now.second + 2)); checked[(now.first + 1) * len + (now.second + 2)] = true; next++; } if(cur == 0) { move++; cur = next; next = 0; } }}int main(){ int n; cin>>n; for(int i = 0; i < n; ++i) { memset(checked, false, maxsize); int len; cin>>len; int sx, sy, ex, ey; cin>>sx>>sy>>ex>>ey; cout<

转载地址:http://nxxli.baihongyu.com/

你可能感兴趣的文章
mysql数据库主从同步的问题解决方法
查看>>
mysql 配置 - on xFanxcy.com
查看>>
mysql一: 索引优化
查看>>
测试人员,今天再不懂BDD就晚了!
查看>>
害怕自动化(1)
查看>>
深圳市软件质量提升工程系列活动——安全测试百人大课堂
查看>>
LoadRunner如何在脚本运行时修改log设置选项?
查看>>
QC数据库表结构
查看>>
自动化测试工具的3个关键部分
查看>>
测试工具厂商的编程语言什么时候“退休”?
查看>>
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
QTP的智能识别(Smart Identification)过程
查看>>
LoadRunner各协议所需耗费的内存资源表
查看>>
AutomatedQA收购Smart Bear?
查看>>
使用QTP进行WEB页面性能测试
查看>>
LoadRunner的VS.NET 2005插件
查看>>
LoadRunner中如何验证下载的文件大小、统计下载时间、度量下载速度?
查看>>
LoadRunner脚本评审Checklist
查看>>