java - Graphics.Path.Direction - Java 枚举方向问题 - 无法使用 EAST

标签 java algorithm

您好,我创建了一个枚举方向

public enum Direction {
    EAST, SOUTH, WEST, NORTH;
  }

在我的算法代码中,我无法在我的一个循环中调用 EAST 我需要使用 CW 方向,我认为这破坏了我的算法我得到以下信息如果我尝试使用 EAST

会出错

PackageName.BoardElement.Direction cannot be converted to android.graphics.Path.Direction

当我使用 CW Direction 算法运行时,我得到了 attempt to read from null array 错误

循环:

if(c0 == c1){
            int[] tmp = nextIsland(r0, c0, Path.Direction.CW);
            if(tmp[0] != r1 || tmp[1] != c1)
                return false;
            if(BRIDGES_TO_BUILD[r0][c0] == 0 || BRIDGES_TO_BUILD[r1][c1] == 0)
                return false;
            for (int i = r0; i <= r1 ; i++) {
                if(IS_ISLAND[i][c0])
                    continue;
                if(BRIDGES_ALREADY_BUILT[i][c0] == EAST) {
                    return false;
                }
            }
        }

整个算法

public class Land {

private int[][] BRIDGES_TO_BUILD;

private boolean[][] IS_ISLAND;
private Direction[][] BRIDGES_ALREADY_BUILT;

public Land(int[][] bridgesToDo){
    BRIDGES_TO_BUILD = copy(bridgesToDo);

    int R = bridgesToDo.length;
    int C = bridgesToDo[0].length;
    BRIDGES_ALREADY_BUILT = new Direction[R][C];
    IS_ISLAND = new boolean[R][C];
    for(int i=0;i<R;i++) {
        for (int j = 0; j < C; j++) {
            BRIDGES_ALREADY_BUILT[i][j] = null;
            IS_ISLAND[i][j] = bridgesToDo[i][j] > 0;
        }
    }
}

public Land(Land other){
    BRIDGES_TO_BUILD = copy(other.BRIDGES_TO_BUILD);
    int R = BRIDGES_TO_BUILD.length;
    int C = BRIDGES_TO_BUILD[0].length;
    BRIDGES_ALREADY_BUILT = new Direction[R][C];
    IS_ISLAND = new boolean[R][C];
    for(int i=0;i<R;i++) {
        for (int j = 0; j < C; j++) {
            BRIDGES_ALREADY_BUILT[i][j] = other.BRIDGES_ALREADY_BUILT[i][j];
            IS_ISLAND[i][j] = other.IS_ISLAND[i][j];
        }
    }
}

public int[] next(int r, int c, Path.Direction dir){
    int R = BRIDGES_TO_BUILD.length;
    int C = BRIDGES_TO_BUILD[0].length;

    // out of bounds
    if(r < 0 || r >=R || c < 0 || c >= C)
        return null;


    // motion vectors
    int[][] motionVector = {{-1, 0},{0,1},{1,0},{0,-1}};
    int i = Arrays.asList(values()).indexOf(dir);

    // calculate next
    int[] out = new int[]{r + motionVector[i][0], c + motionVector[i][1]};

    r = out[0];
    c = out[1];

    // out of bounds
    if(r < 0 || r >=R || c < 0 || c >= C)
        return null;

    // return
    return out;
}

public int[] nextIsland(int r, int c, Path.Direction dir){
    int[] tmp = next(r,c,dir);
    if(tmp == null)
        return null;
    while(!IS_ISLAND[tmp[0]][tmp[1]]){
        tmp = next(tmp[0], tmp[1], dir);
        if(tmp == null)
            return null;
    }
    return tmp;
}

public boolean canBuildBridge(int r0, int c0, int r1, int c1){
    if(r0 == r1 && c0 > c1){
        return canBuildBridge(r0, c1, r1, c0);
    }
    if(c0 == c1 && r0 > r1){
        return canBuildBridge(r1, c0, r0, c1);
    }

        if (r0 == r1) {
            int[] tmp = nextIsland(r0, c0, Path.Direction.CW//This is where i cannot use "EAST");
            if (tmp[0] != r1 || tmp[1] != c1)
                return false;
            if (BRIDGES_TO_BUILD[r0][c0] == 0)
                return false;
            if (BRIDGES_TO_BUILD[r1][c1] == 0)
                return false;
            for (int i = c0; i <= c1; i++) {
                if (IS_ISLAND[r0][i])
                    continue;
                if (BRIDGES_ALREADY_BUILT[r0][i] == Direction.NORTH)
                    return false;
            }
        }
    if(c0 == c1){
        int[] tmp = nextIsland(r0, c0, Path.Direction.CW);
        if(tmp[0] != r1 || tmp[1] != c1)
            return false;
        if(BRIDGES_TO_BUILD[r0][c0] == 0 || BRIDGES_TO_BUILD[r1][c1] == 0)
            return false;
        for (int i = r0; i <= r1 ; i++) {
            if(IS_ISLAND[i][c0])
                continue;
            if(BRIDGES_ALREADY_BUILT[i][c0] == EAST) {
                return false;
            }
        }
    }
    // default
    return true;
}

public int[] lowestTodo(){
    int R = BRIDGES_TO_BUILD.length;
    int C = BRIDGES_TO_BUILD[0].length;

    int[] out = {0, 0};
    for (int i=0;i<R;i++) {
        for (int j = 0; j < C; j++) {
            if(BRIDGES_TO_BUILD[i][j] == 0)
                continue;
            if (BRIDGES_TO_BUILD[out[0]][out[1]] == 0)
                out = new int[]{i, j};
            if (BRIDGES_TO_BUILD[i][j] < BRIDGES_TO_BUILD[out[0]][out[1]])
                out = new int[]{i, j};
        }
    }
    if (BRIDGES_TO_BUILD[out[0]][out[1]] == 0) {
            return null;
    }
    return out;
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private int[][] copy(int[][] other){
    int[][] out = new int[other.length][other.length == 0 ? 0 : other[0].length];
    for(int r=0;r<other.length;r++)
        out[r] = Arrays.copyOf(other[r], other[r].length);
    return out;
}

public void connect(int r0, int c0, int r1, int c1){
    if(r0 == r1 && c0 > c1){
        connect(r0, c1, r1, c0);
        return;
    }
    if(c0 == c1 && r0 > r1){
        connect(r1, c0, r0, c1);
        return;
    }
    if(!canBuildBridge(r0, c0, r1, c1))
        return;

    BRIDGES_TO_BUILD[r0][c0]--;
    BRIDGES_TO_BUILD[r1][c1]--;

    if(r0 == r1){
        for (int i = c0; i <= c1 ; i++) {
            if(IS_ISLAND[r0][i])
                continue;
            BRIDGES_ALREADY_BUILT[r0][i] = Direction.EAST;
        }
    }
    if(c0 == c1){
        for (int i = r0; i <= r1 ; i++) {
            if(IS_ISLAND[i][c0])
                continue;
            BRIDGES_ALREADY_BUILT[i][c0] = Direction.NORTH;
        }
    }
}
}

最佳答案

当您应该导入 PackageName.BoardElement.Direction 时,您正在导入 android.graphics.Path.Direction。尽管名称相同,但它们是两个独立的类,一个不能替代另一个。

删除第一个导入并将 Path.Direction 替换为 Direction,我相信您会得到想要的结果。

关于java - Graphics.Path.Direction - Java 枚举方向问题 - 无法使用 EAST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51949797/

相关文章:

java 为什么这个方法不可见

python - numpy 的非常规向量乘法

python - 通过转换为迭代来加速递归方法

java - 资源泄漏 'blank' 从未关闭

java - 运行 Boggle Solver 需要一个多小时才能运行。我的代码有什么问题吗?

java - 如何获取该类中某个字段的字段值

java - Chronicle Queue : blockSize, 索引计数和大小指导

algorithm - Perl 两列平面文件到复杂的无序列表树

java - 生成项目的所有唯一组合

algorithm - 二维网格形状的周长