挑战程序设计(算法和数据结构)—九宫格

九宫格

题目19.2链接:8 Puzzle
需要自己定义转移状态,并使用BFS来确定最小路径。标准的BFS题,只不过状态不好定义。
代码如下:

#include 
#include 
#include 
#include 
using namespace std;

const int N = 3;
const int N2 = 9;
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
//string dir[5] = {"r", "l", "d", "u"};//右、左、下、上
char dir[5] = {'r', 'l', 'd', 'u'};//右、左、下、上
struct Puzzle
{
    int f[N2];//矩阵数组
    int space;//空格的下标
    string path;//路径
    bool operator < (const Puzzle &p) const {//重载<,以便于map使用其为key值
        for(int i=0; i p.f[i];
        }
        return false;
    }
};
Puzzle P;
bool isTarget(Puzzle s)
{
    for(int i=0; i V;
    queue Q;
    Q.push(P);//先进一个初始元素
    V[P] = true;//标记已访问
    Puzzle u, v;
    while(!Q.empty())
    {
        u = Q.front();
        Q.pop();
        if(isTarget(u)) return u.path.size();//到达目标状态退出
        int sx = u.space/N, sy = u.space%N;//求空格的坐标
        //cout << 1 << endl;
        for(int i=0; i<4; i++)//遍历四个方向
        {
            int tx = sx + dx[i], ty = sy + dy[i];//下一步
            if(tx>=N || tx<0 || ty>=N || ty<0) continue;//出矩阵
            v = u;//新状态
            swap(v.f[u.space], v.f[tx*N+ty]);
            v.space = tx*N+ty;
            if(!V.count(v))//还没遍历过
            {
                V[v] = true;
                v.path += dir[i];
                Q.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    for(int i=0; i> P.f[i];
        if(P.f[i]==0)
        {
            P.space = i;
        }
    }
    P.path = "";
    cout << bfs(P) << endl;
    return 0;
}

你可能感兴趣的:(挑战程序设计-算法和数据结构)