[TopCoder] Thegriddivtwo

Source: Internet
Author: User

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&lt ;  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.