Home java The Wave algorithm. Running waves

# The Wave algorithm. Running waves

Author

Date

Category

It is necessary to write the wave algorithm. It does not get me an empty wave. Trying to put it with the help of recursion. The code works, but it shows only one shortest path or do not understand that the show)
Via recursion reaches the boundaries of the array (right), then I do not understand how to make the steps back to the very beginnings and recursion went in the opposite direction (down, up).

``````public void findPath (int y, int x, int d) {
mas [y] [x] = d;
for (int i = y - 1; i & lt; = y + 1; i ++) {
for (int j = x - 1; j & lt; x + 1; j ++) {
if (x & lt; width - 1 & amp; & amp; mas [y] [x + 1] == 0) {
findPath (y, x + 1, d + 1);
}
if (y & gt; 0 & amp; & amp; mas [y - 1] [x] == 0) {
findPath (y - 1, x, d + 1);
}
if (y & lt; height & amp; & amp; mas [y + 1] [x] == 0) {
findPath (y + 1, x, d + 1);
}
if (x & gt; 0 & amp; & amp; mas [y] [x - 1] == 0) {
findPath (y, x - 1, d + 1);
}
}
}
}
``````

What you have written is not related to the search in width, and in depth, too. To find the shortest path you want to use breadth-first search based on a queue. As the queue, you can use `ArrayList `sample code (only fixed).

