[問題] DFS -窮舉

看板C_and_CPP (C/C++)作者 (Rajon Rondo)時間7年前 (2018/08/07 00:35), 7年前編輯推噓1(107)
留言8則, 1人參與, 7年前最新討論串1/1
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1533573303.A.D51.html

08/07 01:17, 7年前 , 1F
你給的Ideone選到java了 改成C++後只有第46行CE
08/07 01:17, 1F

08/07 01:18, 7年前 , 2F
你在generate_path裡面宣告的path 要給dfs用 必須要在dfs
08/07 01:18, 2F

08/07 01:18, 7年前 , 3F
的path參數前面加上& 代表reference
08/07 01:18, 3F

08/07 01:20, 7年前 , 4F
第33行直接用[]存取vector 在vector還沒那麼大的時候會發
08/07 01:20, 4F

08/07 01:20, 7年前 , 5F
生越界存取
08/07 01:20, 5F

08/07 01:22, 7年前 , 6F
vector可以用resize函式直接改變成你要的大小
08/07 01:22, 6F

08/07 01:27, 7年前 , 7F
第25行 C++沒辦法直接cout一個vector 請改用迴圈印
08/07 01:27, 7F

08/07 01:28, 7年前 , 8F
還有你的arrangement也採用reference可以減少不必要的複製
08/07 01:28, 8F
https://ideone.com/ftGnHc 謝謝大大 改好了! ※ 編輯: qazwsx879345 (39.8.133.181), 08/07/2018 07:51:30
文章代碼(AID): #1RQ7YtrH (C_and_CPP)
文章代碼(AID): #1RQ7YtrH (C_and_CPP)