hdoj_2012年复试题(贪吃蛇)——java实现
problem 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×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(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 move1.
Sample Input
18
NWWWWWWWWWWSESSSWS
20
SSSWWNENNNNNWWWWSSSS
30
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
0
Sample Output
The worm ran into itself on move 9.
The worm ran off the board on move 21.
The worm successfully made all 13 moves.
================================================
思路:
题目乍一看起来很难,但是其实并没有什么难度(顶多很麻烦,没有高深的算法)
直接根据接收的数据来模拟头部移动即可,每次移动前都判断是否违反规则,若违反则直接break
需要注意的点是先让尾部移动,否则头部可能追上本该移动的尾部
下面直接上源码
(尾部在20之前只是向前运动而已,二十之后要跟着输入的轨迹运动,所以要用队列存储每一步运动的点,然后尾部超过初始头部时就转向queue确定轨迹):
package 杭电复试; import java.util.Scanner; /** * 贪吃蛇 */ public class h_2012 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int[][] map = new int[51][51]; for(int i = 11;i<31;++i){ map[25][i] = 1; }//init int head_x = 30; int head_y = 25; int tail_x = 11; int tail_y = 25; int length = sc.nextInt(); char[] data = sc.next().toCharArray(); int[][] queue = new int[1000][2]; int rear = 0; int front = 0; int init_head_x = 30; int init_head_y = 25; boolean flag = false; int i; for(i = 0;i<length;++i){ if(tail_x==init_head_x&&tail_y==init_head_y&&!flag){ flag = true; } if(!flag){ map[tail_y][tail_x] = 0; tail_x += 1; }else{ front = (front+1)%1000; map[tail_y][tail_x] = 0; tail_x = queue[front][0]; tail_y = queue[front][1]; } if(data[i]=='N'){ if(head_y-1==-1){ System.out.println("The worm ran off the board on move " +(i+1)); break; }else if(map[head_y-1][head_x]==1){ System.out.println("The worm ran into itself on move "+(i+1)); break; } rear = (rear+1)%1000; head_y -= 1; queue[rear][0] = head_x; queue[rear][1] = head_y;//recording every point map[head_y][head_x] = 1; }else if(data[i]=='S'){ if(head_y+1==51){ System.out.println("The worm ran off the board on move " +(i+1)); break; }else if(map[head_y+1][head_x]==1){ System.out.println("The worm ran into itself on move "+(i+1)); break; } rear = (rear+1)%1000; head_y += 1; queue[rear][0] = head_x; queue[rear][1] = head_y; map[head_y][head_x] = 1; }else if(data[i]=='W'){ if(head_x-1==-1){ System.out.println("The worm ran off the board on move " +(i+1)); }else if(map[head_y][head_x-1]==1){ System.out.println("The worm ran into itself on move "+(i+1)); break; } rear = (rear+1)%1000; head_x -= 1; queue[rear][0] = head_x; queue[rear][1] = head_y; map[head_y][head_x] = 1; }else if(data[i]=='E'){ if(head_x+1==51){ System.out.println("The worm ran off the board on move " +(i+1)); break; }else if(map[head_y][head_x+1]==1){ System.out.println("The worm ran into itself on move "+(i+1)); break; } rear = (rear+1)%1000; head_x += 1; queue[rear][0] = head_x; queue[rear][1] = head_y; map[head_y][head_x] = 1; } } Util.util.prinout(map); if(i==length){ System.out.println("The worm successfully made all "+i+" moves."); } } } }
代码在本机上已成功输出,中间循环过程可以加上打印,就可以看见蛇的轨迹
希望对大家有所帮助
以上
原文地址:https://www.cnblogs.com/lavender-pansy/p/12557617.html
- HTML语义化:HTML5新标签——template
- 纸上谈兵: 左倾堆 (leftist heap)
- 统计Go, Go, Go
- 前端构建:Source Maps详解
- 解决YUM下Loaded plugins: fastestmirror Determining fastest mirrors 的问题
- 被解放的姜戈01 初试天涯
- 被解放的姜戈02 庄园疑云
- eclipse汉化
- Java魔法堂:URI、URL(含URL Protocol Handler)和URN
- 语义化HTML:ul、ol和dl
- 程序员电邮札记
- JavaSE(十)之反射
- windows下安装redis
- Java魔法堂:内部类详解
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- WordPress调用分类目录 及输出当前分类下的二级目录 和分类文章数量显示
- CSS让Li标签溢出后自动换行
- docker里运行docker命令
- 一次性实战java8 新特性(全)—— Lambda、Optional、stream、DateTime
- 面试Python高频问题汇总
- python自学成才之路 miniconda创建虚拟环境
- 逐行阅读Spring5.X源码(一) BeanDefinition,起点
- linux 磁盘占满 查看占用
- 逐行阅读Spring5.X源码(二) BeanDefinition的父接口-AttributeAccessor 、BeanMetadataElement ,进阶
- 64. Vue中的插槽slot
- 65. Vue中的作用域插槽
- SpringBoot : 全局异常配置
- springboot事务管理
- Java : 分享一个通用的获取请求客户端IP的方法
- 一套常用的css初始化样式