滑块脱逃(木块华容道)

木块逃脱-一个有趣的益智游戏,让你在娱乐的同事变得更加聪明。
木块逃脱-是一个简单而又容易上瘾的游戏,其目标是通过滑动木块,找到出口。
木块逃脱分为三个等级,共有300个关卡供您挑战!
如果您喜欢大脑训练累的游戏,逻辑游戏,数字游戏,拼图游戏或者数独的话,那么请挑战“木块逃脱”

我们相信您一定会喜欢上它!

以上内容引用自百度百科

附上游戏截图:


规则:

对于横着的木块只能横向移动,同理竖着的木块只能纵向移动。

将红色木块“逃脱”即为胜利。

分析:

整个图为6*6的方格,目标块显然必然存在于第三行,对于每一个木块只能在某一行或者某一列移动,所以每一个木块存在的状态是十分有限的,再加上方格只有6*6,得到的最后的总状态数为:每一个木块可能存在的位置的乘机(当然,这是最极限的分析,由木块不可重叠再筛选的话,状态数就更加少了)!

想要得到最优解,即步数最少的解法,显然可以通过BFS(宽度优先搜索)就可以解答

对于状态的表示,我则是利用一个vector数组表示的,每一个vector存储的是所有木块的状态,每一个木块的状态用一个三位数或者四位数表示(行列数,起始点以及木块长度):


对于图中的绿色木块表示为:522 (第5行,起始点为2,长度为2)

对于图中的红色木块表示为:1023(第4(10-6)列 ,起始点为2,长度为3)

然后通过BFS搜索得到最优解,接下来就是记录路径的问题了,我是采用了map映射的方式,对于每一个状态,记录得到当前状态的父状态(即前一个状态)的方式,逐步迭代得到完整路径。

