1. 程式人生 > >迷宮(深度搜索)

迷宮(深度搜索)

cursor 初始 nbsp tst main lec void pattern pat

  1 #include"iostream"
  2 #include"windows.h"
  3 #include"time.h"
  4 using namespace std;
  5 
  6 //gotoxy() x是第x列,y是第y行
  7 void gotoxy(int x, int y) //goto語句  
  8 {  
  9     COORD pos;  
 10     pos.X = x;  
 11     pos.Y = y;  
 12     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);  
13 } 14 15 class Maze{ 16 private: 17 char **map; 18 char bg; 19 char wall; 20 char cat; 21 int w; 22 int h; 23 public: 24 Maze(int w,int h); //創建迷宮 25 void setPattern(char bg,char wall,char cat); //設置圖案 26 void initialization(char bg,char
wall); //隨機初始化迷宮 27 void show(); //打印迷宮 28 29 bool move(int x,int y); //尋找出口 30 }; 31 32 int main(){ 33 const char bg = ; 34 const char wall = #; 35 const char cat = >; 36 Maze m1(80
,20); 37 m1.setPattern(bg,wall,cat); 38 m1.initialization(bg,wall); 39 m1.show(); 40 system("pause"); 41 bool b = m1.move(0,0); 42 gotoxy(0,21); 43 if(b){ 44 cout<<"成功"<<endl; 45 } 46 else{ 47 cout<<"未走出迷宮"<<endl; 48 } 49 gotoxy(0,23); 50 system("pause"); 51 return 0; 52 } 53 Maze::Maze(int w,int h){ 54 this->w = w; 55 this->h = h; 56 map = new char*[h]; 57 for(int i = 0;i < h;i++){ 58 map[i] = new char[w]; 59 } 60 bg = ; 61 wall = #; 62 cat = +; 63 } 64 void Maze::setPattern(char bg,char wall,char cat){ 65 this->bg = bg; 66 this->wall = wall; 67 this->cat = cat; 68 } 69 void Maze::initialization(char bg,char wall){ 70 srand(time(NULL)); 71 for(int i = 0;i < h;i++){ 72 for(int j = 0;j < w;j++){ 73 if(i == 0||j == 0||i == h - 1||j == w - 1){ 74 map[i][j] = wall; 75 } 76 else{ 77 map[i][j] = rand() % 4 == 0 ? wall : bg; 78 } 79 } 80 } 81 map[1][1] = bg; 82 map[0][1] = bg; 83 map[0][0] = bg; 84 map[h - 1][w - 1] = bg; 85 map[h - 1][w - 2] = bg; 86 } 87 void Maze::show(){ 88 for(int i = 0;i < h;i++){ 89 for(int j = 0;j < w;j++){ 90 cout<<map[i][j]; 91 } 92 cout<<endl; 93 } 94 } 95 bool Maze::move(int x,int y){ 96 if(x<0||y<0){ //x,y是否合法 97 return false; 98 } 99 if(x == w - 1&&y == h - 1){ //到達終點返回true 100 return true; 101 } 102 //未到達終點 103 if(map[y][x] == bg){ //如果該點是可以走的 104 gotoxy(x,y); 105 cout<<cat; 106 Sleep(5); 107 map[y][x] = cat; //每走一格就填充,否則會倒退死循環 108 //再走下一步 109 if(move(x + 1,y)||move(x,y + 1)||move(x,y - 1)||move(x - 1,y)){ //如果到達了終點,直接返回true 110 return true; 111 } 112 //如果該點被走過但無法到達終點,標記為- 113 else{ 114 gotoxy(x,y); 115 cout<<-; 116 } 117 } 118 return false; 119 }#include"iostream" 120 #include"windows.h" 121 #include"time.h" 122 using namespace std; 123 124 //gotoxy() x是第x列,y是第y行 125 void gotoxy(int x, int y) //goto語句 126 { 127 COORD pos; 128 pos.X = x; 129 pos.Y = y; 130 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); 131 } 132 133 class Maze{ 134 private: 135 char **map; 136 char bg; 137 char wall; 138 char cat; 139 int w; 140 int h; 141 public: 142 Maze(int w,int h); //創建迷宮 143 void setPattern(char bg,char wall,char cat); //設置圖案 144 void initialization(char bg,char wall); //隨機初始化迷宮 145 void show(); //打印迷宮 146 147 bool move(int x,int y); //尋找出口 148 }; 149 150 int main(){ 151 const char bg = ; 152 const char wall = #; 153 const char cat = >; 154 Maze m1(80,20); 155 m1.setPattern(bg,wall,cat); 156 m1.initialization(bg,wall); 157 m1.show(); 158 system("pause"); 159 bool b = m1.move(0,0); 160 gotoxy(0,21); 161 if(b){ 162 cout<<"成功"<<endl; 163 } 164 else{ 165 cout<<"未走出迷宮"<<endl; 166 } 167 gotoxy(0,23); 168 system("pause"); 169 return 0; 170 } 171 Maze::Maze(int w,int h){ 172 this->w = w; 173 this->h = h; 174 map = new char*[h]; 175 for(int i = 0;i < h;i++){ 176 map[i] = new char[w]; 177 } 178 bg = ; 179 wall = #; 180 cat = +; 181 } 182 void Maze::setPattern(char bg,char wall,char cat){ 183 this->bg = bg; 184 this->wall = wall; 185 this->cat = cat; 186 } 187 void Maze::initialization(char bg,char wall){ 188 srand(time(NULL)); 189 for(int i = 0;i < h;i++){ 190 for(int j = 0;j < w;j++){ 191 if(i == 0||j == 0||i == h - 1||j == w - 1){ 192 map[i][j] = wall; 193 } 194 else{ 195 map[i][j] = rand() % 4 == 0 ? wall : bg; 196 } 197 } 198 } 199 map[1][1] = bg; 200 map[0][1] = bg; 201 map[0][0] = bg; 202 map[h - 1][w - 1] = bg; 203 map[h - 1][w - 2] = bg; 204 } 205 void Maze::show(){ 206 for(int i = 0;i < h;i++){ 207 for(int j = 0;j < w;j++){ 208 cout<<map[i][j]; 209 } 210 cout<<endl; 211 } 212 } 213 bool Maze::move(int x,int y){ 214 if(x<0||y<0){ //x,y是否合法 215 return false; 216 } 217 if(x == w - 1&&y == h - 1){ //到達終點返回true 218 return true; 219 } 220 //未到達終點 221 if(map[y][x] == bg){ //如果該點是可以走的 222 gotoxy(x,y); 223 cout<<cat; 224 Sleep(5); 225 map[y][x] = cat; //每走一格就填充,否則會倒退死循環 226 //再走下一步 227 if(move(x + 1,y)||move(x,y + 1)||move(x,y - 1)||move(x - 1,y)){ //如果到達了終點,直接返回true 228 return true; 229 } 230 //如果該點被走過但無法到達終點,標記為- 231 else{ 232 gotoxy(x,y); 233 cout<<-; 234 } 235 } 236 return false; 237 }

迷宮(深度搜索)