zoj 1056 The Worm Turns
Worm is an old computer game. There are many versions, but all involve maneuvering a "worm" around the screen, trying to avoid running the worm into itself or an obstacle.
We'll simulate a very simplified version here. The game will be played on a 50 x 50 board, numbered so that the square at the upper left is numbered (1, 1). The worm is initially a string of 20 connected squares. Connected squares are adjacent horizontally or vertically. The worm starts stretched out horizontally in positions (25, 11) through (25, 30), with the head of the worm at (25, 30). The worm can move either East (E), West (W), North (N) or South (S), but will never move back on itself. So, in the initial position, a W move is not possible. Thus the only two squares occupied by the worm that change in any move are its head and tail. Note that the head of the worm can move to the square just vacated by the worm's tail.
You will be given a series of moves and will 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 first two cases you should ignore the remaining moves in the list.
Input
There will be multiple problems instances. The input for each problem instance will be on two lines. The first line is an 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 or S), with no spaces separating the letters, indicating the sequence of moves.
Output
Generate one line of output for each problem instance. The output line should be one of the follow three:
The worm ran into itself on move m.
The worm ran off the board on move m.
The worm successfully made all m moves.
Where m is for you to determine and the first move is move 1.
Sample Input
18
NWWWWWWWWWWSESSSWS
20
SSSWWNENNNNNWWWWSSSS
30
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
13
SWWWWWWWWWNEE
0
Sample Output
The worm successfully made all 18 moves.
The worm ran into itself on move 9.
The worm ran off the board on move 21.
The worm successfully made all 13 moves.
Source: East Central North America 2001, Practice
用一个二维数组表示整个游戏画面,其中边界采用-1表示;
初始化的时候空白的地方采用0表示,worm用从尾巴到头采用1-20表示,每次移动一步获得新的头部,并且更新空白的边界
比如移动一步,头部所在的值就是21, >=0 && <= 1的就是空白,-1表示边界,以此类推
1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #include <vector> 5 #include <list> 6 #include <string> 7 #include <cstring> 8 #include <cstdio> 9 #include <algorithm> 10 #include <set> 11 #include <stack> 12 13 using namespace std; 14 15 int main() 16 { 17 int n; 18 while (cin >> n, cin.ignore() && n) 19 { 20 int board[52][52]; 21 22 for (int i = 0; i < 52; ++i) 23 { 24 for (int j = 0; j < 52; ++j) 25 { 26 if (i == 0 || i == 51 || j == 0 || j == 51) 27 board[i][j] = -1; 28 else if (i == 25 && j >= 11 && j <= 30) 29 board[i][j] = j - 10; 30 else 31 board[i][j] = 0; 32 } 33 } 34 35 for (int i = 0; i < 52; ++i) 36 { 37 for (int j = 0; j < 52; ++j) 38 { 39 cout << board[i][j]; 40 } 41 cout << endl; 42 } 43 44 45 //char illegalMove = 'W'; 46 pair<int, int> head(25, 30), tail(25, 11); 47 int h = 20, t = 1, m = 0; 48 while (m++ < n) 49 { 50 int r, c; 51 char ch = getchar(); 52 switch (ch) 53 { 54 case 'W': 55 head.second--; 56 break; 57 case 'E': 58 head.second++; 59 break; 60 case 'N': 61 head.first--; 62 break; 63 case 'S': 64 head.first++; 65 break; 66 default: 67 break; 68 } 69 if (board[head.first][head.second] == -1) 70 { 71 cout << "The worm ran off the board on move " << m << "." << endl; 72 break; 73 } 74 else if (board[head.first][head.second] > t) 75 { 76 cout << "The worm ran into itself on move " << m << "." << endl; 77 break; 78 } 79 else 80 { 81 board[head.first][head.second] = ++h; 82 ++t; 83 } 84 } 85 if (m == n + 1) 86 cout << "The worm successfully made all " << n << " moves." << endl; 87 while (m++ < n) 88 getchar(); 89 } 90 }