1 //BFS 2 #include<list> 3 #include<queue> 4 #include<time.h> 5 #include<algorithm> 6 #include<graphics.h> 7 using namespace std; 8 /*------------全局变量----------------*/ 9 int Wndl = 500; //窗口大小 10 int Count = 25; //节点数量25*25 11 int Width = Wndl / Count; //绘图的正方形长度 12 int Radius = Width / 2; //正方形中点到边的距离 13 struct Pot { 14 int x0, y0; //正方形中点 15 int flag; //1表示障碍,初始化零表示未知 16 Pot(int x, int y, int f) :x0(x), y0(y), flag(f) {} 17 friend bool operator==(const Pot &arg0, const Pot &arg1) { 18 if (arg0.x0 == arg1.x0&&arg0.y0 == arg1.y0) 19 return true; 20 return false; 21 } 22 //为获得队列起始,队列目标的方法 23 Pot ForQue(int f) { 24 flag = f; 25 return *this; 26 } 27 }; 28 /*----------函数的向前声明--------------*/ 29 list<Pot> CreatPots(); 30 void CreatMap(list<Pot>&); 31 list<Pot>::iterator GetIter(list<Pot>&, int); 32 list<Pot>::iterator SearchPot(list<Pot>&, Pot, int); 33 //主函数 34 int main() { 35 //获得所有节点 36 list<Pot> Potlist = move(CreatPots()); 37 //地图的产生 38 HWND hwnd = initgraph(Wndl, Wndl); 39 CreatMap(Potlist); 40 //起始点、终点的随机产生,并特殊标记 41 srand((unsigned int)time(NULL)); 42 int Max = Count * Count; 43 int BnCt = rand() % Max; 44 int EnCt = rand() % Max; 45 Pot Begin_Pot = GetIter(Potlist, BnCt)->ForQue(1); 46 Pot Target_Pot = GetIter(Potlist, EnCt)->ForQue(0); 47 setfillcolor(RED); 48 fillrectangle(Begin_Pot.x0 - Radius, Begin_Pot.y0 - Radius, Begin_Pot.x0 + Radius, Begin_Pot.y0 + Radius); 49 fillrectangle(Target_Pot.x0 - Radius, Target_Pot.y0 - Radius, Target_Pot.x0 + Radius, Target_Pot.y0 + Radius); 50 //广度优先搜索 51 setlinecolor(WHITE); 52 queue<Pot> Potque; 53 Potque.push(Begin_Pot); 54 while (!Potque.empty()) { 55 Pot ThePot = Potque.front(); 56 Potque.pop(); 57 for (int i = 0; i < 4; i++) { 58 auto Iter = SearchPot(Potlist, ThePot, i); 59 if (Iter == Potlist.end()) 60 continue; 61 Sleep(200); 62 line(ThePot.x0, ThePot.y0, Iter->x0, Iter->y0); 63 Potque.push(*Iter); 64 if (*Iter == Target_Pot) 65 goto OK; 66 Potlist.erase(Iter); 67 } 68 } 69 //搜索结束,离开界面 70 MessageBox(hwnd, L"Search default", L"BFS", MB_OK); 71 if (0) { 72 OK: 73 MessageBox(hwnd, L"OK", L"BFS", MB_OK); 74 } 75 closegraph(); 76 return 0; 77 } 78 //包含所有节点的容器 79 list<Pot> CreatPots() { 80 srand((unsigned int)time(NULL)); 81 list<Pot> Potlist; 82 for (int i = 0; i < Count; i++) 83 for (int j = 0; j < Count; j++) 84 Potlist.push_back( 85 { i*Width + Radius,j*Width + Radius,(int)(rand() % 2 && rand() % 2) }); 86 return Potlist; 87 } 88 //界面的初始化 89 void CreatMap(list<Pot>&Potlist) { 90 setlinecolor(GREEN); 91 for (int i = 1; i < Count; i++) { 92 int pos = i * Width; 93 line(0, pos, Wndl, pos); 94 line(pos, 0, pos, Wndl); 95 } 96 setfillcolor(LIGHTGRAY); 97 int R = Radius; 98 for_each(Potlist.begin(), Potlist.end(), 99 [R](Pot it) { 100 if (it.flag == 1) 101 fillrectangle(it.x0 - R + 1, it.y0 - R + 1, it.x0 + R - 1, it.y0 + R - 1); }); 102 } 103 //为获得起点、终点 104 list<Pot>::iterator GetIter(list<Pot> &Potlist, int times) { 105 auto Iter = Potlist.begin(); 106 for (int i = 0; i < times; i++) 107 Iter++; 108 return Iter; 109 } 110 //寻找要入队列的节点 111 list<Pot>::iterator SearchPot(list<Pot> &pots, Pot arg, int flag) { 112 switch (flag) { 113 case 0:arg.x0 -= Width; break; 114 case 1:arg.x0 += Width; break; 115 case 2:arg.y0 -= Width; break; 116 case 3:arg.y0 += Width; break; 117 default:return pots.end(); 118 } 119 list<Pot>::iterator it = 120 find_if(pots.begin(), pots.end(), [arg](Pot it) { 121 if (it.flag == 1) return false; 122 if (it == arg) return true; 123 return false; }); 124 return it; 125 }