TJU ACM-ICPC Online Judge—1191 The Worm Turns
B - The Worm Turns
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
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.
这个题目的话,相信大部分同学都能一眼看出其意思,即使是一个英文题,哈哈,这不就是我们玩的贪吃蛇游戏,判断死亡的一个方法吗,题目不是很难,话不多说看代码先:
AC代码如下:(如有错误和建议,请大家不吝指出)
#include<stdio.h>
#include<string.h>
int main() {
int n,hgs[55][55],wx,wy,tx,ty;
int flag1,flag2;
char str[105];
while(scanf("%d",&n),n) {
getchar();
scanf("%s",str);
memset(hgs,0,sizeof(hgs));//每次游戏开始重置归零
for(int i=11; i<=30; i++) hgs[25][i]=1;
tx=25; ty=30;
wx=25; wy=11;
flag1=flag2=0;
int k=0;
int count=0;
for(int i=0;; i++) {
if(str[i]==' ') continue; //还是判断一下有没有空格的好,被坑怕了,小心一点为好
//先处理尾部的运动
if(count+1<20) {
hgs[wx][wy]=0; wy++;
} else {
hgs[wx][wy]=0;
while(str[k]==' ') k++;
if(str[k]=='E') wy++;
else if(str[k]=='S') wx--;
else if(str[k]=='W') wy--;
else if(str[k]=='N') wx++;
k++;
}
//后处理头部的运动
if(str[i]=='E') { //向右走
ty++;
if(ty>50) {
flag1=1; break;
}
if(hgs[tx][ty]==0) hgs[tx][ty]=1;
else {
flag2=1; break; //头撞到了自己
}
} else if(str[i]=='S') { //向下走
tx--;
if(tx<1) {
flag1=1; break;
}
if(hgs[tx][ty]==0) hgs[tx][ty]=1;
else {
flag2=1; break;
}
} else if(str[i]=='W') { //向左走
ty--;
if(ty<1) {
flag1=1; break;
}
if(hgs[tx][ty]==0) hgs[tx][ty]=1;
else {
flag2=1; break;
}
} else if(str[i]=='N') { //向上走
tx++;
if(tx>50) {
flag1=1; break;
}
if(hgs[tx][ty]==0) hgs[tx][ty]=1;
else {
flag2=1; break;
}
}
count++;
if(count==n) break;
}
if(count==n) printf("The worm successfully made all %d moves.\n",count);
else if(flag1) printf("The worm ran off the board on move %d.\n",count+1);
else if(flag2) printf("The worm ran into itself on move %d.\n",count+1);
}
return 0;
}
TJU ACM-ICPC Online Judge—1191 The Worm Turns的更多相关文章
- TOJ 1191. The Worm Turns
191. The Worm Turns Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 5465 Accepted Run ...
- 【转】lonekight@xmu·ACM/ICPC 回忆录
转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...
- 【转】ACM/ICPC生涯总结暨退役宣言—alpc55
转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering
Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...
随机推荐
- vue 登录跳转
前几次做登录处理,都是写一个公用方法,然后在对应的路由页面调用,即判断是不是处于登录状态,如果不是,就返回登录页面. let exit = (vm)=>{ let login = session ...
- cadence布线完成后的补充操作
完成布线之后,需要生成光绘文件和钻孔文件,在生成钻孔文件之前,还有几点补充!
- CABaRet: Leveraging Recommendation Systems for Mobile Edge Caching
CABaRet:利用推荐系统进行移动边缘缓存 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时 ...
- 常见的java设计模式
单例模式 简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为构造器是被private修饰的,一般通过getInstance()的方法来获取它们的实例. getInstan ...
- 微信小程序没有返回按钮怎么办?微信小程序左上角返回按钮怎么调出来?
如果你发现自己的小程序页面没有返回按钮,请检查是不是用的wx.redirectTo(OBJECT)进行的跳转,如果是那就把它改成wx.navigateTo(OBJECT)就可以了. wx.naviga ...
- 理解Golang哈希表Map的元素
目录 概述 哈希函数 冲突解决 初始化 结构体 字面量 运行时 操作 访问 写入 扩容 删除 总结 在上一节中我们介绍了 数组和切片的实现原理,这一节会介绍 Golang 中的另一个集合元素 - 哈希 ...
- vue-cli的跨域设置
概述 今天打算快速使用vue-cli建立一个小应用用于测试,使用axios发送http请求,但是遇到了跨域问题,总结了一下,供以后开发时参考,相信对其他人也有用. vue-cli的跨域设置 在vue. ...
- CentOS7 Linux中通过加密grub防止黑客通过单用户系统破解root密码
如何防止别人恶意通过单用户系统破解root密码,进入系统窃取数据? 给grub加密,不让别人通过grub进入单用户. 17.3.1 基于centos6进行grub加密 [root@63 ~]# gr ...
- ThinkPHP 数据库操作(二) : 增删改查
基本使用 可以直接使用数据库运行原生SQL操作了,支持 query (查询操作)和 execute (写入操作)方法,并且支持参数绑定. Db::query('select * from think_ ...
- 前端基本知识(三):JS的闭包理解(第一个思考题有错误,已修改)
JS闭包的理解 一.变量的作用域 二.如何从外部读取局部变量 三.什么是闭包 四.深入理解闭包 五.闭包的用途 六.使用闭包注意情况 七.JavaScript的垃圾回收机制 八.一些思考题 一.变量作 ...