leetcode 773. 滑動謎題

題目

在一個 2 x 3 的板上(board)有 5 塊磚瓦,用數字 1~5 來表示, 以及一塊空缺用?0?來表示.

一次移動定義為選擇?0?與一個相鄰的數字(上下左右)進行交換.

最終當板?board?的結果是?[[1,2,3],[4,5,0]]?謎板被解開。

給出一個謎板的初始狀態,返回最少可以通過多少次移動解開謎板,如果不能解開謎板,則返回 -1 。

  • 示例:
輸入:board = [[1,2,3],[4,0,5]]
輸出:1
解釋:交換 0 和 5 ,1 步完成
輸入:board = [[1,2,3],[5,4,0]]
輸出:-1
解釋:沒有辦法完成謎板
輸入:board = [[4,1,2],[5,0,3]]
輸出:5
解釋:
最少完成謎板的最少移動次數是 5 ,
一種移動路徑:
尚未移動: [[4,1,2],[5,0,3]]
移動 1 次: [[4,1,2],[0,5,3]]
移動 2 次: [[0,1,2],[4,5,3]]
移動 3 次: [[1,0,2],[4,5,3]]
移動 4 次: [[1,2,0],[4,5,3]]
移動 5 次: [[1,2,3],[4,5,0]]
輸入:board = [[3,2,4],[1,5,0]]
輸出:14

提示:

  • board?是一個如上所述的 2 x 3 的數組.
  • board[i][j]?是一個?[0, 1, 2, 3, 4, 5]?的排列.

解題思路

  1. 使用字符串代表謎板的狀態
board = [[1,2,3],[4,0,5]]1 2 3
4 0 5使用字符串 123405表示

所以每一個字符串就代表謎板的一個狀態

  1. 廣度優先搜索
    利用隊列實現廣度優先搜索,根據0所在位置,將0和周圍的數字進行交換,從而衍生出若干種情況加入隊列

代碼

