#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
////////////////////////////////
struct XY
{
int x;
int y;
};
////////////////////////////////
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
void Coord(int a,int b)
{
if (INVALID_HANDLE_VALUE != hConsole)
{
COORD pos = { a,b };
SetConsoleCursorPosition(hConsole, pos);
}
}
void Coord(XY a)
{
if (INVALID_HANDLE_VALUE != hConsole)
{
COORD pos = { a.x ,a.y };
SetConsoleCursorPosition(hConsole, pos);
}
}
void SetColor(int text, int background)
{
SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
}
////////////////////////////////
struct Dot
{
XY Here;
XY Previous;
int ID;
};
int FindID(Dot *a,int b, int c)
{
int result(0);
while (1)
{
if (a[result].Here.x == b && a[result].Here.y == c)
return result;
result++;
}
}
int HowLong(XY *a)
{
int result(0);
while (1)
{
if (a[result].x == -1)
return result;
result++;
}
}
//SizeY должен быть равен 14 или изменить 14 в передоваемом фиелде
void PathFind(int SizeX,int SizeY, int Field[][80],Dot Start,Dot Finish, XY *Path1)
{
XY Check;
Dot *FindPath = new Dot[SizeX * SizeY];
int IDS(0), a(0), Counter(0), I(0), Step(0);
bool GotFinish(1);
FindPath[IDS].Here = Finish.Here;
IDS++;
Field[Finish.Here.x][Finish.Here.y] = Step;
while (Field[Start.Here.x][Start.Here.y] == -1 || Counter == SizeX * SizeY - 1)
{
Step++;
Counter++;
for (int i(0); i < SizeX; i++)
for (int j(0); j < SizeY; j++)
{
if (Field[i][j] == Step - 1)
{
Check.x = i + 1; Check.y = j + 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i + 1; Check.y = j - 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i - 1; Check.y = j + 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i - 1; Check.y = j - 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i + 1; Check.y = j;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i - 1; Check.y = j;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i; Check.y = j + 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
if (Field[i][j] == Step - 1)
{
Check.x = i; Check.y = j - 1;
if (Check.x >= 0 && Check.x < SizeX && Check.y >= 0 && Check.y < SizeY)
if (Field[Check.x][Check.y] == -1)
{
Field[Check.x][Check.y] = Step;
FindPath[IDS].ID = IDS;
FindPath[IDS].Here = Check;
FindPath[IDS].Previous.x = i;
FindPath[IDS].Previous.y = j;
IDS++;
}
}
}
}
a = FindID(FindPath, Start.Here.x, Start.Here.y);
Path1[I].x = FindPath[a].Here.x;
Path1[I].y = FindPath[a].Here.y;
while (Path1[I].x != Finish.Here.x || Path1[I].y != Finish.Here.y)
{
I++;
Path1[I].x = FindPath[a].Previous.x;
Path1[I].y = FindPath[a].Previous.y;
a = FindID(FindPath, Path1[I].x, Path1[I].y);
}
delete FindPath;
}
void PrintWall(int SizeX,int SizeY)
{
for (int i(0); i <= SizeX; i++)
{
Coord(i, SizeY);
cout << 'X';
}
for (int i(0); i < SizeY ; i++)
{
Coord(SizeX,i);
cout << 'X';
}
}
void main()
{
srand(time(NULL));
const int Bord = 99, SizeX = 78, SizeY = 80;
int Field[SizeX][SizeY];
XY porebrik[Bord];
porebrik[0].x = 0; porebrik[0].y = 3;
porebrik[1].x = 1; porebrik[1].y = 3;
porebrik[2].x = 1; porebrik[2].y = 7;
porebrik[3].x = 1; porebrik[3].y = 10;
porebrik[4].x = 1; porebrik[4].y = 11;
porebrik[5].x = 1; porebrik[5].y = 12;
porebrik[6].x = 2; porebrik[6].y = 3;
porebrik[7].x = 2; porebrik[7].y = 5;
porebrik[8].x = 2; porebrik[8].y = 7;
porebrik[9].x = 2; porebrik[9].y = 8;
porebrik[10].x = 2; porebrik[10].y = 9;
porebrik[11].x = 2; porebrik[11].y = 10;
porebrik[12].x = 2; porebrik[12].y = 12;
porebrik[13].x = 3; porebrik[13].y = 1;
porebrik[14].x = 3; porebrik[14].y = 3;
porebrik[15].x = 3; porebrik[15].y = 5;
porebrik[16].x = 3; porebrik[16].y = 8;
porebrik[17].x = 3; porebrik[17].y = 12;
porebrik[18].x = 4; porebrik[18].y = 1;
porebrik[19].x = 4; porebrik[19].y = 3;
porebrik[20].x = 4; porebrik[20].y = 5;
porebrik[21].x = 4; porebrik[21].y = 6;
porebrik[22].x = 4; porebrik[22].y = 8;
porebrik[23].x = 4; porebrik[23].y = 12;
porebrik[24].x = 5; porebrik[24].y = 1;
porebrik[25].x = 5; porebrik[25].y = 6;
porebrik[26].x = 5; porebrik[26].y = 8;
porebrik[27].x = 5; porebrik[27].y = 9;
porebrik[28].x = 5; porebrik[28].y = 10;
porebrik[29].x = 5; porebrik[29].y = 12;
porebrik[30].x = 6; porebrik[30].y = 1;
porebrik[31].x = 6; porebrik[31].y = 2;
porebrik[32].x = 6; porebrik[32].y = 3;
porebrik[33].x = 6; porebrik[33].y = 4;
porebrik[34].x = 6; porebrik[34].y = 5;
porebrik[35].x = 6; porebrik[35].y = 6;
porebrik[36].x = 6; porebrik[36].y = 8;
porebrik[37].x = 7; porebrik[37].y = 1;
porebrik[38].x = 7; porebrik[38].y = 8;
porebrik[39].x = 7; porebrik[39].y = 10;
porebrik[40].x = 7; porebrik[40].y = 11;
porebrik[41].x = 7; porebrik[41].y = 12;
porebrik[42].x = 8; porebrik[42].y = 1;
porebrik[43].x = 8; porebrik[43].y = 2;
porebrik[44].x = 8; porebrik[44].y = 3;
porebrik[45].x = 8; porebrik[45].y = 4;
porebrik[46].x = 8; porebrik[46].y = 8;
porebrik[47].x = 8; porebrik[47].y = 10;
porebrik[48].x = 9; porebrik[48].y = 4;
porebrik[49].x = 9; porebrik[49].y = 6;
porebrik[50].x = 9; porebrik[50].y = 7;
porebrik[51].x = 9; porebrik[51].y = 8;
porebrik[52].x = 9; porebrik[52].y = 10;
porebrik[53].x = 9; porebrik[53].y = 12;
porebrik[54].x = 9; porebrik[54].y = 13;
porebrik[55].x = 10; porebrik[55].y = 0;
porebrik[56].x = 10; porebrik[56].y = 1;
porebrik[57].x = 10; porebrik[57].y = 4;
porebrik[58].x = 10; porebrik[58].y = 10;
porebrik[59].x = 10; porebrik[59].y = 12;
porebrik[60].x = 11; porebrik[60].y = 10;
porebrik[61].x = 12; porebrik[61].y = 0;
porebrik[62].x = 12; porebrik[62].y = 2;
porebrik[63].x = 12; porebrik[63].y = 3;
porebrik[64].x = 12; porebrik[64].y = 4;
porebrik[65].x = 12; porebrik[65].y = 5;
porebrik[66].x = 12; porebrik[66].y = 6;
porebrik[67].x = 12; porebrik[67].y = 7;
porebrik[68].x = 12; porebrik[68].y = 8;
porebrik[69].x = 12; porebrik[69].y = 9;
porebrik[70].x = 12; porebrik[70].y = 10;
porebrik[71].x = 13; porebrik[71].y = 2;
porebrik[72].x = 13; porebrik[72].y = 10;
porebrik[73].x = 13; porebrik[73].y = 11;
porebrik[74].x = 13; porebrik[74].y = 12;
porebrik[75].x = 14; porebrik[75].y = 2;
porebrik[76].x = 14; porebrik[76].y = 3;
porebrik[77].x = 14; porebrik[77].y = 4;
porebrik[78].x = 14; porebrik[78].y = 6;
porebrik[79].x = 14; porebrik[79].y = 7;
porebrik[80].x = 14; porebrik[80].y = 8;
porebrik[81].x = 14; porebrik[81].y = 10;
porebrik[82].x = 15; porebrik[82].y = 8;
porebrik[83].x = 16; porebrik[83].y = 2;
porebrik[84].x = 16; porebrik[84].y = 3;
porebrik[85].x = 16; porebrik[85].y = 4;
porebrik[86].x = 16; porebrik[86].y = 5;
porebrik[87].x = 16; porebrik[87].y = 6;
porebrik[88].x = 16; porebrik[88].y = 8;
porebrik[89].x = 16; porebrik[89].y = 10;
porebrik[90].x = 16; porebrik[90].y = 11;
porebrik[91].x = 16; porebrik[91].y = 12;
porebrik[92].x = 16; porebrik[92].y = 13;
porebrik[93].x = 17; porebrik[93].y = 2;
porebrik[94].x = 17; porebrik[94].y = 8;
porebrik[95].x = 17; porebrik[95].y = 10;
porebrik[96].x = 18; porebrik[96].y = 8;
porebrik[97].x = 12; porebrik[97].y = 1;
porebrik[98].x = 11; porebrik[98].y = 4;
for (int i(0); i < SizeX; i++)
for (int j(0); j < SizeY; j++)
Field[i][j] = -1;
for (int i(0); i < Bord; i++)
{
Field[porebrik[i].x][porebrik[i].y] = -2;
}
Dot Start;
Dot Finish;
Start.Here.x = 1;
Start.Here.y = 1;
Finish.Here.x = 67;
Finish.Here.y = 72;
XY Path1[SizeX * SizeY];
for (int i(0); i < SizeX * SizeY;i++)
{
Path1[i].x = -1;
}
// while (1){
for (int i(0); i < SizeX; i++)
for (int j(0); j < SizeY; j++)
Field[i][j] = -1;
for (int i(0); i < Bord; i++)
{
Field[porebrik[i].x][porebrik[i].y] = -2;
}
system("cls");
PathFind(SizeX, SizeY, Field, Start, Finish, Path1);
for (int i(0); i < Bord; i++)
{
Coord(porebrik[i]);
cout << 'X';
}
PrintWall(SizeX, SizeY);
SetColor(11, 0);
for (int i(1); i < HowLong(Path1) - 1; i++)
{
Coord(Path1[i]);
cout << '+';
}
Coord(Start.Here);
cout << 'A';
Coord(Finish.Here);
cout << 'B';
//_getch();
// }
_getch();
}