数字华容道(搜索的应用)

在一个 3×3的网格中,1∼8 这 8 个数字和一个 x 恰好不重不漏地分布在这 3×3 的网格中。

例如:

1 2 3
x 4 6
7 5 8

在游戏过程中,可以把 x 与其上、下、左、右四个方向之一的数字交换(如果存在)。

我们的目的是通过交换,使得网格变为如下排列(称为正确排列):

1 2 3
4 5 6
7 8 x

例如,示例中图形就可以通过让 x 先后与右、下、右三个方向的数字交换成功得到正确排列。

交换过程如下:

1 2 3   1 2 3   1 2 3   1 2 3
x 4 6   4 x 6   4 5 6   4 5 6
7 5 8   7 5 8   7 x 8   7 8 x

现在,给你一个初始网格,请你求出得到正确排列至少需要进行多少次交换。

输入格式

输入占一行,将 3×3 的初始网格描绘出来。

例如,如果初始网格如下所示:

1 2 3 
x 4 6 
7 5 8 

则输入为:1 2 3 x 4 6 7 5 8

输出格式

输出占一行,包含一个整数,表示最少交换次数。

如果不存在解决方案,则输出 −1−1。

输入样例:

2 3 4 1 5 x 7 6 8

输出样例

19

相信大家应该都听过或者玩过数字华容道这个游戏,这道题的x其实就是数字华容道3 x 3宫格当中的那个空缺板块。这个题目主要的难度在于一维数组到二维数组的下标转变,其实如果这道题的输入是3 x 3的相信学过搜索的初学者也会决定这个题很简单

思路:

我们把最初输入的s定义为初始状态,定义一个dist[s] = 0,那么x所在的位置去上下左右交换(不越界的前提下),有4个状态,那这四个状态的dist[] = dist[s] + 1,那么就很清晰了,只要我们当前搜到的这个状态的s == '12345678x'就可以去跟新一下答案,找到最小值即可

我们先暴力搜索试试呗,先暴力找找规律

dfs

#include <iostream>
#include <unordered_map>

using namespace std;

int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

string target = "12345678x";
int res = 0x3f3f3f3f;

unordered_map<string,bool> st;

void dfs(string s,int t){
    if(s == target){
        res = min(res,t);
        return;
    }
    
    if(t >= res) return;
    
    int index = s.find('x');
    int x = index / 3,y = index % 3;
    for(int i = 0;i < 4;i ++){
        int nx = x + d[i][0],ny = y + d[i][1];
        if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
        string ss = s;
        swap(ss[index],ss[nx * 3 + ny]);
        if(st[ss]) continue;
        st[ss] = true;
        dfs(ss,t + 1);
        st[ss] = false;
        swap(ss[index],ss[nx * 3 + ny]);
    }
}

int main()
{
    string s = "";
    for(int i = 0;i < 9;i ++){
        char ch;
        cin >> ch;
        s += ch;
    }
    
    dfs(s,0);
    
    cout << res << endl;
    
    return 0;
}

这样做呢超时了,因为确实状态如果多起来会不断的递归就搜爆了。用bfs去优化,这样可以减少很多不必要的状态搜索和回溯

bfs

#include <iostream>
#include <queue>
#include <unordered_map>
#include <string>
#include <algorithm>

using namespace std;

int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

unordered_map<string,int> dist;

string target = "12345678x";

int bfs(string s)
{
    queue<string> q;
    q.push(s);
    
    while(!q.empty()){
        auto t = q.front();
        q.pop();
        
        int dis = dist[t];
        if(t == target) return dis;
        
        int index = t.find('x');
        int u = index / 3, v = index % 3;
        for(int i = 0;i < 4;i ++){
            int nx = u + d[i][0], ny = v + d[i][1];
            if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
            string ss = t;
            swap(ss[index],ss[nx * 3 + ny]);
            if(dist.find(ss) == dist.end()){
                dist[ss] = dis + 1;
                q.push(ss);
            }
        }
    }
    
    return -1;
}

int main()
{
    string s = "";
    for(int i = 0;i < 9;i ++){
        char ch;
        cin >> ch;
        s += ch;
    }
    dist[s] = 0;
    
    cout << bfs(s) << endl;
    
    return 0;
}

加油

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值