class Solution {public void swagg(char[] tar,int x,int y) {char temp=tar[x];tar[x]=tar[y];tar[y]=temp;}public int slidingPuzzle(int[][] board) {StringBuilder stringBuilder=new StringBuilder();for (int i=0;i<board.length;i++)for (int j=0;j<board[0].length;j++){stringBuilder.append(board[i][j]);}Set<String> set=new HashSet<>();Queue<String> queue=new LinkedList<>();queue.add(stringBuilder.toString());int res=0;String s=null;while (!queue.isEmpty()){int cur=queue.size();for (int i=0;i<cur;i++){s = queue.poll();if(s.equals("123450"))  return res;char[] th=s.toCharArray();for (int j=0;j<6;j++){if(th[j]=='0'){int x=j%3,y=j/3;if(y==1){swagg(th,j,x);String temp=new String(th);if(!set.contains(temp))queue.add(temp);set.add(temp);swagg(th,j,x);}else {swagg(th,j,x+3);String temp=new String(th);if(!set.contains(temp))queue.add(temp);set.add(temp);swagg(th,j,x+3);}if(x>0){swagg(th,j,j-1);String temp=new String(th);if(!set.contains(temp))queue.add(temp);set.add(temp);swagg(th,j,j-1);}if(x<2){swagg(th,j,j+1);String temp=new String(th);if(!set.contains(temp))queue.add(temp);set.add(temp);swagg(th,j,j+1);}}}}res++;}return -1;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390586.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390586.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390586.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據科學領域有哪些技術_領域知識在數據科學中到底有多重要?

數據科學領域有哪些技術Jeremie Harris: “In a way, it’s almost like a data scientist or a data analyst has to be like a private investigator more than just a technical person.”杰里米哈里斯(Jeremie Harris) &#xff1a;“ 從某種意義上說&#xff0c;這就像是數…

python 算術運算

1. 算術運算符與優先級 # -*- coding:utf-8 -*-# 運算符含有,-,*,/,**,//,% # ** 表示^ , 也就是次方 a 2 ** 4 print 2 ** 4 , aa 16 / 5 print 16 / 5 , aa 16.0 / 5 print 16.0 / 5 , a# 結果再進行一次floor a 16.0 // 5.0 print 16.0 // 5.0 , aa 16 // 5 print …

c語言編程時碰到取整去不了_碰到編程墻時如何解開

c語言編程時碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.無論身在何處&#xff0c;陷…

初創公司怎么做銷售數據分析_為什么您的初創企業需要數據科學來解決這一危機...

初創公司怎么做銷售數據分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠狀病毒的…

leetcode 909. 蛇梯棋

題目 N x N 的棋盤 board 上&#xff0c;按從 1 到 N*N 的數字給方格編號&#xff0c;編號 從左下角開始&#xff0c;每一行交替方向。 例如&#xff0c;一塊 6 x 6 大小的棋盤&#xff0c;編號如下&#xff1a; r 行 c 列的棋盤&#xff0c;按前述方法編號&#xff0c;棋盤格…

Python基礎之window常見操作

一、window的常見操作&#xff1a; cd c:\ #進入C盤d: #從C盤切換到D盤 cd python #進入目錄cd .. #往上走一層目錄dir #查看目錄文件列表cd ../.. #往上上走一層目錄 二、常見的文件后綴名&#xff1a; .txt 記事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可執行…

WPF效果(GIS三維篇)

二維的GIS已經被我玩爛了&#xff0c;緊接著就是三維了&#xff0c;哈哈&#xff01;先來看看最簡單的效果&#xff1a; 轉載于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注釋_CSS注釋示例–如何注釋CSS

css注釋Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注釋來解釋代碼塊或在開發過程中進行臨時更改。 注釋的代碼不執行。 Both single and multi-line comments in…

r軟件時間序列分析論文_高度比較的時間序列分析-一篇論文評論

r軟件時間序列分析論文數據科學 &#xff0c; 機器學習 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名稱

題目 給你一個整數 columnNumber &#xff0c;返回它在 Excel 表中相對應的列名稱。 例如&#xff1a; A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1&#xff1a; 輸入&#xff1a;columnNumber 1 輸出&#xff1a;“A” 示例 2&…

飛機訂票系統

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班號7 char start_time[10]; //起飛時間8 char end_time[10]; //抵達時間9 char st…

解決Mac10.13 Pod報錯 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升級10.13以后Pod命令失效&#xff0c;解決辦法如下&#xff1a; 終端執行 brew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復 若報錯繼續執行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復…

angular示例_用示例解釋Angular動畫

angular示例為什么要使用動畫&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的網絡抓取電子商務網站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

劍指 Offer 37. 序列化二叉樹

題目 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現二叉樹的序列化與反序列化…

ie8 ajaxSubmit 上傳文件提示下載

轉載 解決ie下ajaxsubmit上傳文件提示下載文件問題 主要是應為放回類型為json&#xff0c;返回text/html轉載于:https://www.cnblogs.com/yang-C-J/p/8963278.html

一個簡單的 js 時間對象創建

JS中獲取時間很常見&#xff0c;湊湊熱鬧&#xff0c;也獲取一個時間對象試試 首先&#xff0c;先了解js的獲取時間函數如下&#xff1a; var myDate new Date(); //創建一個時間對象 myDate.getYear(); // 獲取當前年份&#xff08;2位&#x…

裁判打分_內在的裁判偏見

裁判打分News flash: being an umpire is hard. Their job is to judge whether a ball that’s capable of moving upwards of 100 MPH or breaking 25 inches crossed through an imaginary zone before being caught. I don’t think many would argue that they have it ea…

數據庫sql課程設計_SQL和數據庫-初學者完整課程

數據庫sql課程設計In this course, Mike Dane will teach you database management basics and SQL.在本課程中&#xff0c;Mike Dane將教您數據庫管理基礎知識和SQL。 The course starts off with Mike helping you install MySQL on Windows or Mac. Then he explores topic…

LCP 07. 傳遞信息

小朋友 A 在和 ta 的小伙伴們玩傳信息游戲&#xff0c;游戲規則如下&#xff1a; 有 n 名玩家&#xff0c;所有玩家編號分別為 0 &#xff5e; n-1&#xff0c;其中小朋友 A 的編號為 0 每個玩家都有固定的若干個可傳信息的其他玩家&#xff08;也可能沒有&#xff09;。傳信息…