Hdoj 1429 victory (continued) [BFS + state compression]

Source: Internet
Author: User


Title: hdoj 1429 victory (continued)

Same question:

Question: Chinese, read by yourself


Analysis: the question is to find the minimum escape time and determine to use BFS

The difficulty of this question is that there are several locks for several keys, and the unique ing relationship cannot be directly labeled, because a graph may need to be searched multiple times.

After careful analysis, we will find that the number of searches in the graph is related to the number of keys, so we can use the binary 0 and 1 to indicate the number of keys that have not appeared, so we can use state compression to mark the key that has appeared, and then use 3D marking. The third dimension indicates the search of the points where several keys have occurred. Others are just as simple.


AC code:

# Include <cstdio> # include <iostream> # include <queue> # include <cstring> using namespace STD; const int n = 25; char map [N] [N]; int vis [N] [N] [1 <11]; struct node {int X, Y, step; int key ;}; int DX [6] = {0, 0, 1,-1}; int dy [6] = {1,-1, 0}; int m, n, T; int BFS (node St, node en) {memset (VIS, 0, sizeof (VIS); ST. step = 0; ST. key = 0; queue <node> q; q. push (ST); node tmp1, tmp2; while (! Q. empty () {tmp1 = Q. front (); q. pop (); If (tmp1.x = en. X & tmp1.y = en. y) return tmp1.step; For (INT I = 0; I <4; I ++) {tmp2.x = tmp1.x + dx [I]; tmp2.y = tmp1.y + dy [I]; tmp2.step = tmp1.step + 1; tmp2.key = tmp1.key; If (Map [tmp2.x] [tmp2.y]> = 'A' & map [tmp2.x] [tmp2.y] <= 'J ') tmp2.key = tmp2.key | (1 <(Map [tmp2.x] [tmp2.y]-'A ')); if (Map [tmp2.x] [tmp2.y] = '*' | tmp2.step> = T) continue; If (vis [tmp2.x] [tmp2.y] [tmp2.key] = 0 & Tmp2.x> = 1 & tmp2.y> = 1 & tmp2.x <= M & tmp2.y <= N) {vis [tmp2.x] [tmp2.y] [tmp2.key] = 1; if (Map [tmp2.x] [tmp2.y]> = 'A' & map [tmp2.x] [tmp2.y] <= 'J ') {If (tmp2.key & (1 <map [tmp2.x] [tmp2.y]-'A') Q. push (tmp2);} else Q. push (tmp2) ;}}return-1 ;}int main () {// freopen ("input.txt", "r", stdin); While (~ Scanf ("% d", & M, & N, & T) {node St, en; For (INT I = 1; I <= m; I ++) {getchar (); For (Int J = 1; j <= N; j ++) {scanf ("% C ", & map [I] [J]); If (Map [I] [J] = '@') ST. X = I, St. y = J; If (Map [I] [J] = '^') en. X = I, en. y = J ;}} int ans = BFS (St, en); If (ANS <t) printf ("% d \ n", ANS ); else puts ("-1");} return 0 ;}


Hdoj 1429 victory (continued) [BFS + state compression]

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.