-4

我在 JavaScript 中有这个函数:

// this is a unit-cost specialization of dijkstra's algorithm
function floodfill(distmap,idx) {
    var q = [idx], q2 = [];
    distmap[idx] = 1;
    var dist = 1;

    while(q.length) {
        dist++;
        for(var i=0;i<q.length;i++) {
            var idx = q[i];

            for(var move=0;move<4;move++) {
                var i2 = idx+di[move];
                if(map[i2] || distmap[i2])
                    continue;
                distmap[i2] = dist;
                q2.push(i2);
            }
        }
        q = q2; q2 = [];
    }
    return distmap;
}

它被称为

var p1dist;
p1dist = floodfill([], myi); // myi its position of player 2 in map (for example 13
                               // and map[13] = 2);

地图是这样的:

// map: 0 = empty, -1 = wall, 1 = player 1, 2 = player 2
var map = [];
var w = 7 //width of map
var h = 9 //height of map

// start out with a clear map with a border around the edges
for(var j=0;j<h*w;j++) map[j] = 0;

function addWall(x,y) {
    map[x+y*w] = -1;
}

for(var i=0;i<w;i++) { addWall(i,0); addWall(i,h-1); }
for(j=0;j<h;j++) { addWall(0,j); addWall(w-1,j); }

我试图为 Java 重写这个函数(floodfill)但没有成功。我哪里想错了?

public int[] floodfill(int idx) {
    int[] q, q2, distmap;
    q2 = new int[1];
    q = new int[idx];
    distmap = new int [w*h];
    distmap[idx] = 1;
    int dist = 1;

    while(q.length != 0) {
        dist++;
        for(int i=0; i < q.length; i++) {
            int index = q[i];
            for(int move = 0; move < 4; move++) {
                int i2 = index+di[move];
                if (i2 > 0) {
                    if(map[i2] == 0 || distmap[i2] == 0)
                        continue;

                    distmap[i2] = dist;
                    q2 = push(i2, q2);
                }
            }
        }
        q = q2; q2 = new int[1];
    }
    return distmap;
} 

int[] p1dist;
p1dist = floodfill(myi);

和功能推送:

public int[] push(int k, int[] r) {
    int[] p;
    p = new int[r.length+1];
    p[r.length] = k;
    r = p;
    return r;
}

请,如果有人不理解我的问题或其他问题,请询问更多细节,但我真的需要一个答案,因为我今晚有截止日期。

对于这个函数和这样的地图(5 表示墙,地图是 int[] 而不是 int[][]):

 5 5 5 5 5 5 5 5 5 5 5 5 5 
 5 5 5 0 0 0 0 0 5 0 0 0 5 
 5 0 0 2 0 0 0 0 0 0 5 5 5 
 5 0 0 0 0 0 0 0 0 0 0 1 5 
 5 5 5 5 5 5 5 5 5 5 5 5 5         

public int[] floodfill(int idx) {
    ArrayList<Integer> q, q2;
    int[] distmap;
    q2 = new ArrayList<Integer>();
    q = new ArrayList<Integer>();
    q.add(idx);
    distmap = new int [w*h];
    distmap[idx] = 1;
    int dist = 1;

    while(q.isEmpty()) {
        dist++;
        for(Integer index:q) {
            for(int move = 0; move < 4; move++) {
                    int i2 = index+di[move];
                    if (i2 > 0) {
                        if( i2 > map.length || i2 > distmap.length)
                        continue;

                    distmap[i2] = dist;
                    q2.add(i2);
                }
            }
        }
        q = q2; q2 = new ArrayList<Integer>();
    }
    return distmap;
}

我有这个可怕的输出:

        [I@24753433 [I@24753433 [I@24753433 ... [I@24753433 [I@24753433 [I@24753433
4

1 回答 1

0
q = new int[idx];

我想你想要

q = new ArrayList<Integer>();
q.add(idx);

而不是使用数组使用 ArrayList 来删除这个push方法,所以你只需要add

可能是这样的:

public int[] floodfill(int idx) {
    List<Integer> q, q2;
    Integer[] distmap;
    q2 = new ArrayList<Integer>();
    q = new ArrayList<Integer>();
    q.add(idx);
    distmap = new Integer[w*h];
    distmap[idx] = 1;
    int dist = 1;

    while(q.length != 0) {
        dist++;
        for(Integer index:q) {
            for(int move = 0; move < 4; move++) {
                    int i2 = index+di[move];
                if (i2 > 0) {
                    if( i2 > map.length     || map[i2] == null || 
                        i2 > distmap.length || distmap[i2] == null)
                        continue;

                    distmap[i2] = dist;
                    q2.add(i2);
                }
            }
        }
        q = q2; q2 = new ArrayList<Integer>();
    }
    return distmap;
} 

您应该像这样显示列表:

int[] p1dist;
p1dist = floodfill(myi);
for(int i:p1dist){
    System.out.print(i);
}
于 2013-04-23T12:12:18.237 回答