代码如下(C++实现):

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cstdio>
#include <map>
using namespace std;
//华容道游戏(也叫滑块逃脱),由6*6的方格组成,方块有1*2跟1*3两种。
//解题思路是利用BFS(宽度优先搜索的做法),总的状态数上限为(9^12)(考虑到块不重叠的情况,实际复杂度远小于这个复杂度)
//状态表示:
//对于每一个块,要么是只能横向移动,要么只能是纵向移动,分为横纵利用每一个块的起始点跟长度标记每一个块(十二个两位数。eg:12表示起始点为1,长度为2)
int grid[10][10],keys;
stack<vector<int> > S;
vector<int> st; // 初始状态
void getGrid(vector<int> status)
{
    memset(grid,0,sizeof(grid));
    int nums = status.size();
    for(int i = 0 ; i < nums ; i++)
    {
        //a为行列数(1-6为行,7-12为列),b为起始点,c为长度(2或3)
        int a = status[i]/100,b = (status[i]/10)%10,c = status[i]%10;
        //cout << a << " " << b << " " << c << endl;
        if(a<=6)
        {
            for(int j = b; j <= b+c-1;j++)grid[a][j] = i+1;
            if(a==3) keys = i+1;
        }
        else
        {
            for(int j = b; j <= b+c-1;j++)grid[j][a-6] = i+1;
        }
    }
//    for(int i=1;i<=6;i++)
//    {
//        for(int j=1;j<=6;j++)
//        {
//            cout << grid[i][j] << " ";
//        }
//        cout << endl;
//    }
}
void show(vector<int> status)
{
    int g[10][10];
    memset(g,0,sizeof(g));
    int nums = status.size();
    for(int i = 0 ; i < nums ; i++)
    {
        //a为行列数(1-6为行,7-12为列),b为起始点,c为长度(2或3)
        int a = status[i]/100,b = (status[i]/10)%10,c = status[i]%10;
        //cout << a << " " << b << " " << c << endl;
        if(a<=6)
        {
            for(int j = b; j <= b+c-1;j++)g[a][j] = i+1;
            if(a==3) keys = i+1;
        }
        else
        {
            for(int j = b; j <= b+c-1;j++)g[j][a-6] = i+1;
        }
    }
    for(int i=1; i<=6; i++)
    {
        for(int j=1; j<=6; j++)
        {
            printf("%-3d",g[i][j]);
        }
        printf("\n");
    }
}
map<vector<int>,bool> mp; //标记状态
map<vector<int>,vector<int> > fa;//标记父亲节点(为了寻找路径)
bool check()
{

    for(int i=6;i>=1;i--)
    {
        if(grid[3][i]==0) continue;
        else
        {
            if(grid[3][i]==keys) return true;
            else return false;
        }
    }
    return false;
}
void solve(vector<int> status)
{
    queue<vector<int>> qu;
    qu.push(status);
    while(!qu.empty())
    {
        vector<int> now = qu.front(),tmp;
        qu.pop();
        int nums = now.size();
        getGrid(now);
        if(check())
        {
            S.push(now);
            while(now!=st)
            {
                now = fa[now];
                S.push(now);
            }

            break;
        }
        for(int i=0;i<nums;i++)
        {
            int a = now[i]/100,b = (now[i]/10)%10,c = now[i]%10;
            if(a<=6) //横向
            {
                if(grid[a][b-1]==0&&b-1>=1) //向反方向移动一格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-10;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b-1]==0&&grid[a][b-2]==0&&b-2>=1) //向反方向移动两格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-20;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b-1]==0&&grid[a][b-2]==0&&grid[a][b-3]==0&&b-3>=1) //向反方向移动三格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-30;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b-1]==0&&grid[a][b-2]==0&&grid[a][b-3]==0&&grid[a][b-4]==0&&b-4>=1) //向反方向移动四格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-40;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b+c]==0&&b+c<=6) //向正方向移动一格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+10;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b+c]==0&&grid[a][b+c+1]==0&&b+c+1<=6) //向正方向移动两格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+20;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b+c]==0&&grid[a][b+c+1]==0&&grid[a][b+c+2]==0&&b+c+2<=6) //向正方向移动三格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+30;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[a][b+c]==0&&grid[a][b+c+1]==0&&grid[a][b+c+2]==0&&grid[a][b+c+3]==0&&b+c+3<=6) //向正方向移动四格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+40;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }
            }
            else     //纵向
            {
                if(grid[b-1][a-6]==0&&b-1>=1) //向反方向移动一格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-10;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b-1][a-6]==0&&grid[b-2][a-6]==0&&b-2>=1) //向反方向移动两格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-20;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b-1][a-6]==0&&grid[b-2][a-6]==0&&grid[b-3][a-6]==0&&b-3>=1) //向反方向移动三格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-30;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b-1][a-6]==0&&grid[b-2][a-6]==0&&grid[b-3][a-6]==0&&grid[b-4][a-6]==0&&b-4>=1) //向反方向移动四格
                {
                    tmp = now;
                    tmp[i] = tmp[i]-40;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b+c][a-6]==0&&b+c<=6)   //向正方向移动一格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+10;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b+c][a-6]==0&&grid[b+c+1][a-6]==0&&b+c+1<=6)   //向正方向移动两格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+20;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b+c][a-6]==0&&grid[b+c+1][a-6]==0&&grid[b+c+2][a-6]==0&&b+c+2<=6)   //向正方向移动三格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+30;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }

                if(grid[b+c][a-6]==0&&grid[b+c+1][a-6]==0&&grid[b+c+2][a-6]==0&&grid[b+c+3][a-6]==0&&b+c+3<=6)   //向正方向移动四格
                {
                    tmp = now;
                    tmp[i] = tmp[i]+40;
                    if(!mp[tmp])
                    {
                        fa[tmp] = now;
                        qu.push(tmp);
                        mp[tmp] = true;
                    }
                }
            }
        }
    }
}
int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
#endif
    int x;
    while(cin>>x) st.push_back(x); //读入每一个木块的状态
    solve(st);
//    getGrid(st);
    int step = 1;
    while(!S.empty())
    {
        cout << "第" << step++ << "步之后图为" << endl;
        show(S.top());
        S.pop();
    }
    return 0;
}

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值