蓝桥杯历届试题——九宫重排(bfs)

问题描述
  如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着。与空格子相邻的格子中的卡片可以移动到空格中。经过若干次移动,可以形成第二个图所示的局面。
这里写图片描述这里写图片描述
  我们把第一个图的局面记为:12345678.
  把第二个图的局面记为:123.46758
  显然是按从上到下,从左到右的顺序记录数字,空格记为句点。
  本题目的任务是已知九宫的初态和终态,求最少经过多少步的移动可以到达。如果无论多少步都无法到达,则输出-1。
输入格式
  输入第一行包含九宫的初态,第二行包含九宫的终态。
输出格式
  输出最少的步数,如果不存在方案,则输出-1。
样例输入
12345678.
123.46758
样例输出
3
样例输入
13524678.
46758123.
样例输出
22

明显要用搜索,我这里用了bfs
一定要剪掉许多不可行的情况,比如某个排列曾经出现的话就不用再继续搜了,寻找下一种情况的时候也不用急着放进队列,有可能这时就已经找到了答案,如果还要放进队列就又会多出许多种情况

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 100005
#define Mod 1000000007
using namespace std;
char hajime[5][5],owali[5][5];
struct Node
{
    int x,y;
    long long step;
    char pre;
    char map[5][5];
};
bool check(Node a)
{
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
            if(a.map[i][j]!=owali[i][j])
                return false;
    return true;
}
set<string> hsh;
bool getvis(Node a)
{
    string tmp="";
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
        {
            tmp+=a.map[i][j];
        }
    if(hsh.find(tmp)!=hsh.end())
        return false;
    hsh.insert(tmp);
    return true;
}
bool bfs(int x,int y)
{
    Node start;
    start.x=x,start.y=y;
    start.step=0;
    start.pre='X';
    for(int i=0; i<3; ++i)
        for(int j=0; j<3; ++j)
            start.map[i][j]=hajime[i][j];
    queue<Node> q;
    q.push(start);
    while(!q.empty())
    {
        Node tmp=q.front(),tmp1;
        q.pop();
        if(check(tmp))
        {
            cout<<tmp.step<<endl;
            return true;
        }
        tmp1=tmp;
        if(tmp1.pre!='U'&&tmp1.x+1<3)
        {
            tmp1.step++;
            tmp1.pre='D';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x+1][tmp1.y]);
            tmp1.x++;
            if(getvis(tmp1))
            {
                if(check(tmp1))
                {
                    cout<<tmp1.step<<endl;
                    return true;
                }
                q.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='D'&&tmp1.x-1>=0)
        {
            tmp1.step++;
            tmp1.pre='U';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x-1][tmp1.y]);
            tmp1.x--;
            if(getvis(tmp1))
            {
                if(check(tmp1))
                {
                    cout<<tmp1.step<<endl;
                    return true;
                }
                q.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='L'&&tmp1.y+1<3)
        {
            tmp1.step++;
            tmp1.pre='R';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y+1]);
            tmp1.y++;
            if(getvis(tmp1))
            {
                if(check(tmp1))
                {
                    cout<<tmp1.step<<endl;
                    return true;
                }
                q.push(tmp1);
            }
        }
        tmp1=tmp;
        if(tmp1.pre!='R'&&tmp1.y-1>=0)
        {
            tmp1.step++;
            tmp1.pre='L';
            swap(tmp1.map[tmp1.x][tmp1.y],tmp1.map[tmp1.x][tmp1.y-1]);
            tmp1.y--;
            if(getvis(tmp1))
            {
                if(check(tmp1))
                {
                    cout<<tmp1.step<<endl;
                    return true;
                }
                q.push(tmp1);
            }
        }
    }
    return false;
}
int main()
{
    char tmp[100];
    scanf("%s",tmp);
    int x,y;
    for(int i=0; tmp[i]!='\0'; ++i)
    {
        hajime[i/3][i%3]=tmp[i];
        if(tmp[i]=='.')
        {
            x=i/3;
            y=i%3;
            hajime[i/3][i%3]='0';
        }
    }
    scanf("%s",tmp);
    for(int i=0; tmp[i]!='\0'; ++i)
    {
        owali[i/3][i%3]=tmp[i];
        if(tmp[i]=='.')
            owali[i/3][i%3]='0';
    }
    if(!bfs(x,y))
        cout<<-1<<endl;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++实现广度优先算法实现九宫重排的示例代码: ```cpp #include <iostream> #include <queue> #include <vector> #include <map> using namespace std; // 目标状态 const vector<int> targetState = {1, 2, 3, 4, 5, 6, 7, 8, 0}; // 表示状态的结构体 struct State { vector<int> nums; // 状态数组 int zeroIndex; // 0的位置 int step; // 步数 State() {} State(vector<int> nums, int zeroIndex, int step) : nums(nums), zeroIndex(zeroIndex), step(step) {} }; // 广度优先搜索 int bfs(vector<int> startState) { queue<State> q; // 队列 map<vector<int>, bool> visited; // 记录已访问过的状态 State initState(startState, 0, 0); // 初始状态 q.push(initState); visited[startState] = true; while (!q.empty()) { State curState = q.front(); q.pop(); if (curState.nums == targetState) { // 已找到目标状态 return curState.step; } // 上下左右四个方向 int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; for (int i = 0; i < 4; i++) { int x = curState.zeroIndex / 3 + dx[i]; int y = curState.zeroIndex % 3 + dy[i]; if (x >= 0 && x < 3 && y >= 0 && y < 3) { // 位置合法 vector<int> newNums = curState.nums; swap(newNums[curState.zeroIndex], newNums[x * 3 + y]); if (!visited[newNums]) { // 新状态未曾访问过 State newState(newNums, x * 3 + y, curState.step + 1); q.push(newState); visited[newNums] = true; } } } } return -1; // 无法到达目标状态 } int main() { vector<int> startState = {2, 8, 3, 1, 6, 4, 7, 0, 5}; int step = bfs(startState); cout << step << endl; // 输出步数 return 0; } ``` 其,`State`结构体表示一个状态,包括`nums`表示状态数组,`zeroIndex`表示0的位置,`step`表示从初始状态到达该状态的步数。`bfs`函数实现广度优先搜索,`startState`为初始状态数组,返回从初始状态到目标状态需要的步数。在搜索过程,使用队列`q`存储待搜索的状态,使用`visited`记录已访问过的状态,遇到新状态时将其加入队列并标记为已访问。每次从队列取出一个状态,遍历其上下左右四个方向,生成新状态,并判断该状态是否已访问过,若未访问过则将其加入队列。最终,若搜索完所有状态仍未找到目标状态,则返回-1表示无法到达目标状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值