``````public void findPath (int x, int y) {
ArrayList & lt; Pair & lt; Integer, Integer & gt; & gt; queue = new ArrayList & lt; & gt; ();
queue.add (new Pair & lt; Integer, Integer & gt; (x, y));
mas [x] [y] = 1;
while (queue.size () & gt; 0) {
Pair & lt; Integer, Integer & gt; cur = queue.remove (queue.size () - 1);
int x = cur.x;
int y = cur.y;
//
if (x & lt; width - 1 & amp; & amp; mas [x + 1] [y] == 0) {
queue.add (new Pair & lt; Integer, Integer & gt; (x + 1, y);
mas [x + 1] [y] = 1;
}
if (x & gt; 0 & amp; & amp; mas [x - 1] [y] == 0) {
queue.add (new Pair & lt; Integer, Integer & gt; (x-1, y);
mas [x-1] [y] = 1;
}
if (y & lt; height - 1 & amp; & amp; mas [x] [y + 1] == 0) {
queue.add (new Pair & lt; Integer, Integer & gt; (x, y + 1);
mas [x] [y + 1] = 1;
}
if (y & gt; 0 & amp; & amp; mas [x] [y-1] == 0) {
queue.add (new Pair & lt; Integer, Integer & gt; (x, y - 1);
mas [x] [y-1] = 1;
}
//
}
}
``````

By the way you have a cereal index is complete. But I think I will be able to finish.

An example in C #. The object model. GitHub

Code:

``````using System;
Using System.Linq;
Using System.Collections.Genic;
namespace Finder
{
public class LeeAlgorithm
{
public int [,] ArrayGraph {get; private set; }
/// & LT; Summary & GT;
Home /// path from the end of the list
/// & LT; / Summary & GT;
public List & lt; Tuple & lt; int, int & gt; & gt; Path {get; private set; }
public int Width {get; private set; }
public int Heidth {get; private set; }
public bool PathFound {get; private set; }
public int LengthPath {get {return Path.Count; }}
private int _step;
private bool _finishingCellMarked;
private int _finishPointI;
private int _finishPointJ;
/// & LT; Summary & GT;
/// Initializes a new instance of the object field and indicating the starting point
/// & LT; / Summary & GT;
public LeeAlgorithm (int startX, int startY, int [,] array)
{
ArrayGraph = array;
Width = ArrayGraph.GetLength (0);
Heidth = ArrayGraph.GetLength (1);
SetStarCell (startX, startY);
PathFound = PathSearch ();
}
/// & LT; Summary & GT;
/// Initializes a new instance of the field. Starting point is set in the array
/// & LT; / Summary & GT;
Public Leealgorithm (int [,] Array)
{
ArrayGraph = Array;
Width = arraygraph.getLength (0);
Heidth = ArrayGraph.getLength (1);
INT STARTX;
Int Starty;
Findstartcell (Out Startx, Out Starty);
SetStarcell (StartX, Starty);
PathFound = Pathsearch ();
}
Private Void Findstartcell (Out Int Startx, Out Int Starty)
{
int w = width;
int h = heidth;
for (int i = 0; i & lt; w; i ++)
{
for (int j = 0; j & lt; h; j ++)
{
if (Arraygraph [i, j] == (int) figures.startposition)
{
startx = i;
starty = j;
Return;
}
}
}
Throw New AggregateException ("No starting point");
}
Private Void SetStarcell (Int Startx, Int Starty)
{
ifx & gt; this.Arraygraph.getLength (0) || startx & lt; 0)
Throw New ArgumentException ("Incorrect X Coordinate");
If (Starty & GT; This.Arraygraph.getLength (1) || Starty & LT; 0)
Throw New ArgumentException ("Incorrect X Coordinate");
// Mark the starting cell D: = 0
_step = 0;
ArrayGraph [startx, starty] = _STEP;
}
Private Bool Pathsearch ()
{
If (WavePropagation ())
if (restorepath ())
RETURN TRUE;
RETURN FALSE;
}
// There is. Orthogonal way
// TODO. Orthogonal diagonal path
/// & LT; Summary & GT;
/// Wave distribution
/// & LT; / Summary & GT;
/// & LT; Returns & gt; & lt; / returns & gt;
Private Bool WavePropagation ()
{
//CYCLE
// For each cell LOC marked by the number D
// Mark all the neighboring free intimate cells by the number D + 1
// KC.
// D: = D + 1
// While (the finish cell is not labeled) and (there is the possibility of spreading the wave)
int w = width;
int h = heidth;
BOOL FINISHED = FALSE;
do.
{
for (int i = 0; i & lt; w; i ++)
{
for (int j = 0; j & lt; h; j ++)
{
if (ArrayGraph [i, j] == _STEP)
{
// Mark all the neighboring free intimate cells by the number D + 1
If (i! = W - 1)
if (ArrayGraph [i + 1, J] == (int) figures.emptyspace) arraygraph [i + 1, j] = _step + 1;
IF (J! = H - 1)
if (ArrayGraph [i, j + 1] == (int) figures.emptyspace) arraygraph [i, j + 1] = _step + 1;
if (i! = 0)
if (ArrayGraph [i - 1, j] == (int) figures.emptyspace) arraygraph [i - 1, j] = _step + 1;
if (j! = 0)
if (arraygraph [i, j - 1] == (int) figures.emptyspace) ArrayGraph [i, j - 1] = _step + 1;
// Path to the finish line is laid
If (I & LT; W - 1)
if (Arraygraph [i + 1, j] == (int) figures.destination)
{
_finishpointi = i + 1;
_finishpointj = j;
FINISHED = TRUE;
}
IF (J & LT; H - 1)
if (ArrayGraph [i, j + 1] == (int) figures.destination)
{
_finishpointi = i;
_finishpointj = j + 1;
FINISHED = TRUE;
}
if (I & GT; 0)
if (Arraygraph [i - 1, j] == (int) figures.destination)
{
_finishpointi = i - 1;
_finishpointj = j;
FINISHED = TRUE;
}
if (j & gt; 0)
If (Arraygraph [i, J - 1] == (int) figures.destination)
{
_finishpointi = i;
_finishpointj = j - 1;
FINISHED = TRUE;
}
}
}
}
_STEP ++;
// While (the finish cell is not labeled) and (there is a possibility of spreading the wave)
} While (! FINISHED & AMP; & amp; _STEP & lt; w * h);
_finishingcellmarked = finished;
Return finished;
}
/// & LT; Summary & GT;
/// Restoration of the Path
/// & LT; / Summary & GT;
/// & LT; Returns & gt; & lt; / returns & gt;
Private Bool RestorePath ()
{
// If the finish cell is marked
// THEN
// Go to the finish cell
// Cycle
// Select among the adjacent cell marked with a number 1 less than the number in the current cell
// Go to the selected cell and add it to the path
// While the current cell is not starting
// Return the path found
// OTHERWISE
if (! _finishingcellmarked)
RETURN FALSE;
int w = width;
int h = heidth;
int i = _finishpointi;
int j = _finishpointj;
Path = new list & lt; TUPLE & LT; INT, INT & GT; & GT; ();
do.
{
If (I & LT; W - 1)
if (arraygraph [i + 1, j] == _STEP - 1)
{
}
IF (J & LT; H - 1)
if (ArrayGraph [i, j + 1] == _STEP - 1)
{
}
if (I & GT; 0)
if (ArrayGraph [i - 1, j] == _STEP - 1)
{
}
if (j & gt; 0)
if (ArrayGraph [i, j - 1] == _STEP - 1)
{
}
_Step--;
} While (_STEP! = 0);
RETURN TRUE;
}
Private Void Addtopath (Int X, Int Y)
{
Path.add (New Tuple & LT; INT, INT & GT; (X, Y));
}
}
Public Enum Figures.
{
Startposition = 0,
Emptyspace = -1,
Destination = -2,
Path = -3,
Barrier = -4.
}
Class Program
{
Static Void Main (String [] Args)
{
Const int heigth = 10;
Const int width = 18;
int [,] my = new int [heigth, width];
For (int i = 0; i & lt; heigth; i ++)
{
for (int j = 0; j & lt; width; j ++)
{
Var Rand = New Random (Unchecked (int) datetime.now.millisecond));
if (rand.next (100) & gt; 70)
My [i, j] = (int) figures.barrier;
ELSE.
My [i, j] = (int) figures.emptyspace;
}
}
Var Random = New Random (Unchecked ((int) datetime.now.millisecond));
My [Random.Next (Heigth), random.next (width)] = (int) figures.startposition;
My [Random.Next (Heigth), Random.Next (Width)] = (int) figures.destination;
Print (MY);
Leealgorithm Li = New Leealgorithm (My);
Console.Writeline (Li.pathfound);
if (li.pathfound)
{
Foreach (Var Item in Li.path)
{
if (Item == li.path.last ())
My [Item.Item1, Item.Item2] = (int) figures.startposition;
ELSE If (Item == Li.Path.first ())
My [Item.Item1, Item.Item2] = (int) figures.destination;
ELSE.
My [Item.Item1, Item.Item2] = (int) figures.path;
}
Print (Li.ArrayGraph);
Console.WriteLine ("Length" + Li.LengThpath);
}
ELSE.
}
Private Static Void Print (int [, Array)
{
Console.WriteLine ("***");
String MSG = string.empty;
int x = array.getlength (0);
int y = array.getlength (1);
for (int i = 0; i & lt; x; i ++)
{
for (int j = 0; j & lt; y; j ++)
{
Switch (Array [i, J])
{
Case (int) figures.path: msg = string.format ("{0,3}", "+"); Console.ForegroundColor = Consolecolor.Yellow; Break;
Case (int) figures.startposition: MSG = String.Format ("{0,3}", "s"); Console.ForeGroundColor = Consolecolor.green; Break;
Case (int) figures.destination: MSG = String.Format ("{0.3}", "D"); Console.ForegroundColor = consolecolor.red; Break;
Case (int) figures.emptyspace: msg = string.format ("{0.3}", "'"); Console.ForeGroundColor = consolecolor.darkblue; Break;
Case (int) figures.barrier: MSG = String.Format ("{0.3}", "*"); Console.ForeGroundColor = Consolecolor.Blue; Break;
Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
Case 6:
Case 7:
Case 8:
Case 9:
Case 10:
Case 11:
Case 12:
Case 13:
Case 14:
Case 15:
Case 16:
Case 17:
Case 18:
Case 19:
Case 20:
msg = string.format ("{0.3}", array [i, j]); Console.ForegroundColor = consolecolor.darkgray; Break;
Default:
Break;
}
Console.Write (MSG);
Console.resetcolor ();
}
Console.Writeline ();
}
Console.WriteLine (MSG);
}
}
}
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.