欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

基于C語言實(shí)現(xiàn)簡(jiǎn)單的走迷宮游戲

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(shí)例講述了C語言實(shí)現(xiàn)簡(jiǎn)單的走迷宮游戲的方法,代碼完整,便于讀者理解。

學(xué)數(shù)據(jù)結(jié)構(gòu)時(shí)用“?!睂懙囊粋€(gè)走迷宮程序,實(shí)際上用到雙向隊(duì)列,方便在運(yùn)行完畢后輸出經(jīng)過的點(diǎn)。

#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;
}

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)C語言有所幫助。

上一篇:詳解C語言編程中的函數(shù)指針以及函數(shù)回調(diào)

欄    目:C語言

下一篇:C/C++程序編譯流程詳解

本文標(biāo)題:基于C語言實(shí)現(xiàn)簡(jiǎn)單的走迷宮游戲

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2375.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有