洛谷P1563 [NOIP2016 提高组] 玩具谜题(Java实现)

import java.util.Scanner;

public class P1563玩具谜题 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        peo[] p = new peo[n];

        for (int i = 0; i < n; i++) {
            //小人朝向,0:朝内,1:朝外 0 0 -,0 1 +,1 0 +, 1 1 -
            p[i] = new peo(sc.nextInt(), sc.next());
        }

        //第一个小人的下标
        int first = 0;
        //输入指令,0:左移,1:右移。本题以逆时针为顺序,因此逆时针+,顺时针-
        for (int i = 0;i<m;i++){
            //指令要移动的方向
            int dre = sc.nextInt();
            int ge = sc.nextInt();
            if (p[first].direction == 0 && dre == 0){
                first -= ge;
            }else if (p[first].direction == 0 && dre == 1){
                first += ge;
            }else if (p[first].direction == 1 && dre == 0){
                first += ge;
            }else if (p[first].direction == 1 && dre ==1){
                first -= ge;
            }
            while (first < 0 || first >= n){
                if (first < 0){
                    first += n ;
                }else {
                    first -= n;
                }
            }
        }
        System.out.println(p[first].name);
    }

    public static class peo {
        int direction;
        String name;

        peo(int direction, String name) {
            this.direction = direction;
            this.name = name;
        }

    }
}

猜你喜欢

转载自blog.csdn.net/Chen__sir__/article/details/122817844
今日推荐