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