How c ++ stack implements a maze

Source: Internet
Author: User

// Cpath. h file

# Include <iostream>
# Include <fstream>
Using namespace STD;

Class cpath // application class
{
Public:
Int M [50] [50];
Void initpath (INT jshux, int jshuy, int qshu );
Int path (INT beginx, int beginy, int endx, int Endy );
Void output (int x, int y );
};

Struct node //
{
Int X;
Int y;
};
Typedef struct stack // chain Stack
{
Int di;
Struct node seat;
Stack * next;
} * Pstack;
Void push (pstack & Top, stack & C)
{
Stack * s = new (stack );
S-> di = C. Di;
S-> seat = C. seat;
S-> next = top;
Top = s;
}
Int POP (pstack & Top, stack & C)
{
If (Top = NULL) return 0;
Stack * P;
C. DI = Top-> Di;
C. Seat = Top-> seat;
P = top;
Top = Top-> next;
Delete (P );
Return 1;
}

// Cpath. cpp File

# Include <iostream>
Using namespace STD;
# Include <iomanip>
# Include "cpath. H"

Void cpath: initpath (INT jshux, int jshuy, int qshu) // initialize the maze
{
Int I, j, X1, Y1;
Int x = jshux;
Int y = jshuy;
For (I = 0; I <X + 2; I ++)
{
M [I] [0] = 0;
M [I] [jshux + 1] = 0;
}
For (I = 0; I <Y + 2; I ++)
{
M [0] [I] = 0;
M [Y + 1] [I] = 0;
}
For (I = 1; I <x + 1; I ++)
For (j = 1; j <Y + 1; j ++)
M [I] [J] = 1;
J = qshu;
Ifstream CCIN ("data.txt", IOS: In );
Cout <"reads the number of lines and columns (Y // n) of each internal wall in the maze from data.txt )";
Char P;
Cin> P;
If (P = 'y ')
{
For (I = 1; I <= J; I ++)
{
CCIN> x1> Y1;
M [X1] [Y1] = 0;
}
}
Else
{
Cout <"manually enter the number of lines and columns of each internal wall in the Maze :";
For (I = 1; I <= J; I ++)
{
CCIN> x1> Y1;
M [X1] [Y1] = 0;
}
}
}
Int cpath: path (INT beginx, int beginy, int endx, int Endy)
{
Int CCX, CCY;
Int MHD [] = {0, 1, 0,-1 };
Int dtj [] = {1, 0,-1, 0 };
CCX = beginx;
CCY = beginy;
Struct stack * s = new (stack );
S-> next = NULL;
Struct stack E;
E. Next = NULL;
Int step = 1;
Do
{
If (M [CCX] [CCY] = 1)
{
M [CCX] [CCY] = step;
E. Seat. x = CCX;
E. Seat. Y = CCY;
E. DI = 0;
Push (S, e );
Step ++;
If (CCX = endx & CCY = Endy) return 1;
CCX + = MHD [E. Di];
CCY + = dtj [E. Di];
}
Else
{
If (S-> next)
{
Pop (S, e );
Step --;
While (E. DI = 3 & S-> next)
{
M [E. Seat. x] [E. Seat. Y] =-1;
Pop (S, e );
Step --;
}
If (E. Di <3)
{
E. Di ++;
Push (S, e );
Step ++;
CCX = E. Seat. X;
CCY = E. Seat. Y;
CCX + = MHD [E. Di];
CCY + = dtj [E. Di];
}
}
}
} While (S-> next );
Return 0;
}
Void cpath: output (int x, int y)
{
For (INT I = 0; I <X + 2; I ++)
{
For (Int J = 0; j <Y + 2; j ++)
Cout <SETW (3) <m [I] [J];
Cout <Endl;
}
}
Int main ()
{
Int jshux, jshuy, qshu, beginx, beginy, endx, Endy;
Cout <"jshux and jshuy :";
Cin> jshux> jshuy;
Cout <"qshu :";
Cin> qshu;
 
Cpath mmm;
Mmm. initpath (jshux, jshuy, qshu );
Cout <"Maze:" <Endl;
Mmm. Output (jshux, jshuy );
Cout <"beginx and beginy :";
Cin> begy> beginy;
Cout <"endx and Endy :";
Cin> endx> Endy;
If (MMM. Path (beginx, beginy, endx, Endy ))
{
Cout <"route:" <Endl;
Mmm. Output (jshux, jshuy );
}
Else cout <"no path! ";
Return 0;
}

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.