作者qazwsx879345 (Rajon Rondo)
看板C_and_CPP
標題[問題] DFS -窮舉
時間Tue Aug 7 00:34:58 2018
開發平台(Platform): (Ex: Win10, Linux, ...)
Win 10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
c++
問題(Question):
想要使用dfs 來窮舉排列組合
但因為之後會加入一些函式判斷
可能再某一層就不繼續嘗試
ex: 0 1 2 3
到0 1 就判斷不可能繼續執行 就不繼續作了
--
因此資料量大
不想使用#include<algorithm>
裡面的next_permutation
那種窮舉的寫法
ex:
https://blog.csdn.net/liuxingbusi/article/details/53556564
餵入的資料(Input):
(0,1,2)
預期的正確結果(Expected Output):
(0,1,2)
(0,2,1)
(1,0,2)
(1,2,0)
(2,1,0)
(2,0,1)
錯誤結果(Wrong Output):
compile error
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
https://ideone.com/zEv3pe
補充說明(Supplement):
網路上查過很多方法
但都是直接cout把它印出來
ex:
https://goo.gl/tq7BqV
而我是想要的是
輸入一個vector<int> ex:0,1,2
將各種可能組合存入一個vector<vector<int>>以做後續的使用
但因為前陣子使用python較多
可能對於一些函式的使用還有call by reference之類的概念有的不好QQ
卡關整晚 有點崩潰 因此上來求救QQ
想請問該怎麼寫會比較好呢QQ
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.8.133.181
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1533573303.A.D51.html
1F:推 idiont: 你給的Ideone選到java了 改成C++後只有第46行CE 08/07 01:17
2F:→ idiont: 你在generate_path裡面宣告的path 要給dfs用 必須要在dfs 08/07 01:18
3F:→ idiont: 的path參數前面加上& 代表reference 08/07 01:18
4F:→ idiont: 第33行直接用[]存取vector 在vector還沒那麼大的時候會發 08/07 01:20
5F:→ idiont: 生越界存取 08/07 01:20
6F:→ idiont: vector可以用resize函式直接改變成你要的大小 08/07 01:22
7F:→ idiont: 第25行 C++沒辦法直接cout一個vector 請改用迴圈印 08/07 01:27
8F:→ idiont: 還有你的arrangement也採用reference可以減少不必要的複製 08/07 01:28
https://ideone.com/ftGnHc
謝謝大大 改好了!
※ 編輯: qazwsx879345 (39.8.133.181), 08/07/2018 07:51:30