自己實現的一個尋徑演算法的架構,具體演算法採用動態庫模式,可以非常方便靈活地嘗試多種尋徑演算法

來源:互聯網
上載者:User
/**<br /> * 尋徑演算法架構<br /> * by lxslove<br /> * e-mail:moodlxs@163.com<br /> * 用法:<br /> * 注意:編譯本架構需要加上-ldl選項<br /> * g++ -ldl src.cpp<br /> *<br /> * 地圖檔案格式為不同符號的矩陣,具體什麼符號代表可通行,什麼符號代表不可通行,由自己決定。舉個例子:<br /> * ###################<br /> * # ###### ########<br /> * # ###### ##########<br /> * # ##########<br /> * ######## # ####<br /> * ## #### ## ######<br /> * ### # ######### ##<br /> * ###### ##### # # ##<br /> * #### # ##### ######<br /> * ###### #######<br /> * ###################<br /> * 以上的例子,假設空格是可通行的,#是不可通行的。<br /> *<br /> * 具體的演算法採用外掛程式的形式,外掛程式(so檔案)需要提供find_path函數介面,該函數的形式為:<br /> * extern "C"<br /> * int find_path(size_t start_pos, size_t end_pos, vector<char>* map_data, vector<char>* path_list)<br /> * {<br /> * return 0;//成功則返回0<br /> * }<br /> * 編譯so檔案的方式例子如下:<br /> * g++ -fPIC -shared -o 1.so 1.cpp 1.h<br /> *<br /> * start_pos/end_pos為地圖資料在map_data中的位置,path_list暫時沒用到<br /> * 在得到路徑時,需要在find_path介面方法中修改map_data的符號,標誌其為路徑,具體修改為什麼符號由自己決定,舉個例子:<br /> * ###################<br /> * #@######@ ########<br /> * #@######@##########<br /> * #@@@@@@@@##########<br /> * ######## # ####<br /> * ## #### ## ######<br /> * ### # ######### ##<br /> * ###### ##### # # ##<br /> * #### # ##### ######<br /> * ###### #######<br /> * ###################<br /> * 以上例子中,將相應位置的符號修改為@<br /> * 架構會直接顯示地圖資料,是什麼符號就顯示什麼符號。<br /> * */</p><p>#define DL cout<br />#include <stdio.h><br />#include <string.h><br />#include <libgen.h><br />#include <dlfcn.h><br />#include <sys/time.h><br />#include <string><br />#include <fstream><br />#include <iostream><br />#include <vector></p><p>using namespace std;</p><p>/// 外掛程式介面<br />// 參數為:<br />// 開始位置,結束位置,地圖資料列表, 路徑列表,暫時不用,固定為NULL<br />typedef int (*pfunc)(size_t, size_t, vector<char>*, vector<char>*);</p><p>const char* const FUNC_NAME = "find_path";</p><p>class XJFrame<br />{<br />private:<br />string m_map_path;/// 地圖位置<br />pfuncp_func;/// 外掛程式的介面函數<br />vector<char> m_map_data;/// 地圖資料指標<br />vector<size_t>* m_p_path_data;/// 路徑資料指標<br />size_t m_map_width;/// 地圖寬度<br />size_tm_map_heigh;/// 地圖高度<br />void *m_p_module;/// 模組指標<br />struct timeval m_use_time;/// 演算法耗時</p><p>public:<br />size_t get_map_width()<br />{<br />return m_map_width;<br />}</p><p>size_t get_map_heigh()<br />{<br />return m_map_heigh;<br />}</p><p>const struct timeval* get_use_time()<br />{<br />return &m_use_time;<br />}</p><p>private:<br />XJFrame(const XJFrame& rv);<br />XJFrame& operator=(const XJFrame& rv);</p><p>public:<br />XJFrame()<br />:m_map_width(0), m_map_heigh(0), m_p_module(NULL), m_p_path_data(NULL)<br />{<br />}</p><p>XJFrame(const string& map_path)<br />:m_map_path(map_path), m_map_width(0), m_map_heigh(0), m_p_module(NULL), m_p_path_data(NULL)</p><p>{<br />}</p><p>~XJFrame()<br />{<br />if (m_p_module)<br />{<br />dlclose(m_p_module);<br />m_p_module = NULL;<br />}<br />if (m_p_path_data)<br />{<br />delete m_p_path_data;<br />m_p_path_data = NULL;<br />}<br />}</p><p>void set_map_path(const string & map_path)<br />{<br />m_map_path = map_path;<br />}</p><p>int open_module(const string & module_path)<br />{<br />if (module_path.empty())<br />{<br />DL << "module_path not setting!\n";<br />return -1;<br />}</p><p>if (m_p_module)<br />{<br />dlclose(m_p_module);<br />m_p_module = NULL;<br />}</p><p>void* m_p_module = dlopen(module_path.c_str(), RTLD_LAZY );<br />if( !m_p_module )<br />{<br />DL << "can't open module: " << module_path <<", error:" << dlerror() << "\n";<br />return -1;<br />}<br />p_func = (pfunc)dlsym(m_p_module, FUNC_NAME);<br />const char *dlsym_error = dlerror();<br />if (dlsym_error)<br />{<br />DL << "load symbol: " << FUNC_NAME << "! err: " << dlsym_error << "\n";<br />return -1;<br />}</p><p>return 0;<br />}</p><p>int init_map()<br />{<br />if (m_map_path.empty())<br />{<br />DL << "map_path not setting!\n";<br />return -1;<br />}</p><p>m_map_data.clear();</p><p>ifstream in_file(m_map_path.c_str());<br />if (!in_file)<br />{<br />DL << "open file error:" << m_map_path << "\n";<br />return -1;<br />}</p><p>size_t len;<br />char buffer[1024];<br />while (!in_file.eof())<br />{<br />in_file.getline(buffer, sizeof(buffer));<br />len = strlen(buffer);<br />if (len == 0)<br />{<br />break;<br />}</p><p>//根據第一次的寬度計算地圖寬度<br />if (!m_map_width)<br />{<br />m_map_width = len;<br />}<br />else if (m_map_width != len)<br />{<br />DL << "map format error!0\n";<br />return -1;<br />}</p><p>//計算地圖高度<br />++m_map_heigh;</p><p>//讀取地圖資訊<br />for (size_t i = 0; i < len; ++i)<br />{<br />if ((*(buffer + i)) != ' ' && (*(buffer + i)) != '#')<br />{<br />DL << "map format error!2\n";<br />return -1;<br />}<br />else<br />{<br />m_map_data.push_back(*(buffer + i));<br />}<br />}<br />}</p><p>in_file.close();</p><p>return 0;<br />}</p><p>void display_map()<br />{<br />if (m_map_data.empty())<br />{<br />DL << "Please load map before!" << endl;<br />return ;<br />}</p><p>DL << "Map Info: width:" << m_map_width << ", heigh:" << m_map_heigh << endl;<br />for (int i = 0; i < m_map_data.size(); ++i)<br />{<br />DL << m_map_data[i];<br />if ((i + 1) % m_map_width == 0)<br />{<br />DL << "\n";<br />}<br />}<br />}</p><p>int find_path(size_t start_pos, size_t end_pos)<br />{<br />if (m_map_data.empty())<br />{<br />DL << "Please load map before!" << endl;<br />return -1;<br />}<br />struct timeval tvs, tve;<br />gettimeofday(&tvs, NULL);<br />int ret = p_func(start_pos, end_pos, &m_map_data, NULL);<br />gettimeofday(&tve, NULL);<br />if (tve.tv_usec < tvs.tv_usec)<br />{<br />--tve.tv_sec;<br />tve.tv_usec+=1000;<br />}<br />m_use_time.tv_sec = tve.tv_sec - tvs.tv_sec;<br />m_use_time.tv_usec = tve.tv_usec - tvs.tv_usec;<br />}<br />};</p><p>int main(int argc, char** argv)<br />{<br />XJFrame xjframe;<br />int user_select;<br />bool is_exit = false;</p><p>if (argc == 3)<br />{<br />xjframe.set_map_path(argv[1]);<br />xjframe.init_map();<br />xjframe.open_module(argv[2]);<br />xjframe.display_map();<br />}<br />do<br />{<br />cout << "MENU:\nSetMap(1)\tSetModule(2)\tDisplayMap(3)\tFindPath(4)\tExit(0)" << endl;<br />cout << "Input:" << flush;<br />while (!(cin >> user_select))<br />{<br />cout <<"Input error, retry:"<< flush;<br />cin.clear();<br />cin.ignore(10000,'\n');<br />}</p><p>string str;<br />int ret;<br />switch (user_select)<br />{<br />case 1:<br />cout << "Your Map Path:";<br />cin >> str;<br />xjframe.set_map_path(str);<br />ret = xjframe.init_map();<br />if (ret != 0)<br />{<br />cout << "Set Map Error" << endl;<br />}<br />else<br />{<br />cout << "Set Map Success" << endl;<br />}</p><p>break;</p><p>case 2:<br />cout << "Your Module Path:";<br />cin >> str;<br />ret = xjframe.open_module(str);<br />if (ret != 0)<br />{<br />cout << "Set Module Error" << endl;<br />}<br />else<br />{<br />cout << "Set Module Success" << endl;<br />}</p><p>break;</p><p>case 3:<br />xjframe.display_map();<br />break;</p><p>case 4:<br />cout << "Enter the start position and end position,format:x y x y";<br />{<br />int sx,sy,ex,ey;<br />if (!(cin >> sx >> sy >> ex >> ey))<br />{<br />cout << "Input Error!" << endl;<br />break;<br />}<br />else<br />{<br />int start_pos = sy * xjframe.get_map_width() + sx;<br />int end_pos = ey * xjframe.get_map_width() + ex;</p><p>ret = xjframe.find_path(start_pos, end_pos);<br />if (ret != 0)<br />{<br />cout << "can not find any path!" << endl;<br />}<br />else<br />{<br />cout << "path find!" << endl;<br />xjframe.display_map();<br />}<br />cout << "Use Time:" << xjframe.get_use_time()->tv_sec << " second " << xjframe.get_use_time()->tv_usec << " microsecond" << endl;<br />}<br />}</p><p>break;</p><p>case 0:<br />is_exit = true;<br />break;<br />default:<br />break;<br />}<br />cin.clear();<br />cin.ignore(10000,'\n');<br />}while (!is_exit);<br />cout << "byebye" << endl;</p><p>return 0;<br />}</p><p>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.