c++ - 在下面的 C++ 代码行中使用 (n/8) 和 (n*7/8) 的目的是什么?
问题描述
我正在研究 A-star 算法的 C++ 代码和此链接中的代码:http: //code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/
// fillout the map matrix with a '+' pattern
for(int x=n/8;x<n*7/8;x++)
{
map[x][m/2]=1;
}
for(int y=m/8;y<m*7/8;y++)
{
map[n/2][y]=1;
}
解决方案
这些只是数值。如果您将 2Dmap
视为线性 2D 空间,n/8
则会n*7/8
分别给您 1/8 和 7/8 的空间。例如,对于大小为 8x8 的地图,结果将如下所示:
01234567 n
0 ........
1 ....1... ---\ starts at m/8
2 ....1... |
3 ....1... |
4 .111111. |
5 ....1... |
6 ....1... ---/ ends at one less than m*7/8
7 ........
m \-------- positioned at n/2
因为n == m == 8
, n/8 = 1
, and n*7/8 == 7
(但循环被指定在 7 之前结束 1)。
推荐阅读
- jsp - c:当测试表达式返回假时
- linux - make dtbs 返回“没有规则来制作目标 'dtbs'”
- reporting-services - SSRS 报告正在传递给非订阅者
- javascript - React 应用程序构建无法将 SVG 作为组件加载,但它适用于开发
- python - 继承和访问实例变量 Python 的问题
- ms-access - 每行仅更新一次基于多个条件的查询
- ruby-on-rails - Rails:使用 RestClient 的外部 API 集成(未定义的局部变量或方法“用户名”)
- javascript - 根据预定义的迭代器编号从数组中提取值的最佳方法是什么?
- cordova - Cordova 或 Ionic 用于在 android 上写入/读取文件?
- java - 为什么 SingleThreadExecutor 使用 BlockingQueue?