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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| #include <iostream> #include <vector> #include <cstring> #include <algorithm> #include <graphics.h>
using namespace std;
#define ROWS 10 #define COLS 10
#define ZXDJ 10 #define XXDJ 14
#define SPACE 50
IMAGE road, wall, pos, ren;
enum direct { p_up, p_down, p_left, p_right, p_lup, p_ldown, p_rup, p_rdown };
struct MyPoint { int row; int col; int f, g, h;
void getF() { f = g + h; } };
struct treeNode { MyPoint pos; treeNode* pParent; vector<treeNode*> child; };
treeNode* createNode(MyPoint pos) { treeNode* pNew = new treeNode{ MyPoint{pos.row, pos.col, 0, 0, 0}, nullptr, vector<treeNode*>() }; return pNew; }
int getH(MyPoint pos, MyPoint endPos);
void releaseMemory(const vector<treeNode*>& nodes) { for (auto node : nodes) { delete node; } }
void printMap(const vector<vector<bool>>& pathMap, const int map[ROWS][COLS]) { for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { if (pathMap[i][j]) { cout << "X "; } else if (map[i][j] == 1) { cout << "# "; } else { cout << ". "; } } cout << endl; } cout << endl; }
void drawMap(int map[ROWS][COLS]);
void printCurrentPosition(const MyPoint& pos) { cout << "当前位置: (" << pos.row + 1 << "," << pos.col + 1 << ")" << endl; }
int main() {
initgraph(COLS * SPACE, ROWS * SPACE, SHOWCONSOLE);
loadimage(&road, L"road.bmp", SPACE, SPACE, true); loadimage(&wall, L"wall.bmp", SPACE, SPACE, true); loadimage(&ren, L"ren.bmp", SPACE, SPACE, true);
int map[ROWS][COLS] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 1, 0, 0, 1, 1, 0, 0, 1 }, { 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 }, { 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 }, { 1, 0, 0, 0, 0, 0, 1, 0, 1, 1 }, { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 0, 1, 0, 0, 0, 0, 0, 1, 1 }, { 1, 0, 1, 0, 1, 1, 1, 0, 0, 1 }, { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };
vector<vector<bool>> pathMap(ROWS, vector<bool>(COLS, false));
MyPoint begPos = { 1, 1 }; MyPoint endPos = { 7, 8 };
pathMap[begPos.row][begPos.col] = true; treeNode* pRoot = createNode(begPos);
vector<treeNode*> buff; vector<treeNode*>::iterator it; vector<treeNode*>::iterator itMin;
treeNode* pCurrent = pRoot; bool isFindEnd = false;
putimage(pCurrent->pos.col * SPACE, pCurrent->pos.row * SPACE, &pos); drawMap(map);
putimage(pCurrent->pos.col * SPACE, pCurrent->pos.row * SPACE, &ren); while (1) { putimage(pCurrent->pos.col * SPACE, pCurrent->pos.row * SPACE, &road); for (int i = 0; i < 8; i++) { treeNode* pTemp = createNode(pCurrent->pos); switch (i) { case p_up: pTemp->pos.row--; pTemp->pos.g += ZXDJ; break; case p_down: pTemp->pos.row++; pTemp->pos.g += ZXDJ; break; case p_left: pTemp->pos.col--; pTemp->pos.g += ZXDJ; break; case p_right: pTemp->pos.col++; pTemp->pos.g += ZXDJ; break; case p_lup: pTemp->pos.row--; pTemp->pos.col--; pTemp->pos.g += XXDJ; break; case p_ldown: pTemp->pos.row++; pTemp->pos.col--; pTemp->pos.g += XXDJ; break; case p_rup: pTemp->pos.row--; pTemp->pos.col++; pTemp->pos.g += XXDJ; break; case p_rdown: pTemp->pos.row++; pTemp->pos.col++; pTemp->pos.g += XXDJ; break; }
if (pTemp->pos.row >= 0 && pTemp->pos.row < ROWS && pTemp->pos.col >= 0 && pTemp->pos.col < COLS && !pathMap[pTemp->pos.row][pTemp->pos.col] && map[pTemp->pos.row][pTemp->pos.col] == 0) { pTemp->pos.h = getH(pTemp->pos, endPos); pTemp->pos.getF(); pCurrent->child.push_back(pTemp); pTemp->pParent = pCurrent; buff.push_back(pTemp); } else { delete pTemp; } }
if (buff.empty()) { break; }
itMin = min_element(buff.begin(), buff.end(), [](const treeNode* a, const treeNode* b) { return a->pos.f < b->pos.f; });
pCurrent = *itMin; pathMap[pCurrent->pos.row][pCurrent->pos.col] = true; printMap(pathMap, map); printCurrentPosition(pCurrent->pos); buff.erase(itMin);
putimage(pCurrent->pos.col* SPACE, pCurrent->pos.row* SPACE, &ren);
getchar();
if (pCurrent->pos.row == endPos.row && pCurrent->pos.col == endPos.col) { isFindEnd = true; break; } }
if (isFindEnd) { cout << "找到终点了!" << endl; cout << "最终路径: "; while (pCurrent) { cout << "(" << pCurrent->pos.row + 1 << "," << pCurrent->pos.col + 1 << ")"; pCurrent = pCurrent->pParent; } cout << endl; } else { cout << "未找到路径!" << endl; }
releaseMemory(buff);
return 0; }
int getH(MyPoint pos, MyPoint endPos) { int x = (pos.col > endPos.col) ? (pos.col - endPos.col) : (endPos.col - pos.col); int y = (pos.row > endPos.row) ? (pos.row - endPos.row) : (endPos.row - pos.row); return (x + y) * ZXDJ; }
void drawMap(int map[ROWS][COLS]) { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (map[i][j] == 1) { putimage(j * SPACE, i * SPACE, &wall); } else { putimage(j * SPACE, i * SPACE, &road); } } } }
|