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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:36:30+00:00 2026-05-31T05:36:30+00:00

I have learned DFS today and I tried practice tonight. I encounter a problem

  • 0

I have learned DFS today and I tried practice tonight.

I encounter a problem in my program.

http://codepad.org/quq5FcwR

void dfs(int x,int y){
  if( maze[x][y]==0 ) maze[x][y]=2;
  if( maze[8][8]==2 ){
    flag=true;
    return;
  }
  if( maze[x+1][y]==0 && x+1<9 ){
    maze[x][y]=2;
    maze[x+1][y]=2;
    dfs(x+1,y);
    if(f lag==false ){
      maze[x+1][y]=0;
      maze[x][y]=0;
    }
  }
  else if( maze[x][y+1]==0 && y+1<9 ){
    maze[x][y]=2;
    maze[x][y+1]=2;
    dfs(x,y+1);                                
    if( flag==false ){
      maze[x][y+1]=0;
      maze[x][y]=0;
    }
  }
  else if( maze[x-1][y]==0 && x-1>0 ){
    maze[x][y]=2;
    maze[x-1][y]=2;
    dfs(x-1,y);                                
    if( flag==false ){
      maze[x-1][y]=0;
      maze[x][y]=0;
    }
  }
  else if( maze[x][y-1]==0 && y-1>0 ){
    maze[x][y]=2;
    maze[x][y-1]=2;
    dfs(x,y-1);                                
    if( flag==false ){
      maze[x][y-1]=0;
      maze[x][y]=0;
    }
  }
  return;
}

1.
The link is the program that I write, but I don’t know how to find the shortest path.

2.
Can you give me some advice about how to do it with stack, I use only recursive.I have seen the wiki about it, but cannot still understand how to use the stack.(eg how to use 1-D array to record the point about 2-D array, I am so confused about it)

Thanks for you spend time reading my problems。

  • 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-05-31T05:36:31+00:00Added an answer on May 31, 2026 at 5:36 am

    There are multiple points to comment on in this case:

    1) The simplest stack is the recursion stack, which you already seem to be using looking at your code. I.e. whenever you call dfs() from the function dfs() itself, all variables will be put onto the stack. In your case, when you call x and y will be saved for returning later. I.e. once you come back from dfs() x and y will have the same value they had before you called.

    2) Before returning you have to undo the last change. In the beginning of your function you set maze[x][y]=2. Before you return you’ll have to undo this, because this space might lead into a dead end.

    3) DFS can be used to find any path with low memory usage. However the path found by DFS may not be the shortest one. There is another algorithm called BFS which will find the shortest path, but has a much higher memory usage. Then there is iterative DFS, which will find the shortest path, has low memory consumption, but takes more time. You’ll have to decide what you want and then pick your algorithm.

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

Sidebar

Related Questions

I have learned so much from http://www.summerofnhibernate.com/ nhibernate screen casts that i wonder why
I have learned basics of Java but want to practice more. I was looking
I have learned C++ and Qt4. I wrote one graphical program with 3000 rows
i have learned that a program is measured by it's complexity - i mean
In school I have learned that creating UML Diagrams/Models is great design practice before
So as I have learned today, if I put a phone in sleep mode
I have learned that we can't instantiate an abstract class. But today i tested
Today I think i might have learned something (not sure), i'm not really a
I have learned Perl and PHP.So I want to do simple real time project
One CSS rule I have learned is that you should use the relative em

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.