a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

基于C語言實現簡單的走迷宮游戲

2020-01-26 14:38:50
字體:
來源:轉載
供稿:網友

本文實例講述了C語言實現簡單的走迷宮游戲的方法,代碼完整,便于讀者理解。

學數據結構時用“棧”寫的一個走迷宮程序,實際上用到雙向隊列,方便在運行完畢后輸出經過的點。

#include <cstdio>#include <deque>#include <windows.h>using namespace std;class node{public: int x,y; int lastOpt;};deque<node> sta;int x,y;int endx,endy;int mapW,mapH;int steps;int xopt[5]= {0,0,1,0,-1};int yopt[5]= {0,1,0,-1,0};int map[100][100]={ };void init(){ x = 1; y = 1; endx = 1; endy = 9; mapH = 10; mapW = 10; for(int i=0; i<=mapH; i++)  for(int j=0; j<=mapW; j++)  {   if(i==0 ||j==0 ||i==mapH||j==mapW)    map[i][j]=-1;  }  steps=0; map[1][2]=-1; map[2][2]=-1; map[3][2]=-1; map[4][2]=-1;  map[6][2]=-1; map[7][2]=-1; map[8][2]=-1; map[9][2]=-1; map[9][3]=-1; map[8][3]=-1; map[1][4]=-1; map[3][4]=-1; map[4][4]=-1; map[5][4]=-1; map[6][4]=-1; map[7][4]=-1; map[1][6]=-1; map[2][6]=-1; map[3][6]=-1; map[4][6]=-1; map[5][6]=-1; map[6][6]=-1; map[7][6]=-1; map[8][6]=-1; map[8][7]=-1; map[8][8]=-1; map[7][8]=-1; map[6][8]=-1; map[5][8]=-1; map[4][8]=-1; map[3][8]=-1; map[2][8]=-1; map[1][8]=-1;  map[endx][endy]=5;} void dis(){ system("cls"); int ori = map[x][y]; map[x][y]=1; for(int i=0; i<=mapH; ++i) {  for(int j=0; j<=mapW; ++j)  {   if(map[i][j]==0)    printf(" ");   else if(map[i][j]==-1)    printf(" #");   else if(map[i][j]==1)    printf(" @");   else if(map[i][j]==2)    printf(" .");   else if(map[i][j]==5)    printf(" !");  }  cout<<i<<endl; } for(int j=0; j<=mapW; ++j)  cout<<j<<" "; printf("/n/n > steps:%d Exit:(%d,%d)/n",steps,endx,endy); map[x][y] = ori;}int can(int n){ if(map[x+xopt[n]][y+yopt[n]] == 0 || map[x+xopt[n]][y+yopt[n]] == 5)  return 1; return 0; }void visit(int n){ map[x][y]=2; x+=xopt[n]; y+=yopt[n]; node tem; tem.x = x; tem.y = y; tem.lastOpt = n; sta.push_back(tem); steps++;}int main(){ init(); node tem; while( x != endx || y!=endy) {   int cans = 0;  for(int i=1; i<=4; i++)  {   if(can(i))   {    cans = 1;    visit(i);    break;   }  }  if(!cans)  {   if(!sta.empty())   {    tem = sta.back();    map[tem.x][tem.y]=0;    sta.pop_back();   }   else   {    map[x][y]=2;    x+=xopt[tem.lastOpt];    x+=yopt[tem.lastOpt];    dis();    break;   }   }   dis();  Sleep(500);  } if(x==endx && y == endy) cout<<"/n > i am finished..../n"; else cout<<"/n > i am finished...but i can't find the right way/n"; return 0;}

效果圖:

以上就是本文的全部內容,希望對大家學習C語言有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 一区二区三区在线 | 四虎影视免费在线观看 | 久久亚洲一区二区 | 亚洲免费视频网站 | 欧美在线观看禁18 | 婷婷精品 | 夜夜操天天操 | 伊人日韩 | 国产视频网| 欧美在线视频一区二区 | 免费a级作爱片免费观看欧洲 | 久草福利在线视频 | 久久成人国产 | 狠狠撸在线视频 | 国产精品久久久久久久岛一牛影视 | 91视频免费网站 | 羞羞视频免费网站 | 成人av在线看 | 无码少妇一区二区三区 | 偷拍自拍亚洲色图 | av免费在线播放 | 久艹在线 | 亚洲天堂在线视频观看 | 亚洲午夜电影 | 黄色av网站免费看 | 久久久久国产亚洲日本 | 一区在线观看 | 成人深夜福利在线观看 | 国产精品亲子伦av一区二区三区 | 免费一二区 | 中文字幕视频在线免费观看 | 国产精品久久久久久久久久免费 | 老牛嫩草一区二区三区眼镜 | 欧美日韩综合精品 | 亚洲精品国产综合区久久久久久久 | 天天天天天天天天干 | 日本中文字幕在线看 | 亚洲国产欧美在线 | 日本一区二区三区精品视频 | 高清精品自拍亚洲 | 日韩视频免费观看 |