Write a code in java. the following data structures. The maze is a checkerboard,
ID: 3612864 • Letter: W
Question
Write a code in java.the following data structures. The maze is a checkerboard, in whichsome of the
squares are blocked. Like this:
<pre>
static final int size = 5;
static final int wall = 1;
static final int open = 0;
static int[][] walls = {
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0 }};
static final int dest_row = 4;
static final int dest_column = 0;
</pre>
The maze is square and "size" is the number of rows and columns.If
walls[r][c] == 1, the square at row and column c is blocked by awall;
if walls[r][c] == 0, the square at row r and column c is open.
The problem is to find a list of squares that starts at (0,0) andends
at (dest_row,dest_column), subject to two constraints. First, everysquare on
the path is open. Second, we can only move up down left or right.To represent
a square, I suggest the following easy data structure:
<pre>
class square {
int row;
int column;
square(int row1, int column1)
{
row=row1;
column = column1;
}
}
</pre>
To represent the path I suggest a linked list of squares:
<pre>
class path {
square first;
path rest;
path(square first1,path rest1)
{
first = first1;
rest = rest1;
}
}
</pre>
We keep the linked list in backwards order: the square we are atnow is the
first square in the list, and the square where we started is thelast
square in the list. This makes it easy to move one square forward-- just
add that square to the front of the list.
We need to check that it is OK to move forward to a given square(r,c). This
means that 0 <= r <= size-1 and 0 <= c <= size-1 (wehaven't stepped off
the edge), walls[r][c] == 0 (we haven't walked through a wall) and(r,c)
is not on the path (we aren't going in circles). I suggest thefollowing
methods.
<pre>
static boolean on_path(square sq1, path p)
</pre>
on_path(sq1,p) returns true if square sq1 appears on the path p.This
is easy, you can decide between recursion and a loop.
<pre>
boolean ok(square sq1, path p)
</pre>
ok(sq1,p) returns true if square sq1 satisfies the OK conditionslisted
above. Obviously "ok" calls "on_path".
You also need a loop that visits all four neighboring squares.Note
the neighbors of square (r,c) are (r+1,c+0),(r-1,c+0),(r+0,c+1)and(r+0,c-1).
Put the row and colum offsets into two arrays:
<pre>
static int[] row_delta = { 0, 0, 1, -1 };
static int[] column_delta = { 1, -1, 0, 0 };
</pre>
As i ranges from 0 to 3, the expression(r+row_delta[i],c+column_delta[i])
ranges over the four neighbors of square (r,c).
Now we come to the most important function:
<pre>
void search(path p)
</pre>
First check to see if the current square (the first square on
path p) is equal to the destination. If so, print the current path.If not,
run a loop that visits the four neighbors of the current square. Ifsq1
is a neighbor and ok(sq1,p) is true, add sq1 to the front of path pand
call "search" with the new path. This function will print everypath
that extends path p and leads to the destination. Your "main"function
will call "search" with a trivial path containing only the
starting square.
Here is a gift: a method that prints the maze, including thepath.
An open square appears as a blank; a blocked square is a *; and asquare on the
path is a +.
<pre>
static void show_path(path p)
{
for(int row1 = 0; row1 < size; row1++)
{
/* Print one row of the maze. */
for(int column1 = 0; column1 < size; column1++)
if(on_path(new square(row1,column1),p))
System.out.print('+');
else if (walls[row1][column1] == wall)
System.out.print('*');
else System.out.print(' ');
/* Move cursor to start of next row. */
System.out.print(" ");
}
console.nextLine();
System.out.print(" ");
} Write a code in java.
the following data structures. The maze is a checkerboard, in whichsome of the
squares are blocked. Like this:
<pre>
static final int size = 5;
static final int wall = 1;
static final int open = 0;
static int[][] walls = {
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0 }};
static final int dest_row = 4;
static final int dest_column = 0;
</pre>
The maze is square and "size" is the number of rows and columns.If
walls[r][c] == 1, the square at row and column c is blocked by awall;
if walls[r][c] == 0, the square at row r and column c is open.
The problem is to find a list of squares that starts at (0,0) andends
at (dest_row,dest_column), subject to two constraints. First, everysquare on
the path is open. Second, we can only move up down left or right.To represent
a square, I suggest the following easy data structure:
<pre>
class square {
int row;
int column;
square(int row1, int column1)
{
row=row1;
column = column1;
}
}
</pre>
To represent the path I suggest a linked list of squares:
<pre>
class path {
square first;
path rest;
path(square first1,path rest1)
{
first = first1;
rest = rest1;
}
}
</pre>
We keep the linked list in backwards order: the square we are atnow is the
first square in the list, and the square where we started is thelast
square in the list. This makes it easy to move one square forward-- just
add that square to the front of the list.
We need to check that it is OK to move forward to a given square(r,c). This
means that 0 <= r <= size-1 and 0 <= c <= size-1 (wehaven't stepped off
the edge), walls[r][c] == 0 (we haven't walked through a wall) and(r,c)
is not on the path (we aren't going in circles). I suggest thefollowing
methods.
<pre>
static boolean on_path(square sq1, path p)
</pre>
on_path(sq1,p) returns true if square sq1 appears on the path p.This
is easy, you can decide between recursion and a loop.
<pre>
boolean ok(square sq1, path p)
</pre>
ok(sq1,p) returns true if square sq1 satisfies the OK conditionslisted
above. Obviously "ok" calls "on_path".
You also need a loop that visits all four neighboring squares.Note
the neighbors of square (r,c) are (r+1,c+0),(r-1,c+0),(r+0,c+1)and(r+0,c-1).
Put the row and colum offsets into two arrays:
<pre>
static int[] row_delta = { 0, 0, 1, -1 };
static int[] column_delta = { 1, -1, 0, 0 };
</pre>
As i ranges from 0 to 3, the expression(r+row_delta[i],c+column_delta[i])
ranges over the four neighbors of square (r,c).
Now we come to the most important function:
<pre>
void search(path p)
</pre>
First check to see if the current square (the first square on
path p) is equal to the destination. If so, print the current path.If not,
run a loop that visits the four neighbors of the current square. Ifsq1
is a neighbor and ok(sq1,p) is true, add sq1 to the front of path pand
call "search" with the new path. This function will print everypath
that extends path p and leads to the destination. Your "main"function
will call "search" with a trivial path containing only the
starting square.
Here is a gift: a method that prints the maze, including thepath.
An open square appears as a blank; a blocked square is a *; and asquare on the
path is a +.
<pre>
static void show_path(path p)
{
for(int row1 = 0; row1 < size; row1++)
{
/* Print one row of the maze. */
for(int column1 = 0; column1 < size; column1++)
if(on_path(new square(row1,column1),p))
System.out.print('+');
else if (walls[row1][column1] == wall)
System.out.print('*');
else System.out.print(' ');
/* Move cursor to start of next row. */
System.out.print(" ");
}
console.nextLine();
System.out.print(" ");
}
the following data structures. The maze is a checkerboard, in whichsome of the
squares are blocked. Like this:
<pre>
static final int size = 5;
static final int wall = 1;
static final int open = 0;
static int[][] walls = {
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0 }};
static final int dest_row = 4;
static final int dest_column = 0;
</pre>
The maze is square and "size" is the number of rows and columns.If
walls[r][c] == 1, the square at row and column c is blocked by awall;
if walls[r][c] == 0, the square at row r and column c is open.
The problem is to find a list of squares that starts at (0,0) andends
at (dest_row,dest_column), subject to two constraints. First, everysquare on
the path is open. Second, we can only move up down left or right.To represent
a square, I suggest the following easy data structure:
<pre>
class square {
int row;
int column;
square(int row1, int column1)
{
row=row1;
column = column1;
}
}
</pre>
To represent the path I suggest a linked list of squares:
<pre>
class path {
square first;
path rest;
path(square first1,path rest1)
{
first = first1;
rest = rest1;
}
}
</pre>
We keep the linked list in backwards order: the square we are atnow is the
first square in the list, and the square where we started is thelast
square in the list. This makes it easy to move one square forward-- just
add that square to the front of the list.
We need to check that it is OK to move forward to a given square(r,c). This
means that 0 <= r <= size-1 and 0 <= c <= size-1 (wehaven't stepped off
the edge), walls[r][c] == 0 (we haven't walked through a wall) and(r,c)
is not on the path (we aren't going in circles). I suggest thefollowing
methods.
<pre>
static boolean on_path(square sq1, path p)
</pre>
on_path(sq1,p) returns true if square sq1 appears on the path p.This
is easy, you can decide between recursion and a loop.
<pre>
boolean ok(square sq1, path p)
</pre>
ok(sq1,p) returns true if square sq1 satisfies the OK conditionslisted
above. Obviously "ok" calls "on_path".
You also need a loop that visits all four neighboring squares.Note
the neighbors of square (r,c) are (r+1,c+0),(r-1,c+0),(r+0,c+1)and(r+0,c-1).
Put the row and colum offsets into two arrays:
<pre>
static int[] row_delta = { 0, 0, 1, -1 };
static int[] column_delta = { 1, -1, 0, 0 };
</pre>
As i ranges from 0 to 3, the expression(r+row_delta[i],c+column_delta[i])
ranges over the four neighbors of square (r,c).
Now we come to the most important function:
<pre>
void search(path p)
</pre>
First check to see if the current square (the first square on
path p) is equal to the destination. If so, print the current path.If not,
run a loop that visits the four neighbors of the current square. Ifsq1
is a neighbor and ok(sq1,p) is true, add sq1 to the front of path pand
call "search" with the new path. This function will print everypath
that extends path p and leads to the destination. Your "main"function
will call "search" with a trivial path containing only the
starting square.
Here is a gift: a method that prints the maze, including thepath.
An open square appears as a blank; a blocked square is a *; and asquare on the
path is a +.
<pre>
static void show_path(path p)
{
for(int row1 = 0; row1 < size; row1++)
{
/* Print one row of the maze. */
for(int column1 = 0; column1 < size; column1++)
if(on_path(new square(row1,column1),p))
System.out.print('+');
else if (walls[row1][column1] == wall)
System.out.print('*');
else System.out.print(' ');
/* Move cursor to start of next row. */
System.out.print(" ");
}
console.nextLine();
System.out.print(" ");
}
the following data structures. The maze is a checkerboard, in whichsome of the
squares are blocked. Like this:
<pre>
static final int size = 5;
static final int wall = 1;
static final int open = 0;
static int[][] walls = {
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0 }};
static final int dest_row = 4;
static final int dest_column = 0;
</pre>
The maze is square and "size" is the number of rows and columns.If
walls[r][c] == 1, the square at row and column c is blocked by awall;
if walls[r][c] == 0, the square at row r and column c is open.
The problem is to find a list of squares that starts at (0,0) andends
at (dest_row,dest_column), subject to two constraints. First, everysquare on
the path is open. Second, we can only move up down left or right.To represent
a square, I suggest the following easy data structure:
<pre>
class square {
int row;
int column;
square(int row1, int column1)
{
row=row1;
column = column1;
}
}
</pre>
To represent the path I suggest a linked list of squares:
<pre>
class path {
square first;
path rest;
path(square first1,path rest1)
{
first = first1;
rest = rest1;
}
}
</pre>
We keep the linked list in backwards order: the square we are atnow is the
first square in the list, and the square where we started is thelast
square in the list. This makes it easy to move one square forward-- just
add that square to the front of the list.
We need to check that it is OK to move forward to a given square(r,c). This
means that 0 <= r <= size-1 and 0 <= c <= size-1 (wehaven't stepped off
the edge), walls[r][c] == 0 (we haven't walked through a wall) and(r,c)
is not on the path (we aren't going in circles). I suggest thefollowing
methods.
<pre>
static boolean on_path(square sq1, path p)
</pre>
on_path(sq1,p) returns true if square sq1 appears on the path p.This
is easy, you can decide between recursion and a loop.
<pre>
boolean ok(square sq1, path p)
</pre>
ok(sq1,p) returns true if square sq1 satisfies the OK conditionslisted
above. Obviously "ok" calls "on_path".
You also need a loop that visits all four neighboring squares.Note
the neighbors of square (r,c) are (r+1,c+0),(r-1,c+0),(r+0,c+1)and(r+0,c-1).
Put the row and colum offsets into two arrays:
<pre>
static int[] row_delta = { 0, 0, 1, -1 };
static int[] column_delta = { 1, -1, 0, 0 };
</pre>
As i ranges from 0 to 3, the expression(r+row_delta[i],c+column_delta[i])
ranges over the four neighbors of square (r,c).
Now we come to the most important function:
<pre>
void search(path p)
</pre>
First check to see if the current square (the first square on
path p) is equal to the destination. If so, print the current path.If not,
run a loop that visits the four neighbors of the current square. Ifsq1
is a neighbor and ok(sq1,p) is true, add sq1 to the front of path pand
call "search" with the new path. This function will print everypath
that extends path p and leads to the destination. Your "main"function
will call "search" with a trivial path containing only the
starting square.
Here is a gift: a method that prints the maze, including thepath.
An open square appears as a blank; a blocked square is a *; and asquare on the
path is a +.
<pre>
static void show_path(path p)
{
for(int row1 = 0; row1 < size; row1++)
{
/* Print one row of the maze. */
for(int column1 = 0; column1 < size; column1++)
if(on_path(new square(row1,column1),p))
System.out.print('+');
else if (walls[row1][column1] == wall)
System.out.print('*');
else System.out.print(' ');
/* Move cursor to start of next row. */
System.out.print(" ");
}
console.nextLine();
System.out.print(" ");
}
Explanation / Answer
0m
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.