Hdu 3152 Obstacle Course bfs + priority queue
# Include
# Include
# Include
# Include
# Include
Using namespace std;
Const int maxn = 130;
Const int inf = 0x7fffffff;
Int N;
Int dx [4] = {-1, 0, 1, 0 };
Int dy [4] = {0, 1, 0,-1 };
Int line [maxn] [maxn];
Int vis [maxn] [maxn];
Int ans = inf;
Struct node
{
Int x, y;
Int sum;
};
Struct cmp {
Bool operator () (struct node & a, struct node & B ){
Return a. sum> B. sum; // the minimum value takes precedence.
}
};
Void bfs ()
{
Priority_queue , Cmp> que;
Struct node first = {1, 1, line [1] [1]};
Vis [1] [1] = 1;
Que. push (first );
While (que. size ())
{
Struct node now = que. top ();
Que. pop ();
If (now. sum> ans)
Continue;
If (now. x = N & now. y = N)
Ans = min (ans, now. sum );
For (int I = 0; I <4; I ++)
{
Int x1 = now. x + dx [I];
Int y1 = now. y + dy [I];
If (! Vis [x1] [y1] & (x1> = 1) & (x1 <= N) & (y1> = 1) & (y1 <= N ))
{
Struct node next = {x1, y1, line [x1] [y1] + now. sum };
Vis [x1] [y1] = 1;
Que. push (next );
}
}
}
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int cas = 0;
While (scanf ("% d", & N)
{
For (int I = 1; I <= N; I ++)
For (int j = 1; j <= N; j ++)
Scanf ("% d", & line [I] [j]);
Ans = inf;
Memset (vis, 0, sizeof (vis ));
Bfs ();
Printf ("Problem % d:", ++ cas );
Printf ("% d \ n", ans );
}
Return 0;
}