Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript to Java Conversion
    primarykey
    data
    text
    <p>I have this function in JavaScript:</p> <pre><code>// 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&lt;q.length;i++) { var idx = q[i]; for(var move=0;move&lt;4;move++) { var i2 = idx+di[move]; if(map[i2] || distmap[i2]) continue; distmap[i2] = dist; q2.push(i2); } } q = q2; q2 = []; } return distmap; } </code></pre> <p>and it's called </p> <pre><code>var p1dist; p1dist = floodfill([], myi); // myi its position of player 2 in map (for example 13 // and map[13] = 2); </code></pre> <p>map it's this :</p> <pre><code>// 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&lt;h*w;j++) map[j] = 0; function addWall(x,y) { map[x+y*w] = -1; } for(var i=0;i&lt;w;i++) { addWall(i,0); addWall(i,h-1); } for(j=0;j&lt;h;j++) { addWall(0,j); addWall(w-1,j); } </code></pre> <p>I tried to rewrite this function (floodfill) for Java but with no succes. Where I think wrong? </p> <pre><code>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 &lt; q.length; i++) { int index = q[i]; for(int move = 0; move &lt; 4; move++) { int i2 = index+di[move]; if (i2 &gt; 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); </code></pre> <p>And function push:</p> <pre><code>public int[] push(int k, int[] r) { int[] p; p = new int[r.length+1]; p[r.length] = k; r = p; return r; } </code></pre> <p>Please, if someone doesn't understand my question or something else ask for more details but i really need an answer because I have deadline tonight.</p> <p>For this function and a map like this (5 means wall and map it's int[] not int[][]):</p> <pre><code> 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&lt;Integer&gt; q, q2; int[] distmap; q2 = new ArrayList&lt;Integer&gt;(); q = new ArrayList&lt;Integer&gt;(); 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 &lt; 4; move++) { int i2 = index+di[move]; if (i2 &gt; 0) { if( i2 &gt; map.length || i2 &gt; distmap.length) continue; distmap[i2] = dist; q2.add(i2); } } } q = q2; q2 = new ArrayList&lt;Integer&gt;(); } return distmap; } </code></pre> <p>I have this horrible output:</p> <pre><code> [I@24753433 [I@24753433 [I@24753433 ... [I@24753433 [I@24753433 [I@24753433 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload