Sicily 1150 簡單魔板(BFS)

2022-04-14 17:10:58 字數 1483 閱讀 6841

此題可以使用bfs進行解答,使用8位的十進位制數來儲存魔板的狀態,用bfs進行搜尋即可

1 #include 2

using

namespace

std;34

int op_a(int n) 910

int op_b(int n)

1920

int op_c(int n)

26int ans = a[0] * 10000000 +

27 a[5] * 1000000 +

28 a[1] * 100000 +

29 a[3] * 10000 +

30 a[4] * 1000 +

31 a[6] * 100 +

32 a[2] * 10 +

33 a[7

];34

return

ans;35}

36struct

node ;

40 node bfs(int step, int

n) 51

//依次進行三種操作

52 node tmp1 =front;

53 tmp1.num =op_a(front.num);

54 tmp1.path.push_back('a'

);55

if (tmp1.num ==n)

58else

6162 node tmp2 =front;

63 tmp2.num =op_b(front.num);

64 tmp2.path.push_back('b'

);65

if (tmp2.num ==n)

68else

7172 node t*** =front;

73 t***.num =op_c(front.num);

74 t***.path.push_back('c'

);75

if (t***.num ==n)

78else

8182}83

}84intmain()

93 node node =bfs(step, ans);

94if (node.path.size() > step) cout << "-1"

<< endl;//

如失敗則輸出-1,否則輸出path

95else

100 cout <

101}

102103

}104

return0;

105 }

1150 尋找最大數

尋找最大數 time limit 1000ms memory limit 65536k total submit 64 accepted 31 description 請在整數 n 中刪除m個數字,使得餘下的數字按原次序組成的新數最大,比如當n 92081346718538,m 10時,則新的最大數...

BZOJ 1150 資料備份

1150 ctsc2007 資料備份backup time limit 10 sec memory limit 162 mb submit 1480 solved 592 submit status discuss description 你在一家 it 公司為大型寫字樓或辦公樓 offices 的...

50道演算法題(11 50)

給定乙個只包括 的字串,判斷字串是否有效。有效字串需滿足 左括號必須用相同型別的右括號閉合。左括號必須以正確的順序閉合。注意空字串可被認為是有效字串。示例 1 輸入 輸出 true 示例 2 輸入 輸出 true 示例 3 輸入 輸出 false 示例 4 輸入 輸出 false 示例 5 輸入 輸...