Maze recursion algorithm implemented using c ++

Source: Internet
Author: User


The program has been debugged by vc6.0, including a header file maze. h and A main program

Add a code indicating the file format.

# Ifndef MAZE_H
# Define MAZE_H

# Include <iostream. h>
# Include <fstream. h>
# Include <stdlib. h>

Typedef struct
{Int left;
Int forward;
Int right;
} Intersection;

Class Maze
{Private:
Int mazesize;
Int EXIT;
Intersection * intsec;
Public:
Maze (char *);
Int TraverseMaze (int );
};

Maze: Maze (char * filename)
{Ifstream fin;
Int I;
Fin. open (filename, ios: in | ios: nocreate );
If (! Fin)
{Cerr <"the maze dose not exist" <endl;
Exit (-1 );
}

Fin> mazesize;
Intsec = new Intersection [mazesize + 1];
For (I = 1; I <= mazesize; I ++)
Fin> intsec [I]. left> intsec [I]. forward> intsec [I]. right;
Fin> EXIT;
Fin. close ();
}

Int Maze: TraverseMaze (int intsecvalue)
// 0 denote for no availble road exist
{
If (intsecvalue> 0)
{If (intsecvalue = EXIT)
{Cout <intsecvalue <'';
Return 1;
}
// Go to left
Else if (TraverseMaze (intsec [intsecvalue]. left ))
{Cout <intsecvalue <'';
Return 1;
}

Else if (TraverseMaze (intsec [intsecvalue]. forward ))
{Cout <intsecvalue <'';
Return 1;
}

Else if (TraverseMaze (intsec [intsecvalue]. right ))
{Cout <intsecvalue <'';
Return 1;
}
}
Return 0;
}
# Endif

# Include "maze. h"
Void main ()
{Char filename [32];
// String filename;
Cout <"enter the data file name :";
Cin> filename;
Maze M (filename );
If (M. TraverseMaze (1 ))
Cout <endl <"you are free" <endl;
Else cout <endl <"you are trapped" <endl;
}

6
0 2 0
3 5 6
0 0 4
0 0 0
0 0 0
7 0 0
7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.