Article View: pl.comp.programming
Article #218010please help
From: F. KAHEN
Date: Tue, 10 Feb 1998 00:03
Date: Tue, 10 Feb 1998 00:03
293 lines
10270 bytes
10270 bytes
This a C++ school assignment that has to be done ASAP. Please let me
now if you know how.
Thank you
Farid@spotcom.com
P r o g r a m T W O
CSCI-102L
Spring 1998
NO GLOBAL VARIABLES ARE ALLOWED IN ANY PROGRAM WRITTEN FOR
THIS COURSE. Thirty points will be deducted from any program
containing even ONE global variable.
You are to develop a program that solves a maze. Your program is to find
a path through the maze if one exists. If a path exists, you are to
display the maze with the path marked with x's. If a path does not
exist, you are to display a message that a path through the maze does
not exist. The start of the maze path will always be at the upper left-
hand corner of the maze (i.e. row 1, column 1 of the maze). The end of
the path (if the maze has a path through it) will always be at the lower
right-hand corner of the maze.
The input data for the program will be read from a file named "indata2"
(which is available on the web page for the class).
The contents of this file will be as follows:
A set of lines in the data file will provide the input data for one
maze in the following manner:
Line one of the set of lines will contain two integer values. The
first represents the number of "rows" in the maze, and the second
represents the number of "columns" in the maze.
If "r" is the integer value read as the "rows" specifier above,
then read "r" more lines of data from the data file. These "r"
lines will contain the '0' and '1' characters which define the
maze itself.
Example: The data file might contain the following
lines of data:
5 7 < {maze is five rows by seven columns}
0111101 -|
1000011 |
1011111 | These 1's and 0's define the maze
1100111 |
1110000 -|
When your program has found the path through this maze, it should
output the maze with the path marked by x's as shown below (Note that
the "dead-end" remains marked with zeros, and the wall around the
maze is shown):
111111111
1x1111011
11x000111 << Note that the "wall" around >>
11x111111 << the maze is to be displayed >>
111xx1111 << enclosing the solved maze. >>
11111xxx1
111111111
The data file may contain data for more than one maze in which case
your program must solve each of the mazes contained in data file
"indata2".
Your program will require four data objects including:
1. A 22 by 22 two-dimensional array of CHARACTERS to contain the
maze read from file "indata2". Each element of this matrix will
initially contain either a '1' or a '0' (call this array "maze").
2. A 20 by 20 two-dimensional array of INTEGERS to mark the
locations in the maze that have already been visited (call this
array "maze_mark").
3. An ARRAY-BASED STACK of structures that will specify the maze
elements that comprise the path through the maze (if one exists).
Another data item associated with this "stack" is an integer
variable named "stk_top" which stores the index of the current
top-of-stack position. The stack structures should be created
as:
#define MAX_PATH 120;
enum dir_type {N,NE,E,SE,S,SW,W,NW,NONE};
struct path_stk_type
{
unsigned int row,col;
dir_type dir;
};
path_stk_type path_stk[MAX_PATH];
int stk_top;
Note the use of TYPE "enum". Study how to define "enumeration"
data types and how to declare and use enumeration variables. You
will find this topic in your C++ language text.
4. A one-dimensional array of structures that is used to determine
the next location in the maze to visit in an effort to find the
path through the maze:
struct move_type
{
short int a,b;
};
move_type move[NONE]; /* NONE is an enum element */
/* declared above. */
Use the following table to assign values to the elements of
"move":
move[N].a=(-1); move[N].b=0;
move[NE].a=(-1); move[NE].b=1;
move[E].a=0; move[E].b=1;
move[SE].a=1; move[SE].b=1;
move[S].a=1; move[S].b=0;
move[SW].a=1; move[SW].b=(-1);
move[W].a=0; move[W].b=(-1);
move[NW].a=(-1); move[NW].b=(-1);
Make direct assignments of values to "move". DO NOT read values
into "move" from a file.
Use the following algorithm to construct your program. Remember, each
action listed in "main" should be carried out through a call to a
function.
void main(void)
{
// Declare and local variables or constants needed.
while(!fobj.eof()) //fobj is your file object
{
Call a function to initialize local variables.
Read the data from "indata2" for the next maze problem.
Display the maze just read with wall around it shown.
Find the path through the maze (function find_path(...))
Display the solved maze (with path shown in x's and the
wall around it shown) or if no path exists, display
no-path message.
}
} // main
find_path(..list of parameters..)
{
Declare and initialize local variables as needed.
char path_found=FALSE;
int cur_row=1, cur_col=1;
Set the ("cur_row-1","cur_col-1") element of "mazemark"
to '1'.
dir_type dir=N;
stk_top=(-1);
push(path_stk,stk_top,row,col,dir);
while(stk_top is greater than -1 and "path_found" is false)
{
pop(path_stk,stk_top,row,col,dir);
while("dir" is less than or equal to "northwest=7" and
"path_found" is false)
{
next_row=cur_row + move[dir].a
next_col=cur_col + move[dir].b
if(values of "cur_row" and "cur_col" signify
that a path has been found)
{
path_found=TRUE
push(path_stk,stk_top,cur_row,cur_col,dir)
} // if
else if("next_row","next_col" specifies a hallway)
if(Mouse not in this part of the hallway before)
{
Set appropriate element in "mazemark" to specify
that cur_row, cur_col has been visited.
push(path_stk,stk_top,row,col,dir+NE)
cur_row=next_row
cur_col=next_col
dir=N;
} // if
else
dir=(dir_type)(dir + NE);
else
dir=(dir_type)(dir + NE);
} // inner while
} // outer while
if("path_found" is false)
Return a "no path exists" value.
else
Return a "path exists" value.
} // find_path
Be sure each maze solution has a header specifying which maze problem is
being solved (e.g. 1, 2, 3, etc).
The following two functions can be used to "push" and "pop" values
onto/from the stack of structures referenced by "path_stk"
void push(stack,&top,row,col,dir)
{
++top;
stack[top].row=row;
stack[top].col=col;
stack[top].dir=dir;
} /* push */
void pop(stack,&top,&row,&col,&dir)
{
row=stack[top].row;
col=stack[top].col;
dir=stack[top].dir;
--top;
} /* pop */
<html><head><title>Class notes on C++</title><head>
<body>
<xmp>
5 8
00111001
11010110
11010110
11010110
11101110
7 10
0110000001
1001111110
0111110111
1000101010
1111011101
1111011111
0000100000
20 20
00000001111100000001
11111110111011111110
11111111011000011110
11110111011011100110
11110110101011111010
11111001101011000111
11111111011010111000
10000000110101110111
01111111101110011111
01111000011111101111
10000111111110011111
11111111111101111111
11111111111011111111
10000000000111111111
01111111111100000000
01111111011101111110
10000000111100100110
01111111011111011110
01111111111111011111
10000000000000110000
20 20
00000001111100000001
11111110111011111110
11111111011000011110
11110111011011100110
11110110101011111010
11111001101011000111
11111111011010111000
10000000110101110111
01111111101110011111
01111000011111101111
10000111111110011111
11111111111101111111
11111111111011111111
10000000000111111111
01111111111100000000
01111111011101111110
10000000111100100110
01111111011111011110
01111111111111011111
10000000000000100000
Technical Details
Message-ID:
<34DF9950.D721BD9B@SPOTCOM.COM>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!cpk-news-hub1.bbnplanet.com!news.bbnplanet.com!newsfeed.internetmci.com!206.250.118.17!nntp.earthlink.net!usenetShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!cpk-news-hub1.bbnplanet.com!news.bbnplanet.com!newsfeed.internetmci.com!206.250.118.17!nntp.earthlink.net!usenet From: "F. KAHEN" <FARID@SPOTCOM.COM> Newsgroups: microsoft.public.win32.programmer.kernel,microsoft.public.win32.programmer.mapi,microsoft.public.win32.programmer.mmedia,microsoft.public.win32.programmer.networks,microsoft.public.win32.programmer.ole,microsoft.public.win32.programmer.pen,microsoft.public.win32.programmer.tapi,microsoft.public.win32.programmer.tools,microsoft.public.win32.programmer.ui,microsoft.public.win32.programmer.wince,microsoft.public.word.programming,nl.comp.programmeren,no.slekt.programmer,pl.comp.nowe-programy,pl.comp.programming Subject: please help Date: Mon, 09 Feb 1998 16:03:29 -0800 Organization: COMPUTER SERVICES Lines: 294 Message-ID: <34DF9950.D721BD9B@SPOTCOM.COM> Reply-To: FARID@SPOTCOM.COM NNTP-Posting-Host: 153.37.178.201 Mime-Version: 1.0 X-Mailer: Mozilla 4.04 [en] (Win95; I) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit