Description
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.
题意:整个游戏棋盘是50*50大小的,左上角在(1,1),贪吃蛇由20个节点组成,头部位置在(25,30),水平延展到(25,11),可以有四个运动方向:东,西,南,北。题目就是给你一个运动序列,判断最终结果是下面3种情况的哪一种:1)正常。2)头撞到自己身体。3)出界。
解题思路:
主要思路:用数组body记录贪吃蛇的身体的坐标,并在地图上标记1
执行一次向上(N)移动后,body数组尾部、头部调整
函数int judge(int n,int m)判断贪吃蛇出现以下哪种情况(n,m表示方向)
1)正常。 return 1;
2)头撞到自己身体。 return -1;
3)出界 return 0;
函数int go(string s)模拟贪吃蛇行走
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
//用数组body记录贪吃蛇的身体,
int n,map[51][51],step,body[21][2];
int end = 0; //记录body数组蛇尾下标
int judge(int n,int m) { //判断贪吃蛇出现哪种情况 n,m表示行走方向
if(m==0){
if(n> 0) {
if(body[(end+19)%20][0]-1<1)return 0;
if(map[body[(end+19)%20][0]- 1][body[(end+19)%20][1]]==1) return -1;
}else {
if(body[(end+19)%20][0]+1>50)return 0;
if(map[body[(end+19)%20][0]+1][body[(end+19)%20][1]]==1)return -1;
}
}else {
if(m> 0) {
if(body[(end+19)%20][1]- 1<1) return 0;
if(map[body[(end+19)%20][0]][body[(end+19)%20][1]-1]==1)return -1;
}else {
if(body[(end+19)%20][1]+1>50)return 0;
if(map[body[(end+19)%20][0]][body[(end+19)%20][1]+1]==1)return -1;
}
}
return1;
}
int go(string s) {
for(inti=0; i<20; i++) { //初始化贪吃蛇位置坐标
body[i][0]= 25;
body[i][1]= i+11;
}
for(inti=0; i<n; i++) {
if(s[i]=='N'){
map[body[end][0]][body[end][1]]= 0;
if(judge(1,0)>0){//“正常”继续走
intstart = (end+19)%20;
map[body[start][0]-1][body[start][1]]= 1;
body[end][0]= body[start][0] - 1;
body[end][1]= body[start][1];
end= (end+1)%20;
step++;
}else return judge(1,0);//撞墙或吃到自己直接返回参数判断输出
}else if(s[i] == 'S') {//下面三个方向参考“N”方向注释
map[body[end][0]][body[end][1]]= 0;
if(judge(-1,0)>0){
intstart = (end+19)%20;
map[body[start][0]+1][body[start][1]]= 1;
body[end][0]= body[start][0]+1;
body[end][1]= body[start][1];
end= (end+1)%20;
step++;
}else return judge(-1,0);
}else if(s[i] == 'W') {
map[body[end][0]][body[end][1]]= 0;
if(judge(0,1)>0){
intstart = (end+19)%20;
map[body[start][0]][body[start][1]- 1] = 1;
body[end][0]= body[start][0];
body[end][1]= body[start][1] - 1;
end= (end+1)%20;
step++;
}else return judge(0,1);
}else if(s[i] == 'E') {
map[body[end][0]][body[end][1]]= 0;
if(judge(0,-1)>0){
intstart = (end+19)%20;
map[body[start][0]][body[start][1]+1]= 1;
body[end][0]= body[start][0];
body[end][1]= body[start][1] + 1;
end= (end+1)%20;
step++;
}else return judge(0,-1);
}
}
return1;
}
int main() {
strings;
while(cin>> n,n) {
cin>> s;
step= 0,end = 0; //用end标记贪吃蛇尾部,并将步数step和end初始化0
memset(map,0,sizeof(map));//初始化地图0
for(inti=11; i<=30; i++)
map[25][i]= 1; //将贪吃蛇身体位置在地图中标记1
intans = go(s);//ans用于存储返回参数作为以下判断依据
if(ans>0)printf("The worm successfully made all %d moves.\n",n);
elseif(ans<0) printf("The worm ran into itself on move %d.\n",step+1);
elseprintf("The worm ran off the board on move %d.\n",step+1);
}
return0;
}