The entire game board is 50*50 in size. in the upper left corner of the page (), a greedy snake consists of 20 nodes, with the header located at () and horizontally extended ), there are four directions: east, west, south, and north. The question is to give you a motion sequence and determine which of the following three conditions is the final result: 1) normal. 2) Hit your body with your head. 3) outbound.
# Include <iostream>
# Include <string>
Using namespace STD;
// Snake Node
Struct wnode
{
Int X; // The row number.
Int y; // column number
};
Int main ()
{
String moves; // The moving sequence.
Wnode worm [20]; // snake
Int N, I, j;
While (CIN> N & n! = 0)
{
// Initialize the snake from the header to the tail
For (I = 0; I <20; ++ I)
{
Worm [I]. x = 25; // start row
Worm [I]. Y = 30-i; // start Column
}
Cin> moves; // input the moving sequence.
For (I = 0; I <n; ++ I)
{
// Other nodes in the greedy snake move to the previous node location
For (j = 19; j> 0; -- J)
{
Worm [J]. x = worm [J-1]. X;
Worm [J]. Y = worm [J-1]. Y;
}
// Move the header
If (moves [I] = 'n ')
{// North
Worm [0]. X-= 1;
}
Else if (moves [I] ='s ')
{// South
Worm [0]. x + = 1;
}
Else if (moves [I] = 'W ')
{// To the west
Worm [0]. Y-= 1;
}
Else if (moves [I] = 'E ')
{// East
Worm [0]. Y + = 1;
}
// Determine whether the domain name is out of bounds
If (worm [0]. x> 50 | worm [0]. y> 50 | worm [0]. x <1 | worm [0]. Y <1)
{
Cout <"the worm ran off the board on move" <I + 1 <"." <Endl;
Break;
}
// Determine whether it hits your body
For (j = 1; j <20; ++ J)
{
// The header node hits another node
If (worm [0]. x = worm [J]. X & worm [0]. Y = worm [J]. Y)
{
Cout <"the worm ran into itself on move" <I + 1 <"." <Endl;
Break;
}
}
If (J! = 20) break; // a collision occurs and the motion cannot be continued.
}
If (I = N)
Cout <"the worm successfully made all" <n <"Moves." <Endl;
}
Return 0;
}