|
|
package class_2022_07_4_week;
|
|
|
|
|
|
// 在一个 2 * 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示,
|
|
|
// 以及一块空缺用 0 来表示。一次 移动 定义为选择 0 与一个相邻的数字(上下左右)进行交换.
|
|
|
// 最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。
|
|
|
// 给出一个谜板的初始状态 board ,
|
|
|
// 返回最少可以通过多少次移动解开谜板,如果不能解开谜板,则返回 -1 。
|
|
|
// 测试链接 : https://leetcode.cn/problems/sliding-puzzle/
|
|
|
public class Code02_SidingPuzzle2 {
|
|
|
|
|
|
public static int[] status = { 12345, 12354, 12435, 12453, 12534, 12543, 13245, 13254, 13425, 13452, 13524, 13542,
|
|
|
14235, 14253, 14325, 14352, 14523, 14532, 15234, 15243, 15324, 15342, 15423, 15432, 21345, 21354, 21435,
|
|
|
21453, 21534, 21543, 23145, 23154, 23415, 23451, 23514, 23541, 24135, 24153, 24315, 24351, 24513, 24531,
|
|
|
25134, 25143, 25314, 25341, 25413, 25431, 31245, 31254, 31425, 31452, 31524, 31542, 32145, 32154, 32415,
|
|
|
32451, 32514, 32541, 34125, 34152, 34215, 34251, 34512, 34521, 35124, 35142, 35214, 35241, 35412, 35421,
|
|
|
41235, 41253, 41325, 41352, 41523, 41532, 42135, 42153, 42315, 42351, 42513, 42531, 43125, 43152, 43215,
|
|
|
43251, 43512, 43521, 45123, 45132, 45213, 45231, 45312, 45321, 51234, 51243, 51324, 51342, 51423, 51432,
|
|
|
52134, 52143, 52314, 52341, 52413, 52431, 53124, 53142, 53214, 53241, 53412, 53421, 54123, 54132, 54213,
|
|
|
54231, 54312, 54321, 102345, 102354, 102435, 102453, 102534, 102543, 103245, 103254, 103425, 103452, 103524,
|
|
|
103542, 104235, 104253, 104325, 104352, 104523, 104532, 105234, 105243, 105324, 105342, 105423, 105432,
|
|
|
120345, 120354, 120435, 120453, 120534, 120543, 123045, 123054, 123405, 123450, 123504, 123540, 124035,
|
|
|
124053, 124305, 124350, 124503, 124530, 125034, 125043, 125304, 125340, 125403, 125430, 130245, 130254,
|
|
|
130425, 130452, 130524, 130542, 132045, 132054, 132405, 132450, 132504, 132540, 134025, 134052, 134205,
|
|
|
134250, 134502, 134520, 135024, 135042, 135204, 135240, 135402, 135420, 140235, 140253, 140325, 140352,
|
|
|
140523, 140532, 142035, 142053, 142305, 142350, 142503, 142530, 143025, 143052, 143205, 143250, 143502,
|
|
|
143520, 145023, 145032, 145203, 145230, 145302, 145320, 150234, 150243, 150324, 150342, 150423, 150432,
|
|
|
152034, 152043, 152304, 152340, 152403, 152430, 153024, 153042, 153204, 153240, 153402, 153420, 154023,
|
|
|
154032, 154203, 154230, 154302, 154320, 201345, 201354, 201435, 201453, 201534, 201543, 203145, 203154,
|
|
|
203415, 203451, 203514, 203541, 204135, 204153, 204315, 204351, 204513, 204531, 205134, 205143, 205314,
|
|
|
205341, 205413, 205431, 210345, 210354, 210435, 210453, 210534, 210543, 213045, 213054, 213405, 213450,
|
|
|
213504, 213540, 214035, 214053, 214305, 214350, 214503, 214530, 215034, 215043, 215304, 215340, 215403,
|
|
|
215430, 230145, 230154, 230415, 230451, 230514, 230541, 231045, 231054, 231405, 231450, 231504, 231540,
|
|
|
234015, 234051, 234105, 234150, 234501, 234510, 235014, 235041, 235104, 235140, 235401, 235410, 240135,
|
|
|
240153, 240315, 240351, 240513, 240531, 241035, 241053, 241305, 241350, 241503, 241530, 243015, 243051,
|
|
|
243105, 243150, 243501, 243510, 245013, 245031, 245103, 245130, 245301, 245310, 250134, 250143, 250314,
|
|
|
250341, 250413, 250431, 251034, 251043, 251304, 251340, 251403, 251430, 253014, 253041, 253104, 253140,
|
|
|
253401, 253410, 254013, 254031, 254103, 254130, 254301, 254310, 301245, 301254, 301425, 301452, 301524,
|
|
|
301542, 302145, 302154, 302415, 302451, 302514, 302541, 304125, 304152, 304215, 304251, 304512, 304521,
|
|
|
305124, 305142, 305214, 305241, 305412, 305421, 310245, 310254, 310425, 310452, 310524, 310542, 312045,
|
|
|
312054, 312405, 312450, 312504, 312540, 314025, 314052, 314205, 314250, 314502, 314520, 315024, 315042,
|
|
|
315204, 315240, 315402, 315420, 320145, 320154, 320415, 320451, 320514, 320541, 321045, 321054, 321405,
|
|
|
321450, 321504, 321540, 324015, 324051, 324105, 324150, 324501, 324510, 325014, 325041, 325104, 325140,
|
|
|
325401, 325410, 340125, 340152, 340215, 340251, 340512, 340521, 341025, 341052, 341205, 341250, 341502,
|
|
|
341520, 342015, 342051, 342105, 342150, 342501, 342510, 345012, 345021, 345102, 345120, 345201, 345210,
|
|
|
350124, 350142, 350214, 350241, 350412, 350421, 351024, 351042, 351204, 351240, 351402, 351420, 352014,
|
|
|
352041, 352104, 352140, 352401, 352410, 354012, 354021, 354102, 354120, 354201, 354210, 401235, 401253,
|
|
|
401325, 401352, 401523, 401532, 402135, 402153, 402315, 402351, 402513, 402531, 403125, 403152, 403215,
|
|
|
403251, 403512, 403521, 405123, 405132, 405213, 405231, 405312, 405321, 410235, 410253, 410325, 410352,
|
|
|
410523, 410532, 412035, 412053, 412305, 412350, 412503, 412530, 413025, 413052, 413205, 413250, 413502,
|
|
|
413520, 415023, 415032, 415203, 415230, 415302, 415320, 420135, 420153, 420315, 420351, 420513, 420531,
|
|
|
421035, 421053, 421305, 421350, 421503, 421530, 423015, 423051, 423105, 423150, 423501, 423510, 425013,
|
|
|
425031, 425103, 425130, 425301, 425310, 430125, 430152, 430215, 430251, 430512, 430521, 431025, 431052,
|
|
|
431205, 431250, 431502, 431520, 432015, 432051, 432105, 432150, 432501, 432510, 435012, 435021, 435102,
|
|
|
435120, 435201, 435210, 450123, 450132, 450213, 450231, 450312, 450321, 451023, 451032, 451203, 451230,
|
|
|
451302, 451320, 452013, 452031, 452103, 452130, 452301, 452310, 453012, 453021, 453102, 453120, 453201,
|
|
|
453210, 501234, 501243, 501324, 501342, 501423, 501432, 502134, 502143, 502314, 502341, 502413, 502431,
|
|
|
503124, 503142, 503214, 503241, 503412, 503421, 504123, 504132, 504213, 504231, 504312, 504321, 510234,
|
|
|
510243, 510324, 510342, 510423, 510432, 512034, 512043, 512304, 512340, 512403, 512430, 513024, 513042,
|
|
|
513204, 513240, 513402, 513420, 514023, 514032, 514203, 514230, 514302, 514320, 520134, 520143, 520314,
|
|
|
520341, 520413, 520431, 521034, 521043, 521304, 521340, 521403, 521430, 523014, 523041, 523104, 523140,
|
|
|
523401, 523410, 524013, 524031, 524103, 524130, 524301, 524310, 530124, 530142, 530214, 530241, 530412,
|
|
|
530421, 531024, 531042, 531204, 531240, 531402, 531420, 532014, 532041, 532104, 532140, 532401, 532410,
|
|
|
534012, 534021, 534102, 534120, 534201, 534210, 540123, 540132, 540213, 540231, 540312, 540321, 541023,
|
|
|
541032, 541203, 541230, 541302, 541320, 542013, 542031, 542103, 542130, 542301, 542310, 543012, 543021,
|
|
|
543102, 543120, 543201, 543210 };
|
|
|
|
|
|
public static int[] ans = { 15, -1, -1, 3, 15, -1, -1, 15, 3, -1, -1, 17, 17, -1, -1, 15, 13, -1, -1, 17, 13, -1,
|
|
|
-1, 7, -1, 21, 15, -1, -1, 13, 3, -1, -1, 11, 11, -1, -1, 9, 15, -1, -1, 15, 9, -1, -1, 15, 17, -1, 15, -1,
|
|
|
-1, 11, 19, -1, -1, 13, 17, -1, -1, 19, 13, -1, -1, 15, 13, -1, -1, 7, 9, -1, -1, 11, -1, 9, 13, -1, -1, 9,
|
|
|
17, -1, -1, 15, 7, -1, -1, 17, 7, -1, -1, 11, 19, -1, -1, 11, 11, -1, 17, -1, -1, 13, 11, -1, -1, 5, 13, -1,
|
|
|
-1, 15, 11, -1, -1, 15, 17, -1, -1, 11, 11, -1, -1, 15, 14, -1, -1, 2, 14, -1, -1, 14, 2, -1, -1, 16, 16,
|
|
|
-1, -1, 14, 12, -1, -1, 16, 12, -1, -1, 6, 13, -1, -1, 1, 13, -1, 2, -1, 1, 0, -1, -1, -1, 10, -1, -1, 11,
|
|
|
12, 10, -1, 11, 12, -1, -1, -1, 15, 3, -1, -1, 17, -1, 14, -1, -1, 15, 16, 14, -1, 15, 16, -1, -1, -1, 6,
|
|
|
-1, -1, 5, 4, 17, -1, -1, 15, 13, -1, 16, -1, 15, 16, -1, -1, -1, 16, -1, -1, 15, 14, 18, -1, 17, 18, -1,
|
|
|
-1, -1, 15, 13, -1, -1, 5, -1, 4, -1, -1, 3, 4, 12, -1, 13, 14, -1, -1, -1, 12, -1, -1, 13, 14, -1, 20, 16,
|
|
|
-1, -1, 12, 4, -1, -1, 12, 12, -1, -1, 8, 16, -1, -1, 14, 8, -1, -1, 16, 16, -1, -1, 19, 17, -1, -1, 13, -1,
|
|
|
14, -1, -1, 13, 14, 18, -1, 17, 18, -1, -1, -1, 18, -1, -1, 17, 18, 5, -1, -1, 13, 13, -1, 16, -1, 15, 14,
|
|
|
-1, -1, -1, 16, -1, -1, 15, 14, 8, -1, 7, 6, -1, -1, -1, 7, 15, -1, -1, 13, -1, 10, -1, -1, 11, 12, 6, -1,
|
|
|
5, 6, -1, -1, -1, 12, -1, -1, 13, 14, 9, -1, -1, 15, 15, -1, 18, -1, 19, 16, -1, -1, -1, 14, -1, -1, 13, 14,
|
|
|
10, -1, 9, 10, -1, -1, 14, -1, -1, 12, 18, -1, -1, 12, 16, -1, -1, 18, 12, -1, -1, 14, 14, -1, -1, 8, 10,
|
|
|
-1, -1, 12, 13, -1, -1, 13, 17, -1, 16, -1, 15, 14, -1, -1, -1, 16, -1, -1, 15, 16, 12, -1, 11, 12, -1, -1,
|
|
|
-1, 13, 15, -1, -1, 19, -1, 20, -1, -1, 19, 20, 14, -1, 13, 14, -1, -1, -1, 14, -1, -1, 13, 14, 11, -1, -1,
|
|
|
15, 15, -1, 14, -1, 15, 16, -1, -1, -1, 16, -1, -1, 17, 16, 10, -1, 9, 10, -1, -1, -1, 9, 11, -1, -1, 13,
|
|
|
-1, 14, -1, -1, 13, 14, 12, -1, 11, 10, -1, -1, -1, 14, -1, -1, 13, 12, -1, 8, 12, -1, -1, 8, 18, -1, -1,
|
|
|
14, 6, -1, -1, 18, 6, -1, -1, 10, 20, -1, -1, 10, 10, -1, -1, 7, 11, -1, -1, 7, -1, 4, -1, -1, 5, 6, 4, -1,
|
|
|
5, 6, -1, -1, -1, 8, -1, -1, 9, 10, 19, -1, -1, 15, 7, -1, 14, -1, 13, 14, -1, -1, -1, 10, -1, -1, 9, 8, 18,
|
|
|
-1, 19, 20, -1, -1, -1, 19, 7, -1, -1, 11, -1, 10, -1, -1, 9, 10, 18, -1, 19, 20, -1, -1, -1, 10, -1, -1, 9,
|
|
|
8, 21, -1, -1, 11, 11, -1, 10, -1, 9, 10, -1, -1, -1, 14, -1, -1, 13, 12, 18, -1, 19, 20, -1, -1, 16, -1,
|
|
|
-1, 12, 12, -1, -1, 6, 14, -1, -1, 16, 10, -1, -1, 14, 16, -1, -1, 10, 12, -1, -1, 14, 15, -1, -1, 13, 13,
|
|
|
-1, 16, -1, 15, 14, -1, -1, -1, 16, -1, -1, 15, 14, 14, -1, 13, 14, -1, -1, -1, 7, 15, -1, -1, 15, -1, 14,
|
|
|
-1, -1, 13, 14, 10, -1, 9, 8, -1, -1, -1, 16, -1, -1, 15, 16, 11, -1, -1, 15, 17, -1, 18, -1, 17, 18, -1,
|
|
|
-1, -1, 18, -1, -1, 17, 18, 12, -1, 11, 12, -1, -1, -1, 9, 13, -1, -1, 13, -1, 10, -1, -1, 11, 12, 8, -1, 7,
|
|
|
8, -1, -1, -1, 12, -1, -1, 13, 14 };
|
|
|
|
|
|
public static int slidingPuzzle(int[][] board) {
|
|
|
int from = board[0][0] * 100000 + board[0][1] * 10000 + board[0][2] * 1000 + board[1][0] * 100
|
|
|
+ board[1][1] * 10 + board[1][2];
|
|
|
return ans[find(from)];
|
|
|
}
|
|
|
|
|
|
public static int find(int num) {
|
|
|
int ans = 0;
|
|
|
int l = 0;
|
|
|
int r = status.length - 1;
|
|
|
int m = 0;
|
|
|
while (l <= r) {
|
|
|
m = (l + r) / 2;
|
|
|
if (status[m] == num) {
|
|
|
ans = m;
|
|
|
break;
|
|
|
} else if (status[m] > num) {
|
|
|
r = m - 1;
|
|
|
} else {
|
|
|
l = m + 1;
|
|
|
}
|
|
|
}
|
|
|
return ans;
|
|
|
}
|
|
|
|
|
|
}
|