首页 > 解决方案 > 在下面的 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;
    }

标签: c++

解决方案


这些只是数值。如果您将 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)。


推荐阅读