fork download
  1. #include <vector>
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <utility>
  5. #include <sstream>
  6. #include <algorithm>
  7. #define INF 100000007
  8.  
  9. using namespace std;
  10.  
  11. class PathFinding{
  12. public:
  13. int minTurns(vector<string> board);
  14. };
  15.  
  16. vector<vector<vector<vector<int> > > > DP;
  17. vector<vector<vector<vector<bool> > > > visited;
  18. int AI, AJ, BI, BJ;
  19.  
  20. int dp(int ai, int aj, int bi, int bj, vector<string> &board){
  21. int m = board.size();
  22. int n = board[0].size();
  23. if(ai < 0 || ai >= m || aj < 0 || aj >= n)
  24. return INF;
  25. if(bi < 0 || bi >= m || bj < 0 || bj >= n)
  26. return INF;
  27. if(ai == bi && aj == bj)
  28. return INF;
  29. if(board[ai][aj] == 'X' || board[bi][bj] == 'X')
  30. return INF;
  31. if(ai == BI && aj == BJ && bi == AI && bj == AJ)
  32. return 0;
  33. if(DP[ai][aj][bi][bj] != INF)
  34. return DP[ai][aj][bi][bj];
  35. if(visited[ai][aj][bi][bj])
  36. return INF;
  37. visited[ai][aj][bi][bj] = true;
  38. cout << ai << " " << aj << " " << bi << " " << bj << endl;
  39. int movex[] = {0, 0, 1, 0, -1, 1, 1, -1, -1};
  40. int movey[] = {0, 1, 0, -1, 0, 1, -1, 1, -1};
  41. int ret = INF;
  42. for (int i = 0; i < 9; ++i)
  43. {
  44. int ci = ai + movex[i];
  45. int cj = aj + movey[i];
  46. for (int j = 0; j < 9; ++j)
  47. {
  48. if(i == 0 && j == 0)
  49. continue;
  50. int di = bi + movex[j];
  51. int dj = bj + movey[j];
  52. if(ci == bi && cj == bj && di == ai && dj == aj)
  53. continue;
  54. ret = min(ret, dp(ci, cj, di, dj, board));
  55. }
  56. }
  57. DP[ai][aj][bi][bj] = ret + 1;
  58. return DP[ai][aj][bi][bj];
  59. }
  60.  
  61. int minTurns(vector<string> board){
  62. int m = board.size();
  63. int n = board[0].size();
  64. DP.clear();
  65. DP.resize(m, vector<vector<vector<int> > >(n, vector<vector<int> >(m, vector<int>(n, INF))));
  66. visited.clear();
  67. visited.resize(m, vector<vector<vector<bool> > >(n, vector<vector<bool> >(m, vector<bool>(n, false))));
  68. for (int i = 0; i < board.size(); ++i)
  69. {
  70. for (int j = 0; j < board[0].size(); ++j)
  71. {
  72. if(board[i][j] == 'A'){
  73. AI = i;
  74. AJ = j;
  75. }
  76. if(board[i][j] == 'B'){
  77. BI = i;
  78. BJ = j;
  79. }
  80. }
  81. }
  82. int ans = dp(AI, AJ, BI, BJ, board);
  83. if(ans > INF)
  84. return -1;
  85. return ans;
  86. }
  87.  
  88. int main(int argc, char const *argv[])
  89. {
  90. vector<string> board;
  91. // board.push_back("A.");
  92. // board.push_back(".B");
  93.  
  94. board.push_back("XXXXXXXXX");
  95. board.push_back("A.......B");
  96. board.push_back("XXXX.XXXX");
  97.  
  98. // board.push_back("AB.................X");
  99. // board.push_back("XXXXXXXXXXXXXXXXXXX.");
  100. // board.push_back("X..................X");
  101. // board.push_back(".XXXXXXXXXXXXXXXXXXX");
  102. // board.push_back("X..................X");
  103. // board.push_back("XXXXXXXXXXXXXXXXXXX.");
  104. // board.push_back("X..................X");
  105. // board.push_back(".XXXXXXXXXXXXXXXXXXX");
  106. // board.push_back("X..................X");
  107. // board.push_back("XXXXXXXXXXXXXXXXXXX.");
  108. // board.push_back("X..................X");
  109. // board.push_back(".XXXXXXXXXXXXXXXXXXX");
  110. // board.push_back("X..................X");
  111. // board.push_back("XXXXXXXXXXXXXXXXXXX.");
  112. // board.push_back("X..................X");
  113. // board.push_back(".XXXXXXXXXXXXXXXXXXX");
  114. // board.push_back("X..................X");
  115. // board.push_back("XXXXXXXXXXXXXXXXXXX.");
  116. // board.push_back("...................X");
  117. // board.push_back(".XXXXXXXXXXXXXXXXXXX");
  118. cout << minTurns(board) << endl;
  119. cout << DP[1][6][1][2] << endl;
  120. return 0;
  121. }
  122.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.3/unordered_map:40,
                 from prog.cpp:3:
