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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sicily 1150. 简单魔板

發布時間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sicily 1150. 简单魔板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ?水題一條,但可以通過此題學習寬搜或深搜,然后通過和1151的比較,學習搜索中剪枝的作用。

? ?寬搜中可以在遞歸的一開始就先檢測是否搜到目標值(如下面貼上來的代碼),也可以在將候選節點入隊時檢查。但如果是在入隊時檢查的話記得要注意在初始節點入隊時檢查初始節點是否就已經是目標值

? ?一開始犯的錯誤就是紅色字體標出的錯誤。因此自己寫多幾個測試用例測試自己的代碼,全部測試用例如下:

4

5 8 7 6

4 1 2 3

3

8 7 6 5

1 2 3 4

0

1 2 3 4

8 7 6 5

1

1 7 2 4

8 6 3 5

2

1 6 7 4

8 3 2 5

1

1 6 7 4

8 3 2 5

-1


2 AB

1 A

0 // 邊界測試

1 C // 邊界測試

2 CC // 邊界測試

? ? ? ?


? ?以下為代碼:

#include <iostream> #include <queue> using namespace std; struct Node{int a;string path; }; int toSingleNum(int a[]) {int i;int n = 0;int pow = 1;for( i = 7; i >= 0; i-- ) {n+=a[i]*pow;pow*=10;}return n; } int A(int a) {int m = a/10000;int n = a%10000;return n*10000 + m; } int B( int a) {int n3 = a/100000; // 123int n4 = a/10000; // 1234int n44 = n4-n3*10; // 4int n88 = a%10; // 5int n58 = a%10000; // 8765int n57 = (n58 - n88)/10; // 876return int(n44*1e7 + n3*1e4 + n88 * 1e3 + n57); } int C( int c) {int i, temp;int a[8];int pow = int(1e7);for( i = 0; i < 8; i++ ) {a[i] = c/pow;c = c%pow;pow /= 10;}temp = a[1];a[1] = a[5];a[5] = a[6];a[6] = a[2];a[2] = temp;return toSingleNum(a); } Node bfs(int M, int goal) {Node u,v;queue<Node> q;u.a = 12348765;u.path = "";if( u.a == goal )return u;q.push(u);while( !q.empty() ) {u = q.front();q.pop();if( u.a == goal )return u;if( u.path.size() > M ) {v.a = 0;v.path = "";return v;}v.a = A(u.a);v.path = u.path + "A";q.push(v);v.a = B(u.a);v.path = u.path + "B";q.push(v);v.a = C(u.a);v.path = u.path + "C";q.push(v);} } int main() {int i;int a[8] = { 1, 2, 3, 4 , 8, 7, 6, 5 };int b[8];char path[10];int M;//cout << tosinglenum(a) << endl;//cout << 1e4 << endl;//print(a(tosinglenum(a)));//print(b(tosinglenum(a)));//print(c(tosinglenum(a)));//system("pause");cin >> M;while( M != -1 ) {for( i = 0; i < 4; i++ )cin >> b[i];for ( i = 4; i < 8; i++ )cin >> b[i];Node s = bfs(M, toSingleNum(b));if( s.a == 0 ) {cout << "-1" << endl;}else{cout << s.path.size() << " " << s.path << endl;}cin >> M;}return 0; }


轉載于:https://blog.51cto.com/xuewei/1351312

總結

以上是生活随笔為你收集整理的sicily 1150. 简单魔板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。