diff --git a/.gitignore b/.gitignore index 259148fa18f9fb7ef58563f4ff15fc7b172339fb..d333e34ee0e5217d1e51f54114ccb7da48f4565a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,8 @@ *.exe *.out *.app +info.plist + +#patch +*.patch +*.dff \ No newline at end of file diff --git a/README.md b/README.md index 46e56744aadc284d9f12caca11c085ab64391ad9..8402c5fdbd8a3573f61d4e858a5b201c0fb74610 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # leetcode -## 题目 +## BFS +[815. 公交路线](https://leetcode-cn.com/problems/bus-routes/) \ No newline at end of file diff --git a/include/stl.h b/include/stl.h new file mode 100644 index 0000000000000000000000000000000000000000..0d21a8ef98c1782ff1fd607613afb7e568d659b1 --- /dev/null +++ b/include/stl.h @@ -0,0 +1,3 @@ +#pragma once +#include "virtualio.h" +using namespace std; diff --git a/include/virtualio.h b/include/virtualio.h new file mode 100644 index 0000000000000000000000000000000000000000..ad61c12494401cfc4011884c09600f6eb62eae2a --- /dev/null +++ b/include/virtualio.h @@ -0,0 +1,75 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define print(...) print_any(__VA_ARGS__); + +class VOut{ +public: + virtual std::string str() {return "";} +}; + +template +class OStream : public VOut { +public: + ostream_t out; + ostream_t *po; + OStream() { po = &out; } + OStream(ostream_t o) : out(o){ po = &out; } + OStream(ostream_t * o) : po(o){} + OStream& operator<<(const char*t){(*po)< OStream& operator<<(const T&t){(*po)<< t; return *this;} + template OStream& operator<<(const T*t){(*po)<<*t; return *this;} + template OStream& operator<<(const std::vector &v){ + return Out(v.begin(), v.end()); + } + template OStream&operator<<(const std::list &v){ + return Out(v.begin(), v.end()); + } + template OStream& operator<<(const std::set &v){ + return Out(v.begin(), v.end()); + } + template OStream& operator<<(const std::pair & pair){ + (*this) << pair.first; + (*po) << '='; + (*this) << pair.second; + return *this; + } + template OStream&operator<<(const std::unordered_map &m){ + return Out(m.begin(), m.end()); + } + template OStream&operator<<(const std::map &m){ + return Out(m.begin(), m.end()); + } +protected: + template + OStream& Out(iterator src, iterator dst){ + auto p = src; + if(p != dst) {(*po)<< '['; (*this)<<*p;} + while(++p!=dst){(*po)<<", "; (*this)<<*p;} + if(p != src) (*po)<< ']'; + return *this; + } +}; + +class StrStream : public OStream +{ +public: + virtual std::string str() override{ + return out.str(); + } +}; + +template +inline void print_any(const Any ...any){ + StrStream ss; + std::initializer_list{(ss << any, true)...}.size(); + std::cout << ss.str(); +} \ No newline at end of file diff --git "a/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/main.cpp" "b/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/main.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..675fbdd5f9d96fe827ec798629dad8e283a94648 --- /dev/null +++ "b/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/main.cpp" @@ -0,0 +1,13 @@ +#include "stl.h" +#include "solution.h" + +int main(int argc, char *argv[]){ + vector> routes = { + {1,2,7}, + {3,6,7}, + }; + int source = 1, target = 6; + int result = Solution().numBusesToDestination(routes, source, target); + print("result=", result); + return 0; +} \ No newline at end of file diff --git "a/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/solution.h" "b/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/solution.h" new file mode 100644 index 0000000000000000000000000000000000000000..c9353a7d25854e4f1a760b95cb6e033bda216c55 --- /dev/null +++ "b/questions/815. \345\205\254\344\272\244\350\267\257\347\272\277/solution.h" @@ -0,0 +1,67 @@ +// #define print(...) +#define map_t unordered_map +struct SearchInfo{ + // 搜索过的线路 + list ln; + // 到达每条线所需换乘次数 + map_t jmp; +}; +class Solution { + SearchInfo infos[2]; + // 各条线上的站点 + std::vector> l2p; + // 各个站点可换乘的线路 + map_t> p2l; +public: + int numBusesToDestination(vector>& routes, int source, int target) { + print(routes, "\n") + if(source == target) return 0; + // init map + l2p.reserve(routes.size()); + l2p.resize(routes.size()); + for(int l=0; lln.size() && back->ln.size()){ + if(forward->ln.size() > back->ln.size()) + swap(forward, back); + print("q[", back-infos, "] = ", back->ln, "\n") + int jump = bfs(*forward, back->jmp); + if( jump > 0 ) return jump; + } + return -1; + } + int bfs(SearchInfo & forward, map_t & backJmp){ + print("q[", &forward-infos, "] = ", forward.ln, "\n") + int ln = forward.ln.front(); forward.ln.pop_front(); + int jmp = forward.jmp[ln]; + print("m[", &forward-infos, "] = ", forward.jmp,"\n") + print("m[",infos-&forward+1,"] = ", backJmp,"\n") + for(auto &p : l2p[ln]){ + print(p, ":->", p2l[p], "\n") + for(auto &l : p2l[p]){ + if(l == ln || forward.jmp.count(l)) continue; + if(backJmp.count(l)) return jmp + backJmp[l]; + forward.ln.push_back(l); + forward.jmp[l] = jmp + 1; + } + } + return -1; + } +}; \ No newline at end of file