• 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

    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.

     

    ================================================

    思路:

    题目乍一看起来很难,但是其实并没有什么难度(顶多很麻烦,没有高深的算法)

    直接根据接收的数据来模拟头部移动即可,每次移动前都判断是否违反规则,若违反则直接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.");
                }
            }
        }
    
    }

    代码在本机上已成功输出,中间循环过程可以加上打印,就可以看见蛇的轨迹

    希望对大家有所帮助

    以上

     

     

     

     

  • 相关阅读:
    c++关于析构的那点小事(个人吐槽向
    我分析内存泄漏的一道作业题,已解决
    大一上学期的一点小疑惑,代码验证ok
    C++类型转换
    c++形参改变实参(对指针的理解
    c++整型->字符型转换
    [转]二重积分换元法的一种简单证明 (ps:里面的符号有点小错误,理解就好。。。
    c++实现矩阵类矩阵行列式,伴随矩阵,逆矩阵
    假期周计划2.0
    大道至简
  • 原文地址:https://www.cnblogs.com/lavender-pansy/p/12557617.html
Copyright © 2020-2023  润新知