JAVA - Check if value is not in other index 2D array -


im stuck on part of school project have shortest route between 2 co-ordinates (traveling salesman problem). made litle here co-ords of nearest neighbour, few co-ords have same closest neighbour, , dont want that.

i thought of clear issue, isn't working, , can't figure out why.

distance current distance between current position , every other. shortestdistance kind of speaks think.

locations[20][3] 2d array in store xco-ord, yco-ord , nearest neighbour each co-ord. x in [x][0], y in [x][1] , neighbour in [x][2]

for(int = 0; < 20; i++){             int shortestdistance = 100;             int distance;             //looking nearest neighbour 20 times              for(int j = 0; j < 20; j++){                 //looking closest neighbour here                 distancex = locations[i][0] - locations[j][0];                 distancey = locations[i][1] - locations[j][1];                 //to prevent negative distance:                 if(distancex < 0){                     distancex = distancex * -1;                  }                 if(distancey < 0){                     distancey = distancey * -1;                 }                 //add distance                 distance = distancex + distancey;                 //if current distance shorter shortestdistance, prevent does'nt see 'neighbour' , prevent co-ord has same neighbour, happens in isok();                  if(distance < shortestdistance && distancex + distancey != 0 && isok(j)){                     shortestdistance = distance;                     locations[i][2] = j;                 }             }         } 

function isok is:

private boolean isok(int j){     boolean result = false;     for(int = 0; < 20; i++){         if(locations[i][2] == j){             result = false;         }         else{             result = true;         }     }     return result; } 

so, i'm asking doing wrong? still items(in 20 * 10 storage) have same item nearest neighbour.

you have initialize neighbors ok isok method. such value is, instance, -1.

for(int = 0; < 20; i++) locations[i][2] = -1; 

the isok contains small mistake. loop should stopped when j has been found neighbor of location:

private boolean isok(int j){     for(int = 0; < 20; i++){         if (locations[i][2] == j) return false;     }     return true; } 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -