Zoj1056 the worm turns

Source: Internet
Author: User

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;
}

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.