-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchWay.h
153 lines (138 loc) · 3.54 KB
/
searchWay.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//Èñïîëüçóåòñÿ öèêëè÷åñêàÿ î÷åðåäü äëÿ àëãîðèòìà âûõîäà èç ëàáèðèíòà
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
struct Coordinate{
int y; //ñòðîêà row
int x; //ñòîëáåö col
friend ostream& operator<<(ostream &os, const Coordinate &tmpC){
os << tmpC.x << " " << tmpC.y << endl;
return os;
}
friend istream& operator>>(istream &is, Coordinate &tmpC){
is >> tmpC.x >> tmpC.y;
return is;
}
Coordinate& operator= (const Coordinate &tmp){
x = tmp.x;
y = tmp.y;
return *this;
}
bool operator!=(const Coordinate &tmp){
if ((y == tmp.y) && (x == tmp.x))
return false;
else
return true;
}
};
int Direction [2000];
int NumMoves = 0;
class Queue{
private:
Coordinate *C;
int size;
int first, last; //èíäåêñ ðåàëüíîãî ïåðâîãî è ïîñëåäíåãî ýëåìåíòà
int n; //ðåàëüíîå êîëè÷åñòâî ýëåìåíòîâ â î÷åðåäè
public:
Queue (){
first = -1; last = -1; //î÷åðåäü ïóñòàÿ
n = 0; size = 100;
C = new Coordinate [size];
}
Queue (int tmpSize){
first = -1; last = -1;
n = 0; size = tmpSize;
C = new Coordinate [size];
}
Queue (const Queue &tmp){
if (C != NULL) delete [] C;
first = tmp.first; last = tmp.last;
n = tmp.n; size = tmp.size;
C = new Coordinate [size];
for (int i = 0; i < size; i++)
C[i] = tmp.C[i];
}
virtual ~Queue (){
delete [] C;
}
//--------------------------------------------------------------
void addLast (const Coordinate &tmp){
if (n >= size) return;
if (n == 0){
first = 0; last = 0;
C[last] = tmp; n++;
}
else{
last = (last + 1) % size;
C[last] = tmp; n++;
}
}
Coordinate delFirst (){
Coordinate tmp;
if (n == 0){
tmp.x = -1;
tmp.y = -1;
return tmp;
}
if (n == 1){
tmp = C[first]; n = 0;
first = -1; last = -1;
}
else{
tmp = C[first]; n--;
first = (first + 1) % size;
}
return tmp;
}
};
void outputSearch (bool **labyrinth, Coordinate &start, const Coordinate &finish, const Coordinate &size){
NumMoves = 0;
int **dir;
dir = new int* [size.x];
for (int i = 0; i < size.x; i++){
dir [i] = new int [size.y];
for (int j = 0; j < size.y; j++)
dir [i][j] = 0;
}
dir [finish.x][finish.y] = 0;
dir [start.x][start.y] = -3;
///////////////////////////////////////////////////
int answer = size.x * size.y;
Queue q (answer); answer = 0;
Coordinate tmpc;
q.addLast (start);
tmpc = q.delFirst ();
while (tmpc != finish){
if ((tmpc.y != 0) && (dir [tmpc.x][tmpc.y - 1] == 0) && (!labyrinth [tmpc.x][tmpc.y - 1])){
dir [tmpc.x][--tmpc.y] = -1;
q.addLast (tmpc); tmpc.y++;
}
if ((tmpc.y != size.y - 1) && (dir [tmpc.x][tmpc.y + 1] == 0) && (!labyrinth [tmpc.x][tmpc.y + 1])){
dir [tmpc.x][++tmpc.y] = 1;
q.addLast (tmpc); tmpc.y--;
}
if ((tmpc.x != 0) && (dir[tmpc.x - 1][tmpc.y] == 0) && (!labyrinth [tmpc.x - 1][tmpc.y])){
dir [--tmpc.x][tmpc.y] = -2;
q.addLast (tmpc); tmpc.x++;
}
if ((tmpc.x != size.x - 1) && (dir [tmpc.x + 1][tmpc.y] == 0) && (!labyrinth [tmpc.x + 1][tmpc.y])){
dir [++tmpc.x][tmpc.y] = 2;
q.addLast (tmpc); tmpc.x--;
}
tmpc = q.delFirst ();
if ((tmpc.x == -1) && (tmpc.y == -1)){
answer = -1; tmpc = finish;
}
}
if (answer != -1){
tmpc = finish;
while (tmpc != start){
if (dir [tmpc.x][tmpc.y] == -1){ tmpc.y++; Direction [NumMoves++] = 1; }
else if (dir [tmpc.x][tmpc.y] == 1){ tmpc.y--; Direction [NumMoves++] = 4; }
else if (dir [tmpc.x][tmpc.y] == -2){ tmpc.x++; Direction [NumMoves++] = 3; }
else if (dir [tmpc.x][tmpc.y] == 2){ tmpc.x--; Direction [NumMoves++] = 2; }
answer++;
}
}
}