HDU 1180 weird STAIR (BFS + priority queue)

Source: Internet
Author: User

Weird elevator !!!

I was confused when I found something strange!

Look at other people's code! This analysis is available !!!

This is the key to determining the direction of the elevator !!! In order to reduce the time, people can stop in the same place and wait for the elevator to change direction !!!

# Include <stdio. h>

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

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

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

Int judge (int x, int y)
{
If (x> = 0 & x <n & Y> = 0 & Y <m)
Return 1;
Return 0;
}

Int BFS (INT Sx, int SY)
{
Int I;
Int X, Y;
Priority_queue <node> q;
Node cur, next;
Cur. x = SX; cur. Y = sy; cur. Step = 0;
Q. Push (cur );
While (! Q. Empty ())
{
Cur = Q. Top ();
Q. Pop ();
For (I = 0; I <4; I ++)
{
X = cur. x + dir [I] [0];
Y = cur. Y + dir [I] [1];
Next. Step = cur. Step + 1;
If (Judge (x, y ))
{
If (Map [x] [Y] = '-' | map [x] [Y] = '| ')
{
If (cur. Step % 2 = 0)
{
If (Map [x] [Y] = '-' & I> 1) | (Map [x] [Y] = '|' & I <2 ))
{
X + = dir [I] [0];
Y + = dir [I] [1];
}
Else
{
Next = cur;
Next. Step ++;
Q. Push (next );
Continue;
}
}
Else
{
If (Map [x] [Y] = '|' & I> 1) | (Map [x] [Y] = '-' & I <2 ))
{
X + = dir [I] [0];
Y + = dir [I] [1];
}
Else
{
Next = cur;
Next. Step ++;
Q. Push (next );
Continue;
}
}
}
If (MARK [x] [Y] = 0 & judge (x, y ))
{
If (Map [x] [Y] = 'T ')
Return cur. Step + 1;
Else if (Map [x] [Y] = '.')
{
Mark [x] [Y] = 1;
Next. x = x; next. Y = y; next. Step = cur. Step + 1;
Q. Push (next );
}
 
}
}
}
}
Return-1;
}

Int main ()
{
Int ans;
While (scanf ("% d", & N, & M )! = EOF)
{
Int Sx, Sy, I, J;
Memset (mark, 0, sizeof (Mark ));
For (I = 0; I <n; I ++)
Scanf ("% s", map [I]);
For (I = 0; I <n; I ++)
For (j = 0; j <m; j ++)
If (Map [I] [J] ='s ')
SX = I, Sy = J;
Ans = BFS (sx, Sy );
Printf ("% d \ n", ANS );
}
Return 0;
}

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

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.