美文网首页
葱的战争一道机试题

葱的战争一道机试题

作者: wzmyyj | 来源:发表于2020-04-08 17:08 被阅读0次

题目

前段时间,同学给看了一道,某个历年的考研机试题。我想用面向对象的方法去实现。

一个m*n的棋盘,上面有k根葱,每根葱面朝方向为d(0123分别表示上下左右),每根葱一个战斗力f。每隔时间葱会向面朝方向走一格, 如果遇到棋盘边界,那么他将把面朝方向转180度(此回合葱不会走动),如果某个时刻有两个或以上的葱在同一位置,那么他们将发生 战争,只有战斗力最高的葱存活,其他的葱全部原地枯萎,不在走动,求经过t时间后所有葱的位置。

  • 输入:第一行m n k,然后接下来k行每根葱的信息x y d f(坐标,方向,战斗力),最后一行输入时间t。
  • 输出:k行,分别表示每个葱的位置。
  • 数据范围:m和n在100内,k在1000内,t在1000内,f在1000内,保证初始每颗葱位置不同,战斗力不同。

面向对象

  1. 建一个Cong(葱)类。
public class Cong {// 葱
    private int x;
    private int y;
    private int d;
    private int f;
    private boolean isLive = true;

    public Cong(int x, int y, int d, int f) {
        super();
        this.x = x;
        this.y = y;
        this.d = d;
        this.f = f;
        this.isLive = true;// 默认存活
    }

    //   此处省略 set 和get 方法

    public void go() {// 走
        if (!isLive)
            return;
        switch (d) {
        case 0:
            y++;
            break;
        case 1:
            y--;
            break;
        case 2:
            x--;
            break;
        case 3:
            x++;
            break;
        }

    }

    public void turn() {// 转向
        if (!this.isLive)
            return;
        switch (d) {
        case 0:
            d = 1;
            break;
        case 1:
            d = 0;
            break;
        case 2:
            d = 3;
            break;
        case 3:
            d = 2;
            break;
        }

    }

    public void fight(Cong c) {// 打架
        if (!this.isLive || !c.isLive())// 死了没?
            return;
        if (this.x == c.getX() && this.y == c.getY()) {// 遇见没?
            if (this.f > c.getF()) {
                c.setLive(false);
            } else if (this.f < c.getF()) {
                this.setLive(false);
            } else {
                this.setLive(false);
                c.setLive(false);
            }
        }
    }
}
  1. Pan(棋盘)类
public class Pan {// 棋盘
    private int m;
    private int n;
    private List<Cong> g;

    public Pan(int m, int n, List<Cong> g) {
        super();
        this.m = m;
        this.n = n;
        this.g = g;
    }

    public void run() {
        for (Cong c : g) {
            if (!c.isLive())// 死亡
                continue;
            war(c);// 战争
            if (isTurn(c)) {// 行动
                c.turn();
            } else {
                c.go();
            }
        }
    }

    public boolean isTurn(Cong c) {// 是否转向
        if (c.getX() == 0 && c.getD() == 2)
            return true;
        if (c.getX() == m - 1 && c.getD() == 4)
            return true;
        if (c.getY() == 0 && c.getD() == 1)
            return true;
        if (c.getX() == n - 1 && c.getD() == 0)
            return true;
        return false;
    }

    public void war(Cong c) {
        for (Cong c1 : g) {
            if (c != c1)// 不跟自己打
                c.fight(c1);
        }
    }
}
  1. 客户端代码
public class Client {// 客户端
    public static void main(String[] args) {
        // 输入
        Scanner can = new Scanner(System.in);
        int m = can.nextInt();
        int n = can.nextInt();
        int k = can.nextInt();

        List<Cong> g = new ArrayList<Cong>();
        for (int i = 0; i < k; i++) {
            int x = can.nextInt();
            int y = can.nextInt();
            int d = can.nextInt();
            int f = can.nextInt();
            Cong c = new Cong(x, y, d, f);
            g.add(c);
        }
        int t = can.nextInt();
        can.close();

        // 逻辑操作
        Pan p = new Pan(m, n, g);
        for (int i = 0; i < t; i++) {// 执行t次
            p.run();
        }

        // 输出
        for (int i = 0; i < k; i++) {
            Cong c = g.get(i);
            System.out.print(c.getX() + " " + c.getY() + "\n");
        }

    }
}

附页

源码地址:https://github.com/wzmyyj/CongWar

配一张图

相关文章

网友评论

      本文标题:葱的战争一道机试题

      本文链接:https://www.haomeiwen.com/subject/rbereftx.html