I have made a priorityqueue which inserts objects and compares them with cost parameter ,when two costs are equal it should keep them in enqueued order,but i found after debugging one time it is in enqueued order and other time it is not in the order but i am not getting what is wrong with my code -
/* package whatever; // don't place package name! */
-
-
import java.util.*;
-
import java.lang.*;
-
import java.io.*;
-
-
class node implements Comparable < node > {
-
-
int x;
-
int y;
-
int dir;
-
-
node(int x, int y, int dir) {
-
this.x = x;
-
this.y = y;
-
this.dir = dir;
-
}
-
-
public int compareTo(node o) {
-
-
if (Ideone.cost[o.x][o.y] == Ideone.cost[x][y])
-
return 1;
-
else {
-
int d = Ideone.cost[x][y] - Ideone.cost[o.x][o.y];
-
if (d > 0)
-
return 1;
-
else
-
return -1;
-
}
-
}
-
-
}
-
-
/* Name of the class has to be "Main" only if the class is public. */
-
class Ideone {
-
-
public static int[][] cost;
-
static PriorityQueue < node > p;
-
public static void main(String[] args) throws java.lang.Exception {
-
-
p = new PriorityQueue < node > ();
-
cost = new int[13][11];
-
-
for (int[] row: cost)
-
Arrays.fill(row, -1);
-
cost[0][8] = 366564;
-
cost[2][9] = 368282;
-
cost[1][3] = 368282;
-
cost[4][9] = 368282;
-
cost[0][9] = 376564;
-
cost[1][9] = 372423;
-
cost[5][9] = 372423;
-
cost[0][3] = 436564;
-
cost[7][0] = 378282;
-
cost[2][10] = 378282;
-
cost[4][10] = 378282;
-
cost[0][4] = 382423;
-
p.add(new node(0, 8, 8));
-
p.add(new node(2, 9, 8));
-
p.add(new node(1, 3, 7));
-
p.add(new node(4, 9, 2));
-
p.add(new node(0, 9, 8));
-
p.add(new node(1, 9, 8));
-
p.add(new node(5, 9, 2));
-
p.add(new node(0, 3, 6));
-
p.add(new node(7, 0, 3));
-
p.add(new node(2, 10, 8));
-
p.add(new node(4, 10, 2));
-
p.add(new node(0, 4, 7));
-
-
while (p.size() != 0) {
-
node n1 = p.poll();
-
System.out.println(n1.x + " " + n1.y + " " + cost[n1.x][n1.y]);
-
-
}
-
-
}
-
}
-
Output is
0 8 366564
1 3 368282
2 9 368282
4 9 368282
5 9 372423
1 9 372423
0 9 376564
4 10 378282
2 10 378282
7 0 378282
0 4 382423
0 3 436564
but i am expecting
0 8 366564
2 9 368282
1 3 368282
4 9 368282
1 9 372423
5 9 372423
0 9 376564
7 0 378282
2 10 378282
4 10 378282
0 4 382423
0 3 436564
0 975 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: weasel |
last post by:
Why is the Farenheit to Celsius part not working properly? Instead of
showing a similar range of what the farenheit is listing, the celsius
portion is showing half the range of farenheit.
print...
|
by: Roberto Castro |
last post by:
I have some problems with the way I am showing the BLOB fields in the
Image web controls. It does work on my localhost though sometimes I
need to hit Refresh for the images to load properly....
|
by: Nita Raju |
last post by:
Hi,
I have to validate a textbox for date without using the validation controls.
So i had to use IsDate(). It's not working properly when i give "11//2004".
When i enter the above date it...
|
by: Annu |
last post by:
Hi
I need help on <enbed> tag.
Following code(No 1) is working properly on windows but on linux code
no.2 is not working
Code No 1:
<EMBED type='application/x-mplayer2'
...
|
by: kamill |
last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it redirected to index.php of admin section......BUT when i...
| |
by: damezumari |
last post by:
When a user logs in to my site http://iwantyourquestion.com I set
$_SESSION to true if his username and password are OK.
When he calls a page I check if $_SESSION is true. If it
not I ask him to...
|
by: rajasree |
last post by:
Hi all,
am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me
my code is as follows;
<script language="javascript"...
|
by: zairali |
last post by:
Hi,
I am trying to fix a webpage ( http://www.d.umn.edu/itss/labs/maps/ ) which uses some html (or xml also?) to show pop ups when you rollover the numbers on a map of the building. They work fine...
|
by: neovantage |
last post by:
Hey all,
Website page is not working properly in IE6 and IE7 but working normal in firefox. Can somebody tell me how i can fixed that problem. Here is the url of the page
http://www.ragehockey.biz/...
|
by: Jiwei06xie |
last post by:
Hi,
I have a main form with 3 subforms and some combo boxes. It is working fine as an independent form. However, when I draged into a tabcontrol page, two off the combo boxes stopped working...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |