Question Link
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1180
This question is not very difficult in itself, that is, to look at '|' and '-' based on the situation, but the meaning of the question must be understood correctly.
That is to say, if t step is taken when the stair is reached, it should be t + 1 step. If it is '|', then if (t + 1) % 2 = 0) '|'; otherwise '-';
I'm setting '| '. '-' Is wrong. After a long time, I found that the framework and main program should be written on the draft to reduce the logical errors of the program.
Think more before coding, think more about several ideas, and choose the best one.
Code:
# Include <iostream>
# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <queue>
# Include <algorithm>
Using namespace STD;
Int ex, ey, n, m;
Int DX [4] = {1, 0,-1, 0 };
Int dy [4] = {0,-1, 0, 1 };
Char map [30] [30];
Int vis [30] [30];
Struct Node
{
Int X, Y, T;
};
Queue <node> q;
Void BFS (int stx, int STY)
{
Memset (VIS, 0, sizeof (VIS ));
Vis [STX] [sty] = 1;
Node S, E;
While (Q. Size ())
Q. Pop ();
S. x = STX;
S. Y = sty;
S. T = 0;
Q. Push (s );
While (Q. Size ())
{
S = Q. Front ();
Q. Pop ();
If (Map [S. x] [S. Y] = 'T ')
{
Printf ("% d \ n", S. t );
Break;
}
Int I;
For (I = 0; I <4; I ++)
{
E. x = S. x + dx [I];
E. Y = S. Y + dy [I];
If (E. x> = 0 & E. x <n & E. y> = 0 & E. Y <M & map [E. x] [E. y]! = '*')
{
Int flag = 0;
If (Map [E. x] [E. Y] = '| ')
{
If (S. T % 2 = 0)
Flag = 1;
Else
Flag = 2;
}
If (Map [E. x] [E. Y] = '-')
{
If (S. T % 2 = 0)
Flag = 2;
Else
Flag = 1;
}
If (flag = 1)
{
If (I % 2 = 1)
{
E. T = s. t + 1;
E. x = S. X;
E. Y = S. Y;
Q. Push (E );
}
Else
{
E. x = E. x + dx [I];
E. Y = E. Y + dy [I];
If (E. x> = 0 & E. x <n & E. y> = 0 & E. Y <M & map [E. x] [E. y]! = '*' & Vis [E. x] [E. Y] = 0)
{
E. T = s. t + 1;
Q. Push (E );
Vis [E. x] [E. Y] = 1;
Vis [E. X-2 * DX [I] [E. Y] = 1;
}
}
}
Else if (flag = 2)
{
If (I % 2 = 0)
{
E. T = s. t + 1;
E. x = S. X;
E. Y = S. Y;
Q. Push (E );
}
Else
{
E. x = E. x + dx [I];
E. Y = E. Y + dy [I];
If (E. x> = 0 & E. x <n & E. y> = 0 & E. Y <M & map [E. x] [E. y]! = '*' & Vis [E. x] [E. Y] = 0)
{
E. T = s. t + 1;
Q. Push (E );
Vis [E. x] [E. Y] = 1;
Vis [E. x] [E. y-2 * dy [I] = 1;
}
}
}
Else
{
If (vis [E. x] [E. Y] = 0)
{
E. T = s. t + 1;
Q. Push (E );
Vis [E. x] [E. Y] = 1;
}
}
}
}
}
}
Int main (void)
{
Int I, j, STX, sty;
While (scanf ("% d", & N, & M) = 2)
{
Getchar ();
For (I = 0; I <n; I ++)
{
For (j = 0; j <m; j ++)
{
Scanf ("% C", & map [I] [J]);
If (Map [I] [J] ='s ')
{
STX = I;
Sty = J;
}
}
Getchar ();
}
BFS (Stx, STY );
}
Return 0;
}
HDU-1180-weird stairs