怎么不一样?
查看原帖
怎么不一样?
749028
QAQ5楼主2024/12/27 20:22
import java.util.Scanner;

public class Main {
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n/*矩形大小*/ = scanner.nextInt(), m/*火把数*/ = scanner.nextInt(), k/*萤石数*/ = scanner.nextInt();
        //接下来输入m个火把和k个萤石的位置
        Position[] torchPositions = new Position[m], fluoritePositions = new Position[k];
        inputPosTo(torchPositions);
        inputPosTo(fluoritePositions);
        Light torch = new Light(swapXY(new BlockState[][]{
                /*
                |暗|暗| 光 |暗|暗|
                |暗|光| 光 |光|暗|
                |光|光|火把|光|光|
                |暗|光| 光 |光|暗|
                |暗|暗| 光 |暗|暗|
                 */
                {BlockState.bark, BlockState.bark, BlockState.bright, BlockState.bark, BlockState.bark},
                {BlockState.bark, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bark},
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright},
                {BlockState.bark, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bark},
                {BlockState.bark, BlockState.bark, BlockState.bright, BlockState.bark, BlockState.bark}
        })), fluorite = new Light(swapXY(new BlockState[][]{
                /*
                |光|光| 光 |光|光|
                |光|光| 光 |光|光|
                |光|光|萤石|光|光|
                |光|光| 光 |光|光|
                |光|光| 光 |光|光|
                 */
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright},
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright},
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright},
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright},
                {BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright, BlockState.bright}
        }));
        System.out.println(torch);
        System.out.println(fluorite);
        RectRegion rectRegion = new RectRegion(n);
        for (Position torchPosition : torchPositions) {
            torch.put(torchPosition, rectRegion);
        }
        for (Position fluoritePosition : fluoritePositions) {
            fluorite.put(fluoritePosition, rectRegion);
        }
        //统计有多少个dark黑暗方块
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (rectRegion.map[i][j] == BlockState.bark)
                    count++;
            }
        }
        System.out.println(rectRegion);
        System.out.println(count);
    }

    //写上去的不是XY循序,要调换
    static BlockState[][] swapXY(BlockState[][] old) {
        final int height = old.length, width = old[0].length;
        BlockState[][] New = new BlockState[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                New[x][y] = old[y][x];
            }
        }
        return New;
    }

    static void inputPosTo(Position[] positions) {
        for (int i = 0; i < positions.length; i++) {
            positions[i] = new Position(scanner.nextInt(), scanner.nextInt());
        }
    }
}

//这个难搞,region可以不要宽高的,但是。。。
abstract class Region {
    final BlockState[][] map;
    final int width, height;

    Region(BlockState[][] map) {
        this.map = map;
        width = map.length;
        height = map[0].length;
    }

    Region(int size) {
        this.width = size;
        this.height = size;
        map = new BlockState[size][size];//怎么不能直接在这里初始化
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                map[i][j] = BlockState.bark;//就只能这样初始化吗
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (map[x][y] == BlockState.bright) {
                    builder.append('光');
                } else {
                    builder.append('暗');
                }
            }
            builder.append('\n');
        }
        return builder.toString();
    }

}

class RectRegion extends Region {
    final int size;

    RectRegion(int size) {
        super(size);
        this.size = size;
    }
}

class Light extends Region {

    Light(BlockState[][] map) {
        super(map);//emm,这。。。
    }

    void put(Position center, RectRegion rectRegion) {
        Position leftTop/*左上角方便遍历*/ = new Position(center.x - width / 2, center.y - height / 2);
        for (int currentY = leftTop.y, mapY = 0, colCount = 0; colCount < height; colCount++, currentY++, mapY++) {
            for (int currentX = leftTop.x, mapX = 0, rowCount = 0; rowCount < width; rowCount++, currentX++, mapX++) {
                if (dotInRectRegion(new Position(currentX, currentY), rectRegion) && map[mapX][mapY] == BlockState.bright)
                    rectRegion.map[currentX][currentY] = BlockState.bright;
            }
        }
    }

    boolean dotInRectRegion(Position dot, RectRegion rectRegion) {
        return dot.x >= 0 && dot.x < rectRegion.size && dot.y >= 0 && dot.y < rectRegion.size;
    }
}

enum BlockState {
    bright, bark
}

class Position {
    final int x, y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
2024/12/27 20:22
加载中...