gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:50:55 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com