Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:34:11+00:00 2026-06-12T08:34:11+00:00

I wrote a maze solving program which is supposed to support DFS, BFS, A*,

  • 0

I wrote a maze solving program which is supposed to support DFS, BFS, A*, Dijkstra’s, and greedy algorithm. Anyway, I chose PriorityQueue for my frontier data structure since I thought a priority can behave like a queue, stack, or priority queue depends on the implementation of the comparator.

This is how I implemented my comparator to turn the priority queue into a queue:

/Since the “natural ordering” of a priority queue has the least element at the head and a conventional comparator returns -1 when the first is less than the second, the hacked comparator always return 1 so that the current (last) square will be placed at the tail (this should work recursively)/

public int compare(Square square1, Square square2)
{
    return 1;
}

However, my solution for the maze was not optimal after I did a BFS.

The maze starts at top right corner with coordinate (35,1) and my program checks the left, then up, then down, then right neighbour.
Here are the println I did:

polled out (35,1)

added (34,1)

added (35,2)

polled out (34,1)

added (33,1)

added (34,2)

polled out (35,2)

added (35,3)

polled out (33,1)

added (32,1)

added (33,2)

polled out (34,2)

add (34,3)

poll out (32,1)

……

Notice in a BFS (35,3) should be polled out before (32,1) since the former is added into the queue before the latter. What really confused me is that the data structure behaved like a queue–all new members were added from the back–until I added (32,1), which was placed at the head of the queue.

I thought my comparator should force the priority queue to put new comers in the back. What is even stranger to me is that the data structure changed its nature from a queue to a stack in the middle.

Many thanks to you guys ahead and sorry about my poor English,
Sincerely,
Sean

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:34:13+00:00Added an answer on June 12, 2026 at 8:34 am

    The way you’ve implemented compare is wrong, and would only work if it’s called only in a very specific way that you’re assuming. However, you have no idea in what context the PriorityQueue actually calls compare. The compare function might well be called on an existing element inside the data structure, instead of the new one, or vice versa.

    (Even if you did read the source code and traced it and found that this particular implementation works in a certain way, you shouldn’t depend on that if you want your code to be maintainable. At the least, you’d be making yourself more work by having to explain why it works.)

    You could just use some sort of counter and assign it as the value for each added item, then implement compare correctly based on the value.

    A correct implementation of compare might look like this:

    int compare(Object x, Object y){
        return x.getSomeProperty() - y.getSomeProperty();
    }
    

    Note that if you switch the order of the parameters, the answer will change as well. No, the int returned does not necessarily have to come from {-1, 0, 1}. The spec calls for 0, or a negative or positive integer. You can use any one you wish, so long as it’s the correct sign.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an attempt to write a brute force maze solving C program, I've written
I wrote a small script which copies the billing address to the delivery address
I wrote a program today and I need to show percentage in my output
I'm making a program that generates a maze and then uses bredth first search
I wrote this code which is working but it made me start thinking how
I'm trying to write a simple C++ program to fill a maze with distances
I wrote a makefile which behaves oddly. You can find it here: http://pastebit.com/pastie/8215 Basically
I wrote a simple opengl application in C which plots sin(x). This is my
I wrote some man pages which I'd like to install from a MakeMaker. I'd
i wrote a xlst code to transform xml to html which creates lots of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.