首页 > 其他分享 >滑动谜题

滑动谜题

时间:2023-05-27 14:45:02浏览次数:25  
标签:string int 谜题 times board str 滑动 size

在一个 2 x 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示。一次 移动 定义为选择 0 与一个相邻的数字(上下左右)进行交换
最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开

1. 广度优先搜索

class Solution {
public:
    vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
    int m; int n;
    int slidingPuzzle(vector<vector<int>>& board) {
        m = board.size(); n = board[0].size();
        set<string> s;
        queue<string> q;
        string str = vec_str(board);
        int count = 0;
        s.insert(str);
        q.push(str);
        while(!q.empty()){
            int len = q.size();
            for(int times=0;times<len;times++){
                string cur = q.front(); q.pop();
                if(check(cur)) return count;
                int index = cur.find('0');
                int x = index/n; int y =index%n;
                for(int i=0;i<dir.size();i++){//遍历四个方向
                    int nx = x + dir[i][0];
                    int ny = y + dir[i][1];
                    if(nx<0||nx==m||ny<0||ny==n) continue;
                    string temp = cur;
                    swap(temp[index],temp[nx*n+ny]);
                    if(s.count(temp)) continue;
                    s.insert(temp);
                    q.push(temp);
                }
            }
            count++;
        }
        return -1;
    }


    string vec_str(vector<vector<int>>& mat){
        string res;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                res.push_back(mat[i][j]+'0');
        return res;
    }
    bool check(string &s){
        return s=="123450";
    }
};

标签:string,int,谜题,times,board,str,滑动,size
From: https://www.cnblogs.com/929code/p/17436699.html

相关文章

  • html在移动端滑动无效的解决方法
    排查下是否有类似touchmove类似的事件绑定,可能就是其中取消了默认的事件行为。解决方法,在你想滚动的直接父级元素上添加@touchmove.stop="voidnull"类似的事件绑定应该就可以解决了。这里利用了事件冒泡终止的方式。......
  • 谜题大陆要塞攻略
    兵团规则1:首先要了解兵团中小兵死亡规则和战损分配原则.打要塞时集结的兵团中,小兵死亡顺序如下:步兵>骑兵>弓兵>板车.前面一个兵种全部伤亡后,才会轮到下一个兵种.例如一次集结中,有3人参加集结,且步骑弓板各5k,最终死亡了12k,死亡的兵种必定为5k步兵,5k骑兵和2k弓......
  • 直播商城系统源码,BottomSheetDialog实现-底部滑动栏
    直播商城系统源码,BottomSheetDialog实现-底部滑动栏bottom_popwindoow.xml中的代码 <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"......
  • 力扣239(Java)- 滑动窗口最大值(困难)
    题目:给你一个整数数组nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k 个数字。滑动窗口每次只向右移动一位。返回滑动窗口中的最大值。示例1:输入:nums=[1,3,-1,-3,5,3,6,7],k=3输出:[3,3,5,5,6,7]解释:滑动窗口的位......
  • 代码随想录算法训练营第13天 | ● 239. 滑动窗口最大值 ● 347.前 K 个高频元素 ● 总
     第五章 栈与队列part03今日内容:  ●  239. 滑动窗口最大值●  347.前 K 个高频元素●  总结  详细布置    239. 滑动窗口最大值 (一刷至少需要理解思路) 之前讲的都是栈的应用,这次该是队列的应用了。 本题算比较有难度的,需要自己去构造......
  • 代码随想录算法训练营第十三天|239. 滑动窗口最大值、347. 前 K 个高频元素
    【参考链接】239.滑动窗口最大值【注意】 1.使用单调队列的经典题目。2.大顶堆每次只能弹出最大值,无法移除其他数值,造成大顶堆维护的不是滑动窗口里面的数值了。所以不能用大顶堆。3.需要一个队列,放进去窗口里的元素,然后随着窗口的移动,队列也一进一出,每次移动之后,队列告诉......
  • android-card-slide-panel模仿探探首页卡片左右滑动效果,滑动流畅,卡片view无限重生...
    android-card-slide-panel类别: 图像(Image)打分: ★★★★★更新: 2015-12-0312:37大小: 10003kb开发环境: AndroidStudio浏览: 913次下载: 213次项目地址: https://github.com/xmuSistone/android-card-slide-panel图片,手势下载 收藏xmuSistone......
  • StackOverView又一个Android 5.0 任务管理器控件。这次这个比上次那个(MaterialRecents
    StackOverView视图布局(ViewLayout)★★★★★2015-09-2120:21147kbAndroidStudio1075次165次https://github.com/Bossyao168/StackOverView卡片,任务下载 收藏 Bossyao168 / StackOverViewacustomwidgetofandroid,liketaskmanagerofandr......
  • 禁止app左右按钮滑动
     onBackPress(options){this.backButtonPress++; if(this.backButtonPress>1){ plus.runtime.quit(); }else{ plus.nativeUI.toast('再按一次退出应用'); } setTimeout(function(){ this.backButtonPress=0; },1000); returntrue; ......
  • 判断移动端手指滑动
    话不多说codetimeconsttarget=document.getElementById('sidebar-open');letstartX,startY;functionhandleStart(e){startX=e.touches[0].clientX||e.clientX;startY=e.touches[0].clientY||e.clientY;}functionhandleEnd(e){const......