ALDS1_13_B:8 Puzzle 九宫格拼图

题目链接

题目给一个九宫格,求最少用多少步能还原到初始状态

代码如下:

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 3
#define N2 9
class puzzle{
public:
    int f[N2];
    int space;
    string path;

    bool operator < (const puzzle &p) const {
        for(int i=0;i<N2;i++){
            if(f[i]!=p.f[i]) return f[i]>p.f[i];
        }
        return false;
    }

};

static const int dx[4]={-1,+1,0,0};
static const int dy[4]={0,0,-1,+1};
static const char dir[4]={'u','d','l','r'};

bool isTarget(puzzle u){
    for(int i=0;i<N2;i++){
        if(u.f[i]!=i+1) return false;
    }
    return true;
}

string bfs(puzzle s){
    queue<puzzle> Q;
    map<puzzle,bool> V;
    puzzle u,v;
    s.path="";
    Q.push(s);
    V[s]=true;
    while(!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u)) return u.path;
        int sx=u.space/N,sy=u.space%N;
        for(int r=0;r<4;r++){
            int tx=sx+dx[r],ty=sy+dy[r];
            if(tx<0 || tx>=N || ty<0 || ty>=N) continue;
            v=u;
            swap(v.f[v.space],v.f[tx*N+ty]);
            v.space=tx*N+ty;
            if(!V[v]){
                V[v]=true;
                v.path+=dir[r];
                Q.push(v);
            }
        }
    }
    return "unsolvable";
}

int main() {
    puzzle in;
    for(int i=0;i<N2;i++){
            cin>>in.f[i];
            if(in.f[i]==0){
                in.f[i]=N2;
                in.space=i;
            }
    }
    string ans=bfs(in);
    cout<<ans.size()<<endl;
    return 0;
}

错点:
1.输入的时候可以直接把0当做9读入;
2.检查是否经历到了已经遍历过的状态时,可以使用map,不一定要用set;

猜你喜欢

转载自blog.csdn.net/qq_33982232/article/details/81839972