作业介绍
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int mat[505][505];
int n,m;
int ix,iy,ex,ey;
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info ={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void print_maze();
void create_maze(int x,int y)
{
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int dir[4]={0,1,2,3};
random_shuffle(dir,dir+4);//打乱数组
mat[x][y] = 1;
// print_maze();
// Sleep(100);
for(int i=0;i<4;i++)
{
int xx = x + dx[dir[i]];
int yy = y + dy[dir[i]];
if(xx>1&&xx<n&&yy>1&&yy<m)
{
if(mat[xx+1][yy]+mat[xx-1][yy]+mat[xx][yy+1]+mat[xx][yy-1]<=1)
{
if(mat[xx][yy]==0)
{
create_maze(xx,yy);
}
}
}
}
}
void print_maze()
{
// system("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){0,0});
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(mat[i][j]==1) cout << " ";
else if(mat[i][j]==0) cout << "[]";
else if(mat[i][j]==2) cout << "我";
else if(mat[i][j]==3) cout << "$$";
}
cout << endl;
}
}
void print_maze2()
{
// system("cls");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){0,0});
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i>=ix-3&&i<=ix+3&&j>=iy-3&&j<=iy+3)
{
if(mat[i][j]==1) cout << " ";
else if(mat[i][j]==0) cout << "[]";
else if(mat[i][j]==2) cout << "我";
else if(mat[i][j]==3) cout << "$$";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
void init()
{
memset(mat,0,sizeof(mat));
system("cls");
cout << "请输入地图大小n*m" << endl;
cin >> n >> m;
HideCursor();
create_maze(n/2,m/2);
print_maze();
}
int main()
{
srand(time(0));
HideCursor();
init();
ix = rand()%(3)+2;
iy = rand()%(3)+2;
ex = rand()%(3)+n-4;
ey = rand()%(3)+m-4;
mat[ix][iy] = 2;
mat[ex][ey] = 3;
print_maze();
int step = 0;
while(1)
{
char dir = getch();
if(dir=='w'&&mat[ix-1][iy]!=0)
{
mat[ix][iy] = 1;
mat[ix-1][iy] = 2;
ix--;
++step;
}
if(dir=='s'&&mat[ix+1][iy]!=0)
{
mat[ix][iy] = 1;
mat[ix+1][iy] = 2;
ix++;
++step;
}
if(dir=='a'&&mat[ix][iy-1]!=0)
{
mat[ix][iy] = 1;
mat[ix][iy-1] = 2;
iy--;
++step;
}
if(dir=='d'&&mat[ix][iy+1]!=0)
{
mat[ix][iy] = 1;
mat[ix][iy+1] = 2;
iy++;
++step;
}
print_maze2();
if(ix==ex&&iy==ey)
{
break;
}
}
cout << "恭喜你找到了宝藏,共用了" << step << " 步"<< endl;
cout << "按任意键退出程序"<<endl;
char c= getch();
return 0;
}
光标移动到0 0
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),(COORD){0,0});
- 状态
- 已结束
- 题目
- 3
- 开始时间
- 2024-1-1 0:00
- 截止时间
- 2024-1-31 23:59
- 可延期
- 24 小时