Морам да си реплицирам оти неможам да го попрам претходниот пост.
#######
#.#.#.#
#...#.#
#.#...#
#######
<---y---->
^ #######
| #П#.#К#
x #...#.#
| #.#...#
V #######
public class Main {
public static int rez[] = new int[100];
public static int brojac = 0;
public static String niza = "{";
public static String najdiPat(char[][] mapa, int px, int py, int kx, int ky) {
if (px == kx && py == ky) {
for (int r = 0; r < brojac; r = r + 2) {
niza += "(" + rez[r] + "," + rez[r + 1] + ")";
if (r + 2 == brojac) {
niza += "}";
} else {
niza += ",";
}
}
} else {
//dolu
if (mapa[px + 1][py] == ' ') {
px = px + 1;
mapa[px][py] = '?';
rez[brojac] = px;
rez[brojac + 1] = py;
brojac = brojac + 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//desno
if (mapa[px][py + 1] == ' ') {
py = py + 1;
mapa[px][py] = '?';
rez[brojac] = px;
rez[brojac + 1] = py;
brojac = brojac + 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//gore
if (mapa[px - 1][py] == ' ') {
px = px - 1;
mapa[px][py] = '?';
rez[brojac] = px;
rez[brojac + 1] = py;
brojac = brojac + 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//levo
if (mapa[px][py - 1] == ' ') {
py = py - 1;
mapa[px][py] = '?';
rez[brojac] = px;
rez[brojac + 1] = py;
brojac = brojac + 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//levo se vraka
if (mapa[px][py - 1] == '?') {
py = py - 1;
brojac = brojac - 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//desno se vraka
if (mapa[px][py + 1] == '?') {
py = py + 1;
brojac = brojac - 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//dolu se vraka
if (mapa[px + 1][py] == '?') {
px = px + 1;
brojac = brojac - 2;
najdiPat(mapa, px, py, kx, ky);
} else {
//gore se vraka
if (mapa[px - 1][py] == '?') {
px = px - 1;
brojac = brojac - 2;
najdiPat(mapa, px, py, kx, ky);
}
}
}
}
}
}
}
}
}
return niza;
}
public static void main(String[] args) {
char mapa[][] = {{'#', '#', '#', '#', '#', '#', '#'}, {'#', ' ', '#', ' ', '#', ' ', '#'}, {'#', ' ', ' ', ' ', '#', ' ', '#'}, {'#', ' ', '#', ' ', ' ', ' ', '#'}, {'#', '#', '#', '#', '#', '#', '#'}};
int px = 1;
int py = 1;
int kx = 1;
int ky = 5;
rez[brojac] = px;
rez[brojac + 1] = py;
brojac = brojac + 2;
System.out.println(najdiPat(mapa, px, py, kx, ky));
}
}
#include <iostream>
using namespace std;
int broj=0;
const int red=10;
const int kolona=10;
char Lavirint[red][kolona] =
{
{'#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ',' ',' ',' ','#',' ',' ','#'},
{'#',' ','#','#','#',' ','#',' ','#','#'},
{'#','P',' ',' ',' ',' ',' ',' ',' ','#'},
{'#','#','#','#','#',' ','#','#','K','#'},
{'#',' ',' ',' ',' ',' ',' ','#',' ','#'},
{'#',' ','#','#',' ','#','#','#',' ','#'},
{'#',' ',' ',' ','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ',' ',' ','#',' ','#','#'},
{'#','#','#','#','#','#','#','#','#','#'}
};
void cistenje();
void barajLavirint(int, int);
int main()
{
for (int i=0;i<red;i++)
for (int j=0;j<kolona;j++)
if(Lavirint[i][j]=='P')
{
barajLavirint(i,j);
}
cout<<endl;
return 0;
}
void barajLavirint(int row, int col)
{
if( (row>0 && row<red) && (col>0 && col<kolona)) {
if( Lavirint[row][col] == 'K' )
{
cout<<broj<<endl;
}
if((Lavirint[row][col] == ' ')||( Lavirint[row][col] == 'P')){
Lavirint[row][col]='*';
broj++;
barajLavirint(row, col-1);
barajLavirint(row, col+1);
barajLavirint(row+1, col);
barajLavirint(row-1, col);
}
}
}
Следниот пат разгледај ги малку овие решенија.The article presents a simple technique to find the shortest path between two points in a 2D Maze. Similar applications use graphs in such situations but this article shows how this can be done without the headache of graphs. It uses a technique similar to breadth-first search.