Hdu5025 Saving Tang Monk bfs + state Compression
// Open a four-dimensional array. The first two dimensions indicate the position in the graph.
// The last two dimensions indicate the number of keys at the location and the number of snakes passing through the route.
// Because the time and number of steps are not synchronized, priority queue is required.
// Or retrieve the maximum value from all situations
// Because the key needs to be searched in order, the size of the last key can be recorded directly.
// The sequence of passing through the snake is not specific. Therefore, a binary number can be used for snake records.
# Include
# Include
# Include
# Include
Using namespace std;
Const int maxn = 110;
Const int inf = 0x7fffffff;
Char map [maxn] [maxn];
Int vis [maxn] [maxn] [10] [32];
Int dx [4] = {-1, 0, 1, 0 };
Int dy [4] = {0, 1, 0,-1 };
Int st_x, st_y, en_x, en_y;
Int snake [maxn] [maxn];
Int N, M;
// Int ans;
Struct node
{
Int x, y;
Int state;
Int step;
Int key;
};
Int get_num (int)
{
Int ans_a = 0;
Int temp_a =;
While (temp_a ){
Ans_a + = (temp_a % 2 );
Temp_a/= 2;
}
Return ans_a;
}
Struct cmp {
Bool operator () (struct node & a, struct node & B ){
Return a. step + get_num (a. state)> B. step + get_num (B. state );
}
};
Int flag = 0;
Void bfs ()
{
Memset (vis, 0, sizeof (vis ));
Priority_queue , Cmp> que;
// Queue Que;
Struct node first = {st_x, st_y, 0, 0 };
Que. push (first );
Vis [st_x] [st_y] [0] [0] = 1;
While (que. size ())
{
Struct node now = que. top ();
// Struct node now = que. front ();
Que. pop ();
If (now. key = M & now. x = en_x & now. y = en_y)
{
Printf ("% d \ n", now. step + get_num (now. state ));
// Ans = min (ans, now. step + get_num (now. state ));
Flag = 1;
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> N)
Continue;
If (map [next. x] [next. y] = '. '| map [next. x] [next. y] = 'K' | map [next. x] [next. y] = 'T ')
{
Next. key = now. key;
Next. state = now. state;
}
Else if (map [next. x] [next. y] ='s ')
{
If (! (1 < Next. state = now. state | (1 < Else
Next. state = now. state;
Next. key = now. key;
}
Else if (map [next. x] [next. y]> = '1' & map [next. x] [next. y] <= '9 ')
{
If (now. key + 1 = (map [next. x] [next. y]-'0 '))
Next. key = map [next. x] [next. y]-'0 ';
Else
Next. key = now. key;
Next. state = now. state;
}
If (vis [next. x] [next. y] [next. key] [next. state])
Continue;
Vis [next. x] [next. y] [next. key] [next. state] = 1;
Next. step = now. step + 1;
Que. push (next );
}
}
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
While (scanf ("% d", & N, & M) & (N | M ))
{
Int num_s = 0;
For (int I = 1; I <= N; I ++)
{
Scanf ("% s", & map [I] [1]);
For (int j = 1; j <= N; j ++)
{
If (map [I] [j] ='s ')
Snake [I] [j] = num_s ++;
If (map [I] [j] = 'k ')
St_x = I, st_y = j;
If (map [I] [j] = 'T ')
En_x = I, en_y = j;
}
}
// Ans = inf;
Flag = 0;
Bfs ();
If (! Flag)
Printf ("impossible \ n ");
// If (ans = inf)
// Printf ("impossible \ n ");
// Else
// Printf ("% d \ n", ans );
}
Return 0;
}