日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

九宫重排_康拓展开_bfs

發(fā)布時(shí)間:2024/9/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 九宫重排_康拓展开_bfs 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
歷屆試題 九宮重排 ? 時(shí)間限制:1.0s ? 內(nèi)存限制:256.0MB 問題描述 如下面第一個(gè)圖的九宮格中,放著 1~8 的數(shù)字卡片,還有一個(gè)格子空著。與空格子相鄰的格子中的卡片可以移動(dòng)到空格中。經(jīng)過若干次移動(dòng),可以形成第二個(gè)圖所示的局面。

?


  我們把第一個(gè)圖的局面記為:12345678.
  把第二個(gè)圖的局面記為:123.46758
  顯然是按從上到下,從左到右的順序記錄數(shù)字,空格記為句點(diǎn)。
  本題目的任務(wù)是已知九宮的初態(tài)和終態(tài),求最少經(jīng)過多少步的移動(dòng)可以到達(dá)。如果無論多少步都無法到達(dá),則輸出-1。 輸入格式 輸入第一行包含九宮的初態(tài),第二行包含九宮的終態(tài)。 輸出格式 輸出最少的步數(shù),如果不存在方案,則輸出-1。 樣例輸入 12345678.
123.46758 樣例輸出 3 樣例輸入 13524678.
46758123. 樣例輸出 22 拿到這個(gè)題的時(shí)候沒什么思路,之前也做過bfs的題,最大的問題在于狀態(tài)的保存,后來看到有個(gè)編碼題用到了康拓展開,豁然開朗,數(shù)學(xué)真的是奇妙啊!還有很多不知道的東西呢! 又知道了java的幾個(gè)坑,哪怕是數(shù)組直接賦值也是賦的地址,添加到鏈表之類的也是地址,想要不受干擾只能手工拷貝。 1 package study_code; 2 3 import java.util.LinkedList; 4 import java.util.Queue; 5 import java.util.Scanner; 6 7 public class 八數(shù)碼 { 8 9 static int[] vis = new int [900900]; //判斷哈希值是否被訪問 10 static int[] f = new int[9]; //保存階乘 11 static int end; //最終狀態(tài)的哈希值 12 static node start = new node(); 13 static String resPath; 14 static int[][] dir = {{-1,0},{1,0},{0,-1},{0,1}}; 15 static char[] dirc = {'U','D','L','R'}; 16 static class node{ 17 int loc; //記錄當(dāng)前空格位置 用一個(gè)數(shù)字記錄 1-9 18 int s[]; //記錄當(dāng)前所有數(shù)字的位置 19 int hashVal; //當(dāng)前狀態(tài)的哈希值 20 String path = ""; //記錄當(dāng)前路徑 21 public node() { 22 } 23 } 24 25 public static void fac() { 26 f[0] = 1; 27 for (int i = 1; i < f.length; i++) { 28 f[i] = f[i-1] * i; 29 } 30 } 31 32 //康拓展開 33 public static int getHash(int[] s) { 34 int res = 0; 35 for (int i = 0; i < s.length; i++) { 36 int index =0; 37 for (int j = i+1; j < s.length; j++) { 38 if (s[j]<s[i]) { 39 index++; 40 } 41 } 42 res += index*f[9-1-i]; 43 } 44 return res; 45 } 46 47 48 public static boolean bfs() { 49 Queue<node> q = new LinkedList<node>(); 50 q.offer(start); 51 vis[start.hashVal] = 1; 52 while (!q.isEmpty()) { 53 node cur = q.poll(); 54 vis[cur.hashVal] = 1; 55 if (cur.hashVal == end) { 56 resPath = cur.path; 57 return true; 58 } 59 int x =cur.loc/3; 60 int y =cur.loc%3; 61 for (int i = 0; i < 4; i++) { 62 int tx = x + dir[i][0]; 63 int ty = y + dir[i][1]; 64 if (tx<0||ty<0||tx>=3||ty>=3) { 65 continue; 66 } 67 node temp = new node(); 68 temp.loc = tx*3+ty; 69 temp.path = cur.path+dirc[i]; 70 temp.s = cur.s.clone(); 71 //交換空格的位置 72 temp.s[cur.loc] = temp.s[temp.loc]; 73 temp.s[temp.loc] = 0; 74 75 temp.hashVal = getHash(temp.s); 76 if (vis[temp.hashVal] == 1) { 77 continue; 78 } 79 q.offer(temp); 80 vis[temp.hashVal] = 1; 81 } 82 } 83 return false; 84 85 86 87 } 88 public static void main(String[] args) { 89 Scanner sc = new Scanner(System.in); 90 fac(); 91 String t = sc.nextLine(); 92 char[] c = t.toCharArray(); 93 int[] n = new int[c.length]; 94 for (int i = 0; i < c.length; i++) { 95 if (c[i] == '.') { 96 n[i] = 0; 97 start.loc = i; 98 }else { 99 n[i] = c[i] - '0'; 100 } 101 } 102 start.s = n.clone(); 103 start.hashVal = getHash(start.s); 104 t = sc.nextLine(); 105 c = t.toCharArray(); 106 n = new int[c.length]; 107 for (int i = 0; i < c.length; i++) { 108 if (c[i] == '.') { 109 n[i] = 0; 110 }else { 111 n[i] = c[i] - '0'; 112 } 113 } 114 end = getHash(n); 115 bfs(); 116 System.out.println(resPath.length()); 117 } 118 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/16crow/p/6641639.html

總結(jié)

以上是生活随笔為你收集整理的九宫重排_康拓展开_bfs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。