网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
11月13日漏签0天
c++吧 关注:641,960贴子:2,116,618
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 5回复贴,共1页
<<返回c++吧
>0< 加载中...

请高手帮我详细解释这段代码!!!

  • 只看楼主
  • 收藏

  • 回复
  • Free乄冻结
  • ,
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include <iostream>#include <cstdlib>#include <ctime>#include <windows.h>
using namespace std;int M;int N;int **maze;
class stack_for_maze//记录路径{ private: struct node { int x; int y; char direction; //上一步方向 node* next; }; node* head; public: stack_for_maze() { head=NULL; } ~stack_for_maze() { node* p=head; while(head!=NULL) { head=head->next; delete p; p=head; } } void push(int xx,int yy,char ddirection)//压栈 { node* new_node; new_node=new node; if(new_node!=NULL) { new_node->x=xx; new_node->y=yy; new_node->direction=ddirection; new_node->next=NULL; if(head==NULL) head=new_node; else { new_node->next=head; head=new_node; } } else cout<<"\n压栈失败!"; } node* pop(int& xx,int& yy) { if(head!=NULL) { node* p=head; head=head->next; xx=p->x; yy=p->y; delete p; } else { cout<<"\n出栈失败!"; } return head; } void print() { if(head!=NULL) { node* p=head; while(p!=NULL) { cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl; p=p->next; } } else cout<<"\n打印失败!"; }};void mazeGenerator(){ cout<<"请输入迷宫的宽度:"; cin>>M; cout<<"请输入迷宫的高度:"; cin>>N; maze = (int **)malloc(sizeof(int *)*M); int i; for (i=0;i<M;i++) maze[i]=(int *)malloc(sizeof(int)*N); int max_way=M*N; int x,y; for(x=0;x<M;x++) for(y=0;y<N;y++) maze[x][y]=1;
srand((unsigned)time(NULL)); for(int i=0;i<=max_way;i++) { x=rand()%(M-2)+1; y=rand()%(N-2)+1; maze[x][y]=0; } maze[1][1]=0; maze[M-2][N-2]=0;
maze[0][1]=2; maze[M-1][N-2]=0;}void PrintMaze(){ int x,y; system("cls"); cout<<endl; for(x=0;x<M;x++) { for(y=0;y<N;y++) { if(maze[x][y]==0||maze[x][y]==8){cout<<"..";continue;} if(maze[x][y]==1){cout<<"##";continue;} if(maze[x][y]==2||maze[x][y]==3||maze[x][y]==4||maze[x][y]==5||maze[x][y]==6){cout<<"×";continue;} if(maze[x][y]==7){cout<<"※";continue;} } cout<<endl; } Sleep(200);}void mazeTraverse(stack_for_maze &s){ int x=1,y=1; while(1) { maze[x][y]=2; if(maze[x+1][y]==0) { s.push(x,y,'D'); maze[x][y]=3;//标记D x=x+1; maze[x][y]=7;//标记当前位置 PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y+1]==0) { s.push(x,y,'R'); maze[x][y]=4; y=y+1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x-1][y]==0) { s.push(x,y,'U'); maze[x][y]=6; x=x-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(maze[x][y-1]==0) { s.push(x,y,'L'); maze[x][y]=5; y=y-1; maze[x][y]=7; PrintMaze(); if((x==M-1)&&(y==N-2)) { s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; } else continue; } if(s.pop(x,y)==NULL&&maze[x-1][y]!=0&&maze[x][y-1]!=0&&maze[x][y+1]!=0&&maze[x+1][y]!=0) { cout<<"\n回退到入口!!!\n"; maze[0][1]=7; if(maze[1][1]!=1)maze[1][1]=2; return; } }}void main(){ mazeGenerator(); cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔..表示迷宫的通路!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔##表示迷宫的墙壁!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔※表示站立的位置!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔×表示走过的路径!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; Sleep(4000); PrintMaze(); stack_for_maze stack; mazeTraverse(stack); system("pause");}


  • Free乄冻结
  • ,
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个。。。代码可读性很差~~~


2025-11-13 11:22:08
广告
不感兴趣
开通SVIP免广告
  • fengbaoxin1988
  • <
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
先整理好顺序吧


  • Free乄冻结
  • ,
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
int M;
int N;
int **maze;
class stack_for_maze//记录路径
{ private:
struct node
{ int x;
int y;
char direction; //上一步方向
node* next; };
node* head;
public:
stack_for_maze()
{ head=NULL; }
~stack_for_maze()
{ node* p=head;
while(head!=NULL)
{ head=head->next;
delete p;
p=head; } }
void push(int xx,int yy,char ddirection)//压栈
{ node* new_node;
new_node=new node;
if(new_node!=NULL)
{ new_node->x=xx;
new_node->y=yy;
new_node->direction=ddirection;
new_node->next=NULL;
if(head==NULL) head=new_node;
else { new_node->next=head; head=new_node; } }
else cout<<"\n压栈失败!"; }
node* pop(int& xx,int& yy)
{ if(head!=NULL)
{ node* p=head;
head=head->next;
xx=p->x;
yy=p->y;
delete p; }
else
{ cout<<"\n出栈失败!"; }
return head; }
void print()
{ if(head!=NULL)
{ node* p=head;
while(p!=NULL)
{ cout<<" "<<p->x<<" "<<p->y<<" "<<p->direction<<endl;
p=p->next; } }
else cout<<"\n打印失败!"; }};
void mazeGenerator()
{ cout<<"请输入迷宫的宽度:";
cin>>M;
cout<<"请输入迷宫的高度:";
cin>>N;
maze = (int **)malloc(sizeof(int *)*M);
int i;
for (i=0;i<M;i++)
maze[i]=(int *)malloc(sizeof(int)*N);
int max_way=M*N;
int x,y;
for(x=0;x<M;x++) for(y=0;y<N;y++) maze[x][y]=1;
srand((unsigned)time(NULL));
for(int i=0;i<=max_way;i++)
{ x=rand()%(M-2)+1;
y=rand()%(N-2)+1;
maze[x][y]=0; }
maze[1][1]=0;
maze[M-2][N-2]=0;
maze[0][1]=2;
maze[M-1][N-2]=0;}
void PrintMaze()
{ int x,y;
system("cls");
cout<<endl;
for(x=0;x<M;x++)
{ for(y=0;y<N;y++)
{ if(maze[x][y]==0||maze[x][y]==8)
{cout<<"..";continue;}
if(maze[x][y]==1){cout<<"##";continue;}
if(maze[x][y]==2||maze[x][y]==3||maze[x][y]==4||maze[x][y]==5||maze[x][y]==6)
{cout<<"×";continue;}
if(maze[x][y]==7){cout<<"※";continue;} }
cout<<endl; }
Sleep(200);}
void mazeTraverse(stack_for_maze &s)
{ int x=1,y=1;
while(1)
{ maze[x][y]=2;
if(maze[x+1][y]==0)
{ s.push(x,y,'D');
maze[x][y]=3;//标记D
x=x+1;
maze[x][y]=7;//标记当前位置
PrintMaze();
if((x==M-1)&&(y==N-2))
{ s.push(x,y,'*'); cout<<"\n成功走出迷宫!!\n"; return; }
else continue; }
if(maze[x][y+1]==0)
{ s.push(x,y,'R');
maze[x][y]=4;
y=y+1;
maze[x][y]=7;
PrintMaze();
if((x==M-1)&&(y==N-2))
{ s.push(x,y,'*');
cout<<"\n成功走出迷宫!!\n";
return; }
else continue; }
if(maze[x-1][y]==0)
{ s.push(x,y,'U');
maze[x][y]=6;
x=x-1;
maze[x][y]=7;
PrintMaze();
if((x==M-1)&&(y==N-2))
{ s.push(x,y,'*');
cout<<"\n成功走出迷宫!!\n";
return; }
else continue; }
if(maze[x][y-1]==0)
{ s.push(x,y,'L');
maze[x][y]=5;
y=y-1;
maze[x][y]=7;
PrintMaze();
if((x==M-1)&&(y==N-2))
{ s.push(x,y,'*');
cout<<"\n成功走出迷宫!!\n";
return; }
else continue; }
if(s.pop(x,y)==NULL&&maze[x-1][y]!=0&&maze[x][y-1]!=0&&maze[x][y+1]!=0&&maze[x+1][y]!=0)
{ cout<<"\n回退到入口!!!\n";
maze[0][1]=7;
if(maze[1][1]!=1)maze[1][1]=2;
return; } }}
void main(){ mazeGenerator();
cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔..表示迷宫的通路!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔##表示迷宫的墙壁!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔※表示站立的位置!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n"; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔×表示走过的路径!▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
Sleep(4000);
PrintMaze();
stack_for_maze stack;
mazeTraverse(stack);
system("pause");}


  • Free乄冻结
  • ,
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
顺序已经弄好。这是段迷宫游戏的代码。。求高手解答


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 5回复贴,共1页
<<返回c++吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示