Hdu 1429 bfs + state Compression

Source: Internet
Author: User

Hdu 1429 bfs + state Compression
// Open a 3D tag array to mark whether Ignatius has reached the location in the key-getting state.
// Because the number of keys is up to 10, you can use state Compression
// Use 1 and 0 to indicate whether there are any I-th keys. In this way, the key State obtained by this person can be expressed in binary.
// Use bfs to find the minimum value
# Include
# Include
# Include
# Include
Using namespace std;
Const int maxn = 30;
Const int inf = 0x7fffffff;
Int vis [maxn] [maxn] [1 <10];
Char map [maxn] [maxn];
Int ans = ans;
Struct node
{
Int x, y;
Int step;
Int state;
};
Int st_x, st_y;
Int n, m;
Int dx [4] = {-1, 0, 1, 0 };
Int dy [4] = {0, 1, 0,-1 };
Queue Que;
Void bfs ()
{
While (que. size ())
Que. pop ();
Memset (vis, 0, sizeof (vis ));
Struct node first = {st_x, st_y, 0, 0 };
Vis [st_x] [st_y] [0] = 1;
Que. push (first );
While (que. size ())
{
Struct node now = que. front ();
Que. pop ();
If (map [now. x] [now. y] = '^ ')
{
Ans = now. step;
Break;
}
For (int I = 0; I <4; I ++)
{
Struct node next;
Next. x = now. x + dx [I];
Next. y = now. y + dy [I];
If (map [next. x] [next. y] = '*' | next. x <1 | next. x> n | next. y <1 | next. y> m)
Continue;
If (map [next. x] [next. y]> = 'A' & map [next. x] [next. y] <= 'J ')
If (! (Now. state & (1 <(map [next. x] [next. y]-'A '))))
Continue;
If (map [next. x] [next. y]> = 'A' & map [next. x] [next. y] <= 'J ')
{
Next. state = now. state | (1 <(map [next. x] [next. y]-'A '));
Next. step = now. step + 1;
If (vis [next. x] [next. y] [next. state])
Continue;
Que. push (next );
Vis [next. x] [next. y] [next. state] = 1;
Continue;
}
Next. state = now. state;
Next. step = now. step + 1;
If (vis [next. x] [next. y] [next. state])
Continue;
Vis [next. x] [next. y] [next. state] = 1;
Que. push (next );
}
}
}
Int main ()
{
// Freopen(in.txt, r, stdin );
Int time;
While (scanf (% d, & n, & m, & time )! = EOF)
{
For (int I = 1; I <= n; I ++)
{
Scanf (% s, & map [I] [1]);
For (int j = 1; j <= m; j ++)
If (map [I] [j] = '@')
St_x = I, st_y = j;
}
Ans = inf; // The end point may not be reached, so it is possible that the bfs does not update ans.
Bfs (); // so ans must initialize each time.
If (ans <time)
Printf (% d, ans );
Else
Printf (-1 );
}
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.