ZOJ 1056 The Worm Turns 杭电计算机复试2012第二题

The Worm Turns

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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.


网站上不能提交  奇怪了   使用双向链表   自己感觉应该是没问题~~  main函数 里面乱了一点  其实四个处理可以写一起

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
	int x;
	int y;
	node* next;
	node* pre;
};
//创建链表
void create(node* ht[]){
	node *p,*pre,*head,*tail;
	head = (node*)malloc(sizeof(node));
	head->next = NULL;
	head->pre = NULL;
	head->x = 25;
	head->y = 30;
	pre = head;
	for(int i = 29; i >=11;i--){
		p = (node*)malloc(sizeof(node));
		p->x = 25;
		p->y = i;
		pre->next = p;
		p->pre = pre;
		p->next = NULL;
		pre = p;
	}
	tail = p;
	ht[0] = head;
	ht[1] = tail;
	
} 
//头部插入节点 
node* insert(node* head,int x,int y){
	node *p;
	p = (node*)malloc(sizeof(node));
	head->pre = p;    //将原来的头的前驱节点指向p    !!!!忘记这一步调试了很久 
	p->x = x;
	p->y = y;
	p->next = head;
	p->pre = NULL;
	return p; 
}
int main(){
	char direc[200]; 
	int n,i;
	node *head,*tail,*ht[2],*p,*temp;
	while(scanf("%d",&n)!=EOF && n){
		memset(direc,0,sizeof(direc)); 
		scanf("%s",&direc);
		if(strlen(direc) != n) break; 
		create(ht);          //放在while里面的话  每次都重新走
		head = ht[0];
		tail = ht[1];
		p = head;
		
		for( i = 0;i < strlen(direc);i++){
			if(direc[i] == 'N'){
				while( p->next != NULL ){     //不考虑尾部   因为尾部会随着前进而free 
					if(head->x-1 < 0 ){
						printf("The worm ran off the board on move %d\n",i+1);
						break;
					}else if(p->x == head->x-1 && p->y == head->y){
						printf("The worm move to itself on move %d\n",i+1);
						break;
					} 
					p = p->next;
				}
				
					
				if(p->next == NULL){
					head = insert(head,head->x-1,head->y);
					temp = tail;
					tail = tail->pre;
					tail->next = NULL;
					p = tail;      //free后P已经没有地址了 ,此处为了最下方的if判断把新的尾给p
					
					free(temp);
				}
				
			}else if(direc[i] == 'S'){
				while( p->next!=NULL ){
					if(head->x+1 > 50 ){
						printf("The worm ran off the board on move %d\n",i+1);
						break;
					}else if(p->x == head->x+1 &&p->y == head->y){
						printf("The worm move to itself on move %d\n",i+1);
						break;
					} 
					p = p->next;
				}
				if(p->next == NULL ) {
					head = insert(head,head->x+1,head->y);
					temp = tail;
					tail = tail->pre;
					tail->next = NULL;
					p = tail;
					free(temp);
				}
				
			}else if(direc[i] == 'E'){
				while( p->next!=NULL ){
					if(head->y+1 > 50 ){
						printf("The worm ran off the board on move %d\n",i+1);
						break;
					}else if(p->y == head->y+1 &&p->x == head->x){
						printf("The worm move to itself on move %d\n",i+1);
						break;
					}
					p = p->next;
				}
				if(p->next == NULL ){
					head = insert(head,head->x,head->y+1);
					temp = tail;
					tail = tail->pre;
					tail->next = NULL;
					p = tail;
					free(temp);
				}	
				
				
			}else if(direc[i] == 'W'){
				while( p->next!=NULL ){
					if(head->y-1 < 0 ){
						printf("The worm ran off the board on move %d\n",i+1);
						break;
					}else if(p->y == head->y-1&&p->x == head->x	){
						printf("The worm move to itself on move %d\n",i+1);
						break;
					} 
					p = p->next;
				}
				if(p->next == NULL ){
					head = insert(head,head->x,head->y-1);
					temp = tail;
					tail = tail->pre;
					tail->next = NULL;
					p = tail;
					free(temp);
				}
				
				
			}
			//printf("move:%d\n",i+1);
			//printf("head-x:%d head-y:%d tail-x :%d tali-y:%d\n",head->x,head->y,tail->x,tail->y);
		
			if(p->next!=NULL) break;    //不为空 则已经出现错误  结束for循环 
			else p = head;              //本次操作成功 p重新赋值为head 
		}
		
		if(i == n) printf("The worm successfully made all %d moves\n",n);
	}
	

	
	return 0;
}


<pre name="code" class="cpp">
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值