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

  • Home
  • SEARCH
  • 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 9124319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:32:43+00:00 2026-06-17T06:32:43+00:00

I’ve implemented flood fill algorithm that takes array[15*15] with path and generates a queue

  • 0

I’ve implemented flood fill algorithm that takes array[15*15] with path and generates a queue of steps he took while filling the path. tl;dr it looks like this

std::queue <int> f_path;

void Enemy::find_path(int *map, int *grid, int node) {

    if (grid[node] == 1) // colored-grid
        return;
    if (map[node] == 0) // grid with defined map (0 - no path, 1 path)
        return;

    f_path.push(node);
    grid[node] = 1;

    if ((node + 1) % 15 != 0) this->find_path(map, grid, node + 1); // go right
    if (node % 15 != 0) this->find_path(map, grid, node - 1); // go left
    if (node - 15 > 0) this->find_path(map, grid, node - 15); // go up
    if (node + 15 < 15*15) this->find_path(map, grid, node + 15); // go down
}

But now I have a queue of steps it took to fill the grid, but I don’t know how to apply this information for my objects to follow and get from start to end. I mean, it’s simple with one path, but if it splits like this (9 is exit):
0 0 1 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 0
0 0 9 0 0
I will have both left and right path in queue so if I do a simple go(f_path.front()) it will do god’s know what. How do I filter it, so it only goes to exit and then stops? I can’t wrap my head arround it.

  • 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-17T06:32:44+00:00Added an answer on June 17, 2026 at 6:32 am

    Deith, I will say you’re on the right track!

    Right now your code will simply iterate through everything and get nowhere. You forgot a couple things.
    First, Enemy::find_path has to return a boolean: whether or not it reached the destination. So, at the top you have

    if (grid[node] == 1) // colored-grid
        return;
    if (map[node] == 0) // grid with defined map (0 - no path, 1 path)
        return;
    

    I noticed the first one is to keep it from backtracking over itself.
    The second one is pretty clear: It hits a wall. Therefore, have return false after it to show it reached a dead-end. But you need a third so that, if it reaches its destination, it returns true.

    Then, when you call the four-way iterations, test if they return true. If they do then return true again since the goal was reached, thus searching, finding, and zipping back to the source!

    Do note that zipping back to the source part, because that’s the part you can use. Right now your queue will fill up with random junk going everywhere. It doesn’t empty after it goes the wrong way. However, that zip-back part is perfect – instead of a queue, use a stack, and once you reach the destination, push each node when zipping back onto the stack, thus providing a full path from beginning to end!

    Hope I could help 😉

    EDIT: Ok, so there is one more important thing I must mention: working, but inefficient paths.
    Your algorithm will find a path, but not always the shortest path – in fact sometimes it’ll even find a really long path. Fortunately to fix this is somewhat simple. You need to have the coordinate of the destination, first. Then, at each iteration of the find_path function, reorder your direction iterators. First you choose right, then left, then up, then down. Instead, see which direction will head in the direction of the goal. Then check that direction. If it fails try the next closest. If that fails, then try the next closest, and the next closest (well, now the farthest).

    Yes, I know this will not be the shortest distance, since it generally tends toward wall-hugging, but it’s definitely better than going in completely random directions.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I have an array which has BIG numbers and small numbers in it. I
I know there's a lot of other questions out there that deal with this

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.