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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:27:00+00:00 2026-05-18T02:27:00+00:00

Hi I write a program for solving 9 problem by bfs which is attached

  • 0

Hi
I write a program for solving 9 problem by bfs which is attached bellow.
I want to change this code in order to make it use of dfs. in my bfs I used an array and 2 pointers to simulate queue and putting successor at the end of queue each time.
I don’t know what should I do to change it to DFS.
what should I do?

#include <stdio.h>    
#include <conio.h>            
#define MAX_QUEUE_SIZE 1000            
struct State    
{    
    int table[3][3];        
};        

struct Queue    
{   
  State contents[MAX_QUEUE_SIZE];    
  int front;                                
  int count;   
};        

//prototypes of functions

int checkGoal(State curState);    
State* findChilds(State state);    
void printState(State state);       

int main()    
{    
    //number of steps    
    int nodesNumber=0;              
    //initial state    
    State s0;

    s0.table[0][0]=1;    
    s0.table[0][1]=2;    
    s0.table[0][2]=3;    
    s0.table[1][0]=4;    
    s0.table[1][1]=8;   
    s0.table[1][2]=5;    
    s0.table[2][0]=7;   
    s0.table[2][1]=9;    
    s0.table[2][2]=6;        

    Queue queue={NULL,0,0};    
    queue.contents[queue.count++]=s0;               
    //check that if the front node in queue pass the goal test finish the program    
    while(checkGoal(queue.contents[queue.front])!=1)    
    {    
        nodesNumber++;   
        //removeing front node from queue and shift front pointer to next one    
        State curState = queue.contents[queue.front++];     
        //print current state table    
        printState(curState);          
        //getting successors of current state   
        State* childStates=findChilds(curState);     
        //putting successors to queue

        for(int i=0;i<4;i++)
            queue.contents[queue.count++]=childStates[i];
    }
    printState(queue.contents[queue.front]);    
    printf("Number of Nodes Which Were Tested In BFS Tree is : %d\n",nodesNumber);      
    getch();    
    return 0;    
}      

//if even one of the cells of the table are not equal to its number correct will be false 
int checkGoal(State curState)    
{  
  int correct=1;    
  for(int i=0;i<3;i++)  
  {  
      for(int j=0;j<3;j++)   
      {
          if(curState.table[i][j]!=(i*3+j+1))
          {
              correct=0;
          }
      }
  }
  return correct;  
}    

void printState(State state)   
{
  for(int i=0;i<3;i++) {
      for(int j=0;j<3;j++)
          printf("%d ",state.table[i][j]);
      printf("\n");
  }
  printf("\n\n");
}
//return list of childs of given state 
State* findChilds(State state)
{
    State* newStates=new State[4];
    //temporary table    
    int x[3][3];    
    int a,b,counter=0;
    //creating all possible states

    for(int i=0;i<2;i++) 
    {
       for(int j=0;j<2;j++)
       {
          for(a=0;a<3;a++)
          for(int b=0;b<3;b++)

          x[a][b] = state.table[a][b];

        //rotating
            int temp = x[i][j+1]; 
            x[i][j+1] = x[i][j];
            x[i][j] = x[i+1][j];
            x[i+1][j] = x[i+1][j+1];
            x[i+1][j+1] = temp;
            State newState;

            for(a=0;a<3;a++)
                for(b=0;b<3;b++)
                    newState.table[a][b]=x[a][b];

            //putting new state in the queue
            newStates[counter++]=newState;
        }
    }
    return newStates;
}
  • 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-18T02:27:00+00:00Added an answer on May 18, 2026 at 2:27 am

    BFS uses a Queue, DFS uses a stack. In your case, all you need to do is to pick the element at the back of the array instead of the front element to turn your BFS into a DFS, i.e. turn

      State curState = queue.contents[queue.front++];
    

    into

      State curState = queue.contents[--queue.count];
    

    and change the other parts that refer to queue.front accordingly.

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

Sidebar

Related Questions

I want to write a program in which plays an audio file that reads
I want to write a program that would print every combination of a set
I want to write a program in C/C++ that will dynamically read a web
I'm planning to write a program in Ruby to analyse some data which has
I need to write a program used internally where different users will have different
Sometimes whenever I write a program in Linux and it crashes due to a
I have to write a program that read from a file that contains the
I would like to write a program that will identify a machine( for licensing
I'm trying to write a program to process the BSD-style process accounting file under
I'm trying to write a program that uses sockets to connect with other instances

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.