HDU 1983 Kaitou kid-the phantom thief (2)

Source: Internet
Author: User

Given an 8*8 map, the time requirement is 5000 Ms. It seems that we want to traverse the entire map and enumerate the blocked area.

In the first place, we wanted to find all feasible paths and then look at the repeated paths to determine how many blocked regions should be set. However, once a blocked area is set, there are different paths again, so what is the difference with enumeration?

Idea (Daniel's ):

Block the grid around the exit or entrance. A maximum of four blocking points are required. Therefore, we can adopt the following strategy: 1. Find a feasible route for thieves. If not, return 0. 2. Calculate the number of blocked points required for the blocked egress and ingress. Take a smaller value. Assume K, k <= 4 3. traverse the solution where the number of blocked points is smaller than K to verify whether the solution is effective. (You can quickly verify whether the line in step 1 is blocked ). If a valid overwrite scheme exists, the overwrite value of the scheme is returned. Otherwise, the process continues. 4. If there is no coverage scheme smaller than K, return K. First of all, in traversal, we naturally think of DFS. To determine whether the setting is successful during the traversal process, we should use the faster BFs, but I did not expect that the path can be saved in this way, I thought the memory would definitely blow up and never dared to give it a try, but maybe it's for this question, if the 8*8 matrix is cleared in time, it seems not big either. This is based on the idea of Daniel, but in my style. Code It may be a coincidence, but it's a little faster than Daniel.
# Include <iostream> # include <queue> using namespace STD; char map [10] [10]; int n, m, T, ANS, dir [4] [2] = {}, {0,-1 }}; bool vis [10] [10] [2]; // a three-dimensional array. The third dimension indicates whether the vertex has obtained the gem struct node {int X, Y, num, and step; int Rox [64], Roy [64]; // X, Y indicates the coordinate, num indicates whether to get the gem, and step indicates the number of steps or time, rox [] and Roy [] respectively Save the path to the point}; node F; // start point queue <node> q; void DFS (INT deep) {If (deep> ans) return; // you only need to block up to four areas in total, that is, the four directions of the entry or exit node T; while (! Q. empty () // clear the queue Q. pop (); q. push (f); memset (VIS, 0, sizeof (VIS); vis [F. x] [F. y] [0] = 1; int minstep =-1; while (! Q. empty () {T = Q. front (); q. pop (); node temp; If (Map [T. x] [T. y] = 'E' & T. num) {minstep = T. step; break;} For (int K = 0; k <4; k ++) {int I = T. X + dir [k] [0]; Int J = T. Y + dir [k] [1]; if (I> N | I <1 | j> M | j <1 | map [I] [J] = '#' | T. step> = T) continue; If (Map [I] [J] = 'J') temp. num = 1; else temp. num = T. num; If (vis [I] [J] [temp. num]) continue; For (int l = 1; L <= T. step; l ++) {temp. rox [l] = T. rox [l]; temp. roy [l] = T. roy [l];} // save path vis [I] [J] [temp. num] = 1; temp. X = I; temp. y = J; temp. step = T. step + 1; temp. rox [temp. step] = I; temp. roy [temp. step] = J; q. push (temp) ;}}if (minstep =-1) // minstep =-1 indicates that the blocked region is successfully set, and kid cannot complete the task {If (deep <ans) ans = deep; return;} For (INT I = 1; I <t. step; I ++) {char cc = map [T. rox [I] [T. roy [I]; // Save the original map if (Cc = 'E' | cc = 's') // The continue cannot be blocked at the entrance or exit; map [T. rox [I] [T. roy [I] = '#'; DFS (deep + 1); // After setting a blocked area, continue traversing map [T. rox [I] [T. roy [I] = cc; // restore the map} int main () {int CAS; CIN> CAS; while (CAS --) {CIN> N> m> T; For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {CIN> map [I] [J]; If (Map [I] [J] = 's') F. X = I, F. y = J;} f. num = 0; F. step = 0; ans = 4; DFS (0); cout <ans <Endl;} return 0 ;}

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.