/usr/include/c++/4.3/c++0x_warning.h:36:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
In file included from /usr/include/c++/4.3/unordered_map:48,
                 from prog.cpp:3:
/usr/include/c++/4.3/type_traits:82: error: template argument 1 is invalid
/usr/include/c++/4.3/type_traits:106: error: template argument 1 is invalid
/usr/include/c++/4.3/type_traits:136: error: expected unqualified-id before '&&' token
In file included from /usr/include/c++/4.3/bits/hashtable.h:55,
                 from /usr/include/c++/4.3/unordered_map:54,
                 from prog.cpp:3:
/usr/include/c++/4.3/tr1_impl/hashtable:223: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/hashtable:223: error: invalid constructor; you probably meant 'std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys> (const std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>&)'
/usr/include/c++/4.3/tr1_impl/hashtable:232: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/hashtable:660: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/hashtable:660: error: prototype for 'std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>)' does not match any in class 'std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>'
/usr/include/c++/4.3/tr1_impl/hashtable:619: error: candidates are: std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(const std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>&)
/usr/include/c++/4.3/tr1_impl/hashtable:218: error:                 template<class _Key, class _Value, class _Allocator, class _ExtractKey, class _Equal, class _H1, class _H2, class _Hash, class _RehashPolicy, bool __cache_hash_code, bool __constant_iterators, bool __unique_keys> template<class _InputIterator> std::_Hashtable::_Hashtable(_InputIterator, _InputIterator, typename _Allocator::size_type, const _H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const _Allocator&)
/usr/include/c++/4.3/tr1_impl/hashtable:557: error:                 std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hashtable(typename _Allocator::size_type, const _H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const _Allocator&)
/usr/include/c++/4.3/tr1_impl/hashtable:714: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/hashtable: In member function 'void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::swap(std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>)':
/usr/include/c++/4.3/tr1_impl/hashtable:723: error: '__x' was not declared in this scope
In file included from /usr/include/c++/4.3/unordered_map:69,
                 from prog.cpp:3:
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:90: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map:90: error: invalid constructor; you probably meant 'std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code> (const std::__unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)'
/usr/include/c++/4.3/tr1_impl/unordered_map:147: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map:147: error: invalid constructor; you probably meant 'std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code> (const std::__unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc, __cache_hash_code>&)'
/usr/include/c++/4.3/tr1_impl/unordered_map:205: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map:205: error: invalid constructor; you probably meant 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc> (const std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&)'
/usr/include/c++/4.3/tr1_impl/unordered_map:209: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In member function 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator=(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:213: error: '__x' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:254: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map:254: error: invalid constructor; you probably meant 'std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc> (const std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&)'
/usr/include/c++/4.3/tr1_impl/unordered_map:258: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In member function 'std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::operator=(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:262: error: '__x' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:283: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In function 'void std::swap(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:285: error: '__x' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map:285: error: '__y' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:290: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In function 'void std::swap(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>&, std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:291: error: '__y' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:295: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In function 'void std::swap(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:297: error: '__x' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map:297: error: '__y' was not declared in this scope
/usr/include/c++/4.3/tr1_impl/unordered_map: At global scope:
/usr/include/c++/4.3/tr1_impl/unordered_map:302: error: expected ',' or '...' before '&&' token
/usr/include/c++/4.3/tr1_impl/unordered_map: In function 'void std::swap(std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>&, std::unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>)':
/usr/include/c++/4.3/tr1_impl/unordered_map:303: error: '__y' was not declared in this scope
stdout
Standard output is empty