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 8837209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:40:00+00:00 2026-06-14T09:40:00+00:00

In the latest IEEE Xtreme Competition, a problem I’ve tried to solve is this,

  • 0

In the latest IEEE Xtreme Competition, a problem I’ve tried to solve is this,

Input Two points p1(x1,y1) , p2(x2,y2) you must find the length of shortest path from p1 to p2,

for example if p1(1,1) , p2(4,4) then the shortest path has lenght of 9 edges,

enter image description here

I did something like depth first search, it works great if the distance between the two point is small, and take long time for example for the points (1,1) & (10,10),

And there is a limit on the points the maximum point is (12,12).

my approach is to convert the above picture to an undirected graph with all weights to 1, and then find the shortest path.

here are my function that finds the shortest path:

int minCost;
vector<int> path;
multimap<int,int> Connections;
typedef multimap<int,int>::iterator mmit;

void shortestPath(int cs){
    if(cs > minCost)
        return;
    if(path.back() == Target){
        if(cs < minCost)
            minCost = cs;
        return;
    }

    pair<mmit,mmit> it = Connections.equal_range(path.back());
    mmit mit = it.first;

    for( ; mit != it.second ; ++mit){
        if(isVisited(mit->second))
            continue;
        markVisited(mit->second);
        path.push_back(mit->second);
        shortestPath(cs+1);
        markUnvisited(mit->second);
        path.pop_back();
    }
}

Is there any way faster than this ?? could i use dijkstra for this undirected graph ??

  • 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-14T09:40:01+00:00Added an answer on June 14, 2026 at 9:40 am

    Using Dijkstra or any kind of graph-based search seems like total overkill here. At each vertex, you just need to choose the next vertex that brings you closer to your target.

    So you start in the centre of (1,1). You need to choose the starting vertex. Obviously this is the one in the south-east.

    From there, you have three choices: move west, move north-east or move south-east. This is in fact the choice you have at every second vertex. You choose the direction that brings you closer (ie, subtends smallest angle with your target).

    In fact, you can represent all your vertex co-ordinates in a sort of cheaty way. Notice that they are all roughly half-way between the hexagon coordinates. So you could say the first vertex is at (1.5,1.33).

    Your possible moves at the first co-ordinate are the directions:

    west       = (0, -0.67)
    north-east = (-0.5, 0.33)
    south-east = (0.5, 0.33)
    

    Let’s call that the odd-movement. Now, the even-movement you have these choices:

    east       = (0, 0.67)
    north-west = (-0.5, -0.33)
    south-west = (0.5, -0.33)
    

    So all you have to do is, for each possible direction, test the new distance (as the crow flies — ie pythagoras) to your target. Choose the new vertex that has the smallest distance of the three choices. Obviously you don’t have to compute the actual distance — distance squared is fine.

    You can figure out the initial and final moves, I’m sure.

    One final point, obviously I’m using double arithmetic which is imprecise. You can scale all your directions (and of course your hexagon co-ordinates) by 6 and use integers.

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

Sidebar

Related Questions

I have install the latest Xcode but this keep occurring. How could I solve
My latest issue involves trying to find http:// in a variable. This variable contains
I'm using the latest version of the jquery plugin DataTables and I tried to
LATEST Update (Nov 2 2011 9AM) I tried running tnsping from the service and
The latest link I can find is from last may and that's Beta 1.
The latest version of iOS as I write this (iOS 6) no longer has
The latest I can find from the web and blogosphere indicate that Microsoft's XML
The latest oddity with this IDE is the default behaviour of right-clicking on a
My latest problem is one that I already have a solution for, it just
Installed latest version of pandas 0.9.0 in case this was an error. EDIT: forgot

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.