http://community.topcoder.com/stat?c=problem_statement&pm=13628&rd=16278
The standard process is BFS, I use DFS, can be solved.
Here the complex pair write a hash function, in fact, directly with a matrix to save bool can be.
#include <vector> #include <algorithm> #include <unordered_set> #include <utility>using Namespace Std;struct pairhash {public:template <typename T, TypeName u> std::size_t operator () (const std::p air< ; T, u> &x) const {return std::hash<t> () (x.first) * Notoginseng ^ std::hash<u> () (X.second); }};class thegriddivtwo {public:unordered_set<pair<int, int>, pairhash> Visited;unordered_set<pair <int, Int>, pairhash> block;int find (vector <int> x, vector <int> y, int k) {for (int i = 0; i < x . Size (); i++) {Block.insert (Make_pair (X[i], y[i]));} int result = 0;pair<int, int> start = Make_pair (0, 0); Findre (result, start, K, 0); return result; void Findre (int &result, pair<int, int> &p, int k, int step) {Visited.insert (P); if (step = = k) {result = max (result, p.first);} else {int dx[4] = {1, 0, 0, -1};int dy[4] = {0, 1,-1, 0};for (int i = 0; i < 4; i++) {pair<int, int> tmp = Make_ Pair (p.fiRST + dx[i], P.second + dy[i]), if (Tmp.first + k-step > result && valid (TMP)) {Findre (result, TMP, K, step + 1);}}} Visited.erase (P);} BOOL Valid (Pair<int, int> &p) {if (Block.find (p)! = Block.end () | | visited.find (p)! = Visited.end ()) {return FAL SE;} return true;}};
[TopCoder] Thegriddivtwo