A simple maze algorithm implemented by C ++

Source: Internet
Author: User

The runtime environment is Ms vc6.0. If you compile it on vs, change the header file

# Include <iostream>

Using namespace STD;

* **************************** The original code is as follows ********* *************************

Simple code

# Include <iostream. h>
Int const max = 100;
Int const M = 6;
Int const n = 8;
Int maze [M + 2] [n + 2] = {,}, // defines the maze of (6*8, the entry is (1.1), and the exit is (6.8)
},
},
},
},
{, 0 },
},
{1, 1, 1, 1, 1, 1, 1 }};
Typedef struct // direction Encapsulation
{
Int X, Y;
} Item;
Item move [8] = {0, 1}, {1, 1}, {1}, {1,-1}, {0,-1}, {-1, -1}, {-}, {-}; // move Array
Typedef struct // stack element Encapsulation
{
Int X, Y, D;
} Yuansu;
Typedef struct // defines the stack
{
Yuansu data [Max];
Int top;
} Seqstack;
Seqstack * init_seqstack (seqstack * s) // initialize the stack
{
If (! S)
{
Cout <"insufficient space" <Endl;
Return 0;
}
Else
{S-> Top =-1; return s ;}
}
Int empty_seqstack (seqstack * s) // judge empty Stack
{If (S-> Top =-1)
Return 1;
Else
Return 0;
}
Int push_seqstack (seqstack * s, yuansu temp) // inbound Stack
{
If (S-> Top = max-1)
Return 0;
Else
{
S-> top ++;
S-> data [S-> top] = temp;
Return 1;
}
}
Int pop_seqstack (seqstack * s, yuansu & temp) // output Stack
{
If (empty_seqstack (s ))
Return 0;
Else
{
Temp = (S-> data [S-> top]);
S-> top --;
Return 1;
}
}
Int path (seqstack * s, item move [8], int maze [8] [10]) // Maze Algorithm
{
Yuansu temp;
Int X, Y, D, I, J;
Temp. x = 1;
Temp. y = 1;
Temp. d =-1; // initialize the coordinate and direction of the Entry
Push_seqstack (S, temp );
While (! Empty_seqstack (s) // while
{
Pop_seqstack (S, temp); // outputs the stack and saves the top elements of the stack.
X = temp. X;
Y = temp. Y;
D = temp. d + 1;
While (d <8) // start the test in eight directions. If it is 0, it can arrive
{
I = x + move [D]. X;
J = Y + move [D]. Y;
If (maze [I] [J] = 0) // you can determine whether the domain name can be reached.
{
Temp. x = X;
Temp. Y = y;
Temp. d = D; // pass the information of the reachable coordinates to the Temporary Variable temp
Push_seqstack (S, temp); // coordinates and inbound Stack
X = I;
Y = J;
Maze [x] [Y] =-1; // arrive at the new point and mark it as-1
If (x = M & Y = N) // The exit coordinate is reached. If there is a path, success code 1 is returned.
Return 1;
Else
D = 0; // re-initialize the direction
}
Else
D ++;
}
}
Return 0; // no path to the maze. The error code 0 is returned.
}
Void main () // Main Function
{
Int A, B, D = 0;
Cout <"############ maze algorithm ##################" <Endl;
Cout <"maze initialization status:" <Endl;
For (A = 1; A <8; A ++) // The output shows the initial state of the maze.
{For (B = 1; B <10; B ++)
{
If (maze [a] [B] =-1)
Cout <"" <maze [a] [B];
Else
Cout <"" <maze [a] [B];
}
Cout <Endl;
}
Seqstack * s;
S = new seqstack; // new stack
Init_seqstack (s );
PATH (S, move, maze );
Cout <"marked as:" <'\ n' <"NOTE: 0 is the start point, → indicates the route, * indicates the exit or the dead path" <Endl;
For (A = 1; A <8; A ++)
{For (B = 1; B <10; B ++)
{
If (maze [a] [B] =-1) // The output arrow when-1 is met
{
Maze [a] [B] = 2; // set the output to 2.
While (d <8) // select the appropriate arrow to output
{
If (maze [A + move [D]. x] [B + move [D]. Y] =-1)
{Switch (d)
{Case 0: cout <"→"; D = 9; break;
Case 1: cout <"; D = 9; break;
Case 2: cout <"break"; D = 9; break;
Case 3: cout <"; D = 9; break;
Case 4: cout <"break"; D = 9; break;
Case 5: cout <"; D = 9; break;
Case 6: cout <"break"; D = 9; break;
Case 7: cout <"; D = 9; break;
}
}
D ++;
}
If (D = 8)
Cout <"*"; // enter "*" when there is no way out or the exit is reached
}
Else
Cout <"" <maze [a] [B]; // enter a normal value.
D = 0; // The value is 0 after the output.
}
Cout <Endl;
}
}

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.