89分错了一个
查看原帖
89分错了一个
749028
QAQ5楼主2024/12/31 15:56
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Map map;
        {
            Scanner scanner = new Scanner(System.in);
            char[][] linesChars = new char[Map.size][Map.size];
            for (int line = 0; line < Map.size; line++) {
                char[] lineChars = scanner.nextLine().toCharArray();
                System.arraycopy(lineChars, 0, linesChars[line], 0, Map.size);
            }
            map = new Map(linesChars);
        }
        while (!(map.C.pos.equals(map.F.pos) || (map.repeatC && map.repeatF)))
            map.next();
        if (map.C.pos.equals(map.F.pos))
            System.out.println(map.step);
        else
            System.out.println(0);
    }
}

class Map {
    static final int size = 10;
    char[][] data = new char[size][size];
    PosAndDir C, F;
    int step = 0;
    ArrayList<PosAndDir> readyCs = new ArrayList<>(), readyFs = new ArrayList<>();
    boolean repeatC = false, repeatF = false;

    Map(char[][] origin) {
        int countTwo = 0;
        for (int y = 0; y < size; y++)
            for (int x = 0; x < size; x++) {
                char get = origin[y][x];
                if (countTwo != 2/*后续的多余判断就不知道如何省略了*/) {
                    boolean cache;
                    if ((cache = (get == 'C')) || get == 'F') {
                        if (cache) C = new PosAndDir(new Position(x, y));
                        else F = new PosAndDir(new Position(x, y));
                        countTwo++;
                    }
                }
                data[x][y] = get;
            }
    }

    void next() {
        //当前的是下一次的之前的
        readyCs.add(C.copy());
        readyFs.add(F.copy());
        //先判断下一次要移动的情况作操作
        Position nextC = C.nextPos(), nextF = F.nextPos();
        if (unreachable(nextC))
            C.dir = C.dir.left90();
        else {
            Position pos = C.pos;
            data[pos.x][pos.y] = '.';
            data[nextC.x][nextC.y] = 'C';
            C.pos = nextC;
        }
        if (unreachable(nextF))
            F.dir = F.dir.left90();
        else {
            Position pos = F.pos;
            data[pos.x][pos.y] = '.';
            data[nextF.x][nextF.y] = 'F';
            F.pos = nextF;
        }
        step++;
        //下一次移动完成后的
        repeatC = inArrayList(readyCs, C);
        repeatF = inArrayList(readyFs, F);
    }

    boolean inArrayList(ArrayList<PosAndDir> posAndDirs, PosAndDir posAndDir) {
        for (PosAndDir pad : posAndDirs)
            if (posAndDir.equals(pad))
                return true;
        return false;
    }

    boolean unreachable(Position p) {
        return posOut(p) || isBarrier(p);
    }

    boolean isBarrier(Position p) {
        return data[p.x][p.y] == '*';
    }

    boolean posOut(Position p) {
        return p.x < 0 || size <= p.x || p.y < 0 || size <= p.y;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int line = 0; line < size; line++) {
            for (int x = 0; x < size; x++)
                builder.append(data[x][line]);
            builder.append('\n');
        }
        return builder.toString();
    }
}

class PosAndDir {
    Position pos;
    Direction dir;

    PosAndDir(Position position) {
        this.pos = position;
        dir = Direction.up;
    }

    Position nextPos() {
        return switch (dir) {
            case up -> new Position(pos.x, pos.y - 1);
            case right -> new Position(pos.x + 1, pos.y);
            case down -> new Position(pos.x, pos.y + 1);
            case left -> new Position(pos.x - 1, pos.y);
        };
    }

    PosAndDir copy() {
        PosAndDir result = new PosAndDir(pos.copy());
        result.dir = dir;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj.getClass() != PosAndDir.class)
            return false;
        PosAndDir second = (PosAndDir) obj;
        return pos.equals(second.pos) && dir == second.dir;
    }
}

enum Direction {
    up, right, down, left;

    Direction left90() {
        return switch (this) {
            case up -> right;
            case right -> down;
            case down -> left;
            case left -> up;
        };
    }
}

class Position {
    int x, y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    Position copy() {
        return new Position(this.x, this.y);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj.getClass() != Position.class) return false;
        Position second = (Position) obj;
        return x == second.x && y == second.y;
    }
}

2024/12/31 15:56
加载中...