"" "" Simulation Problem ""
"title" The Worm turns time limit:2 Seconds Memory limit:65536 KB
Worm is a old computer game. There are many versions, but all involve maneuvering a "worm" around the screens, trying to avoid running the worm in its Elf or an obstacle.
We ll simulate a very simplified version here. The game would be played on a X board, and numbered so this square at the upper of the left is numbered (1, 1). The worm is initially a string of connected squares. Connected squares are adjacent horizontally or vertically. The worm starts stretched out horizontally in positions (a) through (25, 30) with the head of the worm. The worm can move either East (E), D (W), North (N) or South (S), but'll never move back on itself. So, in the initial position, the A-W move are not possible. Thus the two squares occupied by the worm, that, and the any move are their head and tail. The "head of" the worm can move to the square just vacated by the worm ' s tail.
You'll be given a series of moves and would simulate the moves until either the worm runs into itself, the worm runs off The board, or the worm successfully negotiates its list of moves. In the the ' the ' two cases you should ignore the remaining in the list.
Input
There would be multiple problems instances. The input for each problem instance is on two lines. The ' A ' is a integer n (<100) indicating the number of moves to follow. (A value of n = 0 indicates end of input.) The next line contains n characters (either E, W, N, and S), with no spaces separating the letters, indicating the sequence of moves.
Output
Generate one line to output for each problem instance. The output line should is one of the follow three:
The worm ran into itself in move M.
The worm ran off the board in Move M.
The worm successfully made all M moves.
Where m is for I to determine and the the ' the ' I move 1.
Sample Input
18
Nwwwwwwwwwwsesssws
20
Ssswwnennnnnwwwwssss
30
Eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
13
Swwwwwwwwwnee
0
Sample Output
The worm successfully made all moves.
The worm ran into itself in move 9.
The worm ran off the board in move 21.
The worm successfully made all moves.
Source:east North America 2001, Practice
"Instructions"
Worm game is a relatively classic handheld game, a simple version of a worm in the grid interface, the player to control the direction of its movement in order to prevent it from touching its own body or walls. The topic simulates a simple worm game to give a 50x50 lattice matrix, and a worm head and tail coordinates of 20 squares in length, the initial worm as a worm head, Yuhirian. Requires that the moving direction of the input string (' E ' ' W ' S ' ' N ' means move to the east and west), the output worm's movement results (a worm head touched his or her body or wall, or safely moved through all the steps).
Figure 1
Troubleshooting
(i) Analysis: the development of 50x50 two-dimensional bool array bord on behalf of the worm crawling lattice matrix, the array element bord (I,J) in the matrix, the horizontal axis is J, the ordinate is I of the lattice (I increase the expression ordinate upward, j increase means the horizontal axis to the right), A lattice value of TRUE indicates that the lattice is a worm body, false as a space, and Bord (0,0) as the origin of the matrix coordinate system. For each move, only 2 lattice in the lattice of the state will change, one is the worm head will move to the grid state will become true, one is the tail removed after the original lattice state to false, so the program design simulation process only to consider the worm head and tail of the movement of the direction of change, That is, each move step according to the direction of movement to determine the worm head, wormtail which direction of the lattice state needs to change. It should be noted that the insect head pulls the entire worm body (including wormtail) movement, so each time the direction of the head and the input given by the direction of each move is the same, and the direction of the worm is based on the location of the current position to determine, as shown in Fig. 1 The movement of a wheel suddenly to the south, then the head of the worm to the south, and the tail of At this point it is also the east of the worm body moving, so the worm tail still eastward until its follow the worm body moved to the north and south after moving northward. The final step is to determine whether the grid to be moved by the worm in the course of the movement is a worm or a crossing, otherwise it means that the safe movement completes all the input steps required.
(ii) Code:
#include <iostream> using namespace std; int main () {bool board[50][50];//simulate worm crawl matrix lattice, the lattice value is true indicates that the lattice is worm body, false is space int hi,hj,ti,tj;//worm Head lattice coordinate (HI,HJ), tail lattice coordinates (TI,TJ
int n;//moving Direction string length int i,j; Char Moves[100],td;//moves represents a moving direction string.
TD indicates the movement direction of the tail (cin>>n) {if (n==0) break;
cin>>moves;
hi=24;
hj=29;
ti=24;
tj=10;
for (i=0;i<50;i++) for (j=0;j<50;j++) Board[i][j]=false;
for (j=10;j<=29;j++) board[24][j]=true;
Td= ' E ';
for (i=0;i<n;i++) {//Bug tail ready to remove, vacated original lattice board[ti][tj]=false;
Move the insect head direction if (moves[i]== ' E ') {hj++;
else if (moves[i]== ' W ') {hj--;
else if (moves[i]== ' N ') {hi++;
else {hi--; //insect tail to prepare to the east, and the east of the worm tail is a space, then wormtail southward or North if (td== ' E ' &&board[ti][tj+1]==false) {if (board[ti-1][tj]==true)/worm tail
The South is the worm body, then the tail of the worm to the south {ti--;
Td= ' S ';
else if (board[ti+1][tj]==true)//Worm tail North is worm body, then the tail of worms to North {ti++;
Td= ' N '; }//The worm follows the head of the worm to prepare westward, and the west of the worm tail is empty.Lattice, then wormtail southward or northward else if (td== ' W ' &&board[ti][tj-1]==false) {if (board[ti-1][tj]==true)//Worm tail South is the worm body, then the tail to the south {
ti--;
Td= ' S ';
else if (board[ti+1][tj]==true)//Worm tail North is worm body, then the tail of worms to North {ti++;
Td= ' N '; }//The worm follows the head of the worm to prepare to the north, and the tail of the worm is the north of the space, then Wormtail westward or eastward else if (td== ' N ' &&board[ti+1][tj]==false) {if board[ti][tj-1]=
=true)//Worm tail West is a worm body, then the tail of the worm to the west {tj--;
Td= ' W ';
else if (board[ti][tj+1]==true)//Worm tail East is worm body, then wormtail to east {tj++;
Td= ' E '; }//The worm follows the head of the worm to prepare to the south, and the tail of the worm is a space on the south, then the Wormtail westward or eastward else if (td== ' S ' &&board[ti-1][tj]==false) {if board[ti][tj-1]=
=true)//Worm tail West is a worm body, then the tail of the worm to the west {tj--;
Td= ' W ';
else if (board[ti][tj+1]==true)//Worm tail East is worm body, then wormtail to east {tj++;
Td= ' E ';
}//The worm follows the head of the worm to the east and the West, and the corresponding surface of the worm tail is the worm body, then the worm tail and the insect head move in the same direction else {if (td== ' E ') tj++;
else if (td== ' W ') tj--;
else if (td== ' S ') ti--;
else ti++; //Bug Head collision to worm body if (board[hi][hj]==true) {cout<< "The worm ran INto itself on move "<<i+1<<".
<<endl;
Break //Bug head move to matrix outside if (hi<0| | hi>=50| | hj<0| | HJ>=50) {cout<< "The worm ran off the board on move" <<i+1<< ".
<<endl;
Break
//Bug head move to next space board[hi][hj]=true; } if (i>=n) cout<< "The worm successfully made all" <<i<< "moves."
<<endl;
return 0; }//accepted
(Solution to 2009/10)