Maze Problem Discussion - (Stack)
I: Method for stacking with labyrinth: Seeking an algorithm from the path from the entrance to the exit to the exit to be described as follows: Set the initial value of the current location as the entry location: DO {If the current location is available, then {will current location Insert the top; if this location is the outlet location, then end; otherwise switch the east neighbor block of the current location is a new current location;} Otherwise, if the stack is not empty and the top position does not have other directions; The new current location is the next phase neighbor block along the top of the stack of clockwise; if the stack is not available, the stack is not easy, then {delete the top position of the stack; if the stack is not empty, then Repeat the top position of the new stack until you find an accessible adjacent or out of the stack to the stack;}} (stack is not empty)
Typedef struct {int order; // channel block in the path "Sequence" postype seat; // channel block in the "coordinate position" INT DI; / / from this channel block to the "direction" of the next channel block} SELEMTYPE; / / Stack element type
Status MazePath (Maztype Maze, Post, Postype End) {// If there is a channel from the entry start to the exit end in the maze maze, you will find a storage in the stack (from the bottom of the stack to the stack), and return true; Otherwise, returning falseinitstack (s); curpos = start; // Set "Current position" is "inlet position" curStep = 1; // Explore the first step Do {if (Pass (curpos) {// The current location can pass That is, there is no passage that has never been passed. Footprint (curpos); // Leave footprint E = (Curstep, Curpos, 1); Push (s, e); // Add path IF (curpos == end) Return (True ); // reach the endpoint (export) curpos = nextpos (CURPOS, 1); // Next is the east of the current location CursteP ; // Explore the next step} else {// Current location cannot pass if (! Stackempty (s) )) {POP (s, e); while (e.di == 4 &&! Stackempty (s)) {markprint (e.seat; POP (s, e)); // Leave a flag that cannot pass, and returns One step} if (e.di <4) {E.DI ; push (s, e); // Replace the direction to explore curpos = nextpos (e.seat, e.di); // Setting the current location is Neighbor Blocks in the New Direction}}}}}}} } While (! Stackempty (s)); return (false);}
Posted on June 26, 2004 2:13 AM
HREF = "/ iampolaris / services / pingback.aspx" Rel = "pingback">
Feedback
# 回 问题 Discussion - (Stack) I have encountered a problem when using C # to realize the maze problem, is using the C # Stack class, but I want to put the Struct structure as an element into the stack, or pop up the top element .
I tried it and saw MSDN's explanation. It seems that the STACK class does not support multiple values to press or pops up. This is understandable, but how to put the struct structure element into or pop up? Who knows what this is? What about the reason?
code show as below:
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Namespace STKMAZE
{
///
/// Form1 summary description.
/// summary>
Public Class Form1: System.Windows.Forms.form
{
Public Int I, J, DIR;
Public const Int maxsize = 100;
Static public int [] movei = new int [] {- 1, -1, 0, 1, 1, 1, 0, -1};
Static public int [] movej = new int = {0, 1, 1, 1, 0, -1, -1, -1};
Static int rows = 5;
Static int columns = 9;
Public bool stopmaze = false;
Public int [,] maze = new int [5,9] {{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1,0,1,0,0,0,1,0,1},
{1, 1, 0, 0, 0, 1, 0, 0, 1},
{1, 10, 1, 1, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}};
Public int Curi = 1, Curj = 1, D = 2, Nexti, Nextj;
Public int [] nextd = new int =
Stack STKI = New Stack ();
Stack STKJ = New Stack ();
Stack STKDIR = New Stack ();
Private system.windows.Forms.TextBox textBox1;
Private system.windows.Forms.Label label1;
Private system.windows.Forms.Button button1;
Private system.windows.Forms.Label label2;
///
/// The required designer variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
//
// Windows Form Designer Support
//
InitializationComponent ();
INT I, J;
String a;
For (i = 0; i <5; i )
{
TextBox1.AppendText ("/ n");
For (j = 0; j <9; j )
{
a = convert.toString (Maze [i, j]);
TextBox1.AppendText (a);
TextBox1.AppendText ("");
}
}
}
//
// Todo: Add any constructor code after the initializeComponent call //
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
This.TextBox1 = new system.windows.Forms.TextBox ();
THIS.LABEL1 = New System.windows.Forms.label ();
This.button1 = new system.windows.Forms.Button ();
THIS.LABEL2 = New System.windows.Forms.label ();
THIS.SUSPENDLAYOUT ();
//
// textbox1
//
This.TextBox1.font = new system.drawing.font ("Song", 12F, System.drawing.FontStyle.Regular, System.drawing.graphicsUnit.Point ((System.byte) (134))));
This.TextBox1.Location = new system.drawing.point (16, 24);
THIS.TEXTBOX1.MULTILINE = TRUE;
THIS.TEXTBOX1.NAME = "TextBox1";
THIS.TEXTBOX1.SIZE = New System.drawing.size (256, 240);
this.TextBox1.tabindex = 0;
THIS.TEXTBOX1.TEXT = "";
This.TextBox1.textAlign = system.windows.forms.horizontalalignment.center;
//
// label1
//
This.Label1.Location = new system.drawing.point (280, 24);
THIS.LABEL1.NAME = "label1";
THIS.Label1.size = new system.drawing.size (80, 200);
THIS.Label1.tabindex = 1;
This.Label1.text = "This M × N (this example 5 × 9) Maze (matrix) outermost is 1, of which 1 means cannot pass, 0 means it can pass. The code has been entered after the labyrinth (matrix) Press the "Walk Maze" button. "
//
// Button1
//
This.Button1.Location = new system.drawing.point (280, 240);
This.button1.name = "button1";
This.button1.size = new system.drawing.size (80, 24); this.button1.tabindex = 2;
This.button1.text = "Walking Maze";
This.Button1.click = new system.eventhandler (this.button1_click);
//
// label2
//
This.Label2.Font = New System.drawing.Font ("Song", 10.5F, System.drawing.FontStyle.bold, System.drawing.graphicsUnit.Point ((System.byte) (134)));
This.Label2.Location = new system.drawing.point (80, 8);
THIS.LABEL2.NAME = "label2";
THIS.Label2.size = new system.drawing.size (136, 16);
THIS.Label2.tabindex = 3;
THIS.Label2.text = "Minister";
THIS.Label2.TextAlign = system.drawing.contentalignment.topcenter;
//
// Form1
//
THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);
THIS.CLIENTSIZE = New System.drawing.size (376, 274);
THIS.CONTROLS.ADD (this.label2);
This.Controls.add (this.button1);
This.Controls.add (this.label1);
This.Controls.add (this.TextBox1);
THIS.NAME = "Form1";
THIS.TEXT = "Ministry of Treatment";
This.ResumeLayout (false);
}
#ndregion
///
/// The main entry point for the application.
/// summary>
[Stathread]
Static void main ()
{
Application.run (New Form1 ());
}
Public Int Wayout (int I, int J, int DD)
{
INT Count, K;
For (k = DD, count = 0; (k <= 7 && count <= 1); k )
{
IF ((i == (ROWS-2)) && (j == (columns-2))))))
Return -1;
IF (Maze [i Movei [K], J Movej [k]] == 0)
{
Nextd [count ] = K;
IF (count == 1)
{
Nexti = i movei [k];
Nextj = J Movej [k];
}
}
}
Return count;
}
// Function Fork feeds branch point information into the stack
Public Void fork (INT I, INT J, INT D)
{
Int Tempi, Tempj, Tempdir
TEMPI = I;
Tempj = j;
Tempdir = D;
STKI.PUSH (TEMPI);
STKJ.PUSH (TEMPJ);
STKDIR.PUSH (TEMPDIR);
}
// Function BACK Executes a backtracking
Public Bool Back (Ref INT IP, Ref Int JP, Ref INT DP)
{Int Tempi, Tempj, Tempdir
IF (STKI.COUNT == 0 && STKJ.COUNT == 0 && stkdir.count == 0)
Return False;
Tempi = (int) STKI.POP ();
Tempj = (int) STKJ.POP ();
Tempdir = (int) STKDIR.POP ();
IP = TEMPI;
JP = TEMPJ;
DP = Tempdir;
Return True;
}
Private void Button1_Click (Object Sender, System.Eventargs E)
{
INT I, J;
String a;
While (! stopmaze)
{
Switch (Wayout (Curi, Curj, D))
{
Case -1:
STOPMAZE = True;
Break;
Case 0:
IF (back (Ref Curi, Ref Curj, Ref D) == FALSE)
{
TextBox1.text = "This labyrinth does not solve";
}
Break;
Case 2:
Fork (Curi, Curj, Nextd [1]); TextBox1.text = "This labyrinth is not available";
Break;
Case 1:
Curi = nexti;
Curj = nextj;
D = 0;
Maze [Curi, Curj] = 8;
CONTINUE;
}
}
For (i = 0; i <5; i )
{
TextBox1.AppendText ("/ n");
For (j = 0; j <9; j )
{
a = convert.toString (Maze [i, j]);
TextBox1.AppendText (a);
TextBox1.AppendText ("");
}
}
}
}
}