HDU 2612 find a way (BFS + priority queue)

Source: Internet
Author: User

This was not the case at the beginning, and the running result was correct, but the memory was too large. Baidu used other people's methods. In fact, they only stored the values below, so I opened an dis [] [] [] array on the basis of the original one. To be honest, this is the first time I used a 3D array, I just stored the following and submitted the AC! Heheh ·······

I used to find a KFC and use BFS once, so the memory is too large. It seems that the storage can reduce a lot of operations and save time !!

# Include <stdio. h>

# Include <string. h>
# Include <queue>
Using namespace STD;

# Define INF 99999999

Char map [201] [201];
Int mark [201] [201];
Int dis [201] [201] [2];
Int n, m;
Int dir [4] [2] = {,-, 0,-1 };

Struct Node
{
Int X, Y, time;
Friend bool operator <(node A, Node B)
{
Return A. Time> B. time;
}
};

Int judge (int x, int y)
{
If (x> = 1 & x <= N & Y> = 1 & Y <= M & Mark [x] [Y] = 0)
Return 1;
Return 0;
}

Int BFS (INT Sx, int SY)
{
Priority_queue <node> q;
Node cur, in;
Int I, flag;
If (Map [SX] [sy] = 'y ')
Flag = 0;
Else
Flag = 1;
Cur. x = SX; cur. Y = sy; cur. Time = 0;
Q. Push (cur );
While (! Q. Empty ())
{
In = Q. Top ();
Q. Pop ();
If (Map [IN. x] [In. Y] = '@')
Dis [IN. x] [In. Y] [flag] = in. time;
For (I = 0; I <4; I ++)
{
Cur. x = in. x + dir [I] [0];
Cur. Y = in. Y + dir [I] [1];
If (Map [cur. x] [cur. Y]! = '#' & Judge (cur. X, cur. y ))
{
Mark [cur. x] [cur. Y] = 1;
Cur. Time = in. Time + 11;
Q. Push (cur );
}
}
}
Return 1;
}
Int main ()
{
Int I, j, Sx, Sy, dx, Dy, Min, Q;
While (scanf ("% d", & N, & M )! = EOF)
{
Min = inf;
For (I = 1; I <= N; I ++)
Scanf ("% s", map [I] + 1 );
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
For (q = 0; q <2; q ++)
Dis [I] [J] [Q] = inf;
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
{
If (Map [I] [J] = 'y ')
{
Memset (mark, 0, sizeof (Mark ));
SX = I; Sy = J;
BFS (I, j );
}
If (Map [I] [J] = 'M ')
{
Memset (mark, 0, sizeof (Mark ));
DX = I; DY = J;
BFS (I, j );
}
}
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
{
If (Map [I] [J] = '@' & dis [I] [J] [0] + dis [I] [J] [1] <min)
Min = dis [I] [J] [0] + dis [I] [J] [1];

}
Printf ("% d \ n", min );
}
Return 0;
}

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2612

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.