当前位置: 首页>>代码示例>>C++>>正文


C++ Puzzle::get_width方法代码示例

本文整理汇总了C++中Puzzle::get_width方法的典型用法代码示例。如果您正苦于以下问题:C++ Puzzle::get_width方法的具体用法?C++ Puzzle::get_width怎么用?C++ Puzzle::get_width使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Puzzle的用法示例。


在下文中一共展示了Puzzle::get_width方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: manhattan

int manhattan(const Puzzle& state, const Puzzle& solution) {
    int result = -1;

    /*
     * These loops compare two vectors piece by piece,
     * finding the row and column indeces for corresponding
     * pieces, and adding the magnitudes of the differences.
     *
     * This is presently O(n^2).
     * TODO: investigate O(n) solution
     */
    if(state.get_width() == solution.get_width()) {
        size_t width = state.get_width();
        int i = 0;
        for(TileIt it1 = state.tiles.begin(); it1 != state.tiles.end(); it1++, i++) {
            if(*it1 != 0) {

                int j = 0;
                for(TileIt it2 = solution.tiles.begin(); *it1 != *it2; it2++, j++);

                int r1 = i / width;
                int r2 = j / width;
                int c1 = i % width;
                int c2 = j % width;

                result += abs(r2 - r1) + abs(c2 - c1);
            }
        }
    }

    return result;
}
开发者ID:jonolss,项目名称:libastar,代码行数:32,代码来源:n-puzzle.cpp

示例2: displaced

/*
 * These are the three examined heuristic functions.
 * displaced(...) counts how many tiles are out of place
 * manhattan(...) determines how far away each tile is from
 * its correct location, as specified in the solution.
 * sumdisman(...) simply sums displaced(...) and manhattan(...).
 */
int displaced(const Puzzle& state, const Puzzle& solution) {
    int result = -1;

    if(state.get_width() == solution.get_width()) {
        result = 0;

        for(TileIt it1 = state.tiles.begin(), it2 = solution.tiles.begin(); it1 != state.tiles.end(); it1++, it2++) {
            if(*it1 != 0 && *it1 != *it2) {
                result++;
            }
        }
    }

    return result;
}
开发者ID:jonolss,项目名称:libastar,代码行数:22,代码来源:n-puzzle.cpp


注:本文中的Puzzle::get_width方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。