开头没啥好写的,直接看吧(
1.
随机生成指定大小的迷宫(附有移动功能)
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
enum CellState: int {
PATH=0,WALL,FLAG
};
struct Point2{
int x,y;
Point2(int _x,int _y): x(_x), y(_y) {}
};
const int mazeSize=41;//迷宫大小(可调,一定要为奇数)
//递归版
/*
void DFS_generator(int _x,int _y,vector<vector<int>>& maze){
vector<vector<int>> dir{ {1,0},{-1,0},{0,1},{0,-1} };
random_shuffle(dir.begin(),dir.end());
maze[_x][_y]=PATH;
for(int i=0;i<4;i++){
if(_x+2*dir[i][0]>=1 && _x+2*dir[i][0]<=mazeSize-2 && _y+2*dir[i][1]>=1 && _y+2*dir[i][1]<=mazeSize-2 && maze[_x+2*dir[i][0]][_y+2*dir[i][1]]==WALL){
maze[_x+dir[i][0]][_y+dir[i][1]]=PATH;
DFS_generator( _x+2*dir[i][0], _y+2*dir[i][1],maze);
}
}
}
*/
//迭代版
void DFS_generator(vector<vector<int>>& maze){
stack<Point2> sp;
vector<vector<int>> dir{ {1,0},{-1,0},{0,1},{0,-1} };
Point2 temp((rand()%(mazeSize-2)+1)|1,(rand()%(mazeSize-2)+1)|1);
sp.push(temp);
while(!sp.empty()){
if(maze[temp.x][temp.y]!=PATH){
maze[temp.x][temp.y]=PATH;
}
random_shuffle(dir.begin(),dir.end());
int i=0;
for(;i<4;i++){
if(temp.x+2*dir[i][0]>=1 && temp.x+2*dir[i][0]<=mazeSize-2 && temp.y+2*dir[i][1]>=1 && temp.y+2*dir[i][1]<=mazeSize-2 && maze[temp.x+2*dir[i][0]][temp.y+2*dir[i][1]]==WALL){
maze[temp.x+dir[i][0]][temp.y+dir[i][1]]=PATH;
temp.x+=2*dir[i][0];
temp.y+=2*dir[i][1];
sp.push(temp);
break;
}
}
if(i==4){
sp.pop();
}
if(!sp.empty()){
temp=sp.top();
}
}
}
int main(){
srand(time(nullptr));
printf("迷宫(按wasd移动)");
_sleep(1000);
system("cls");
printf("请按任意键继续...");
char ttt=_getwch();
system("cls");
Point2 start(0,1);
Point2 end(mazeSize-1,mazeSize-2);
vector<vector<int>> maze;
for(int i=0;i<mazeSize;i++){
maze.push_back(vector<int>());
}
for(int i=0;i<mazeSize;i++){
for(int j=0;j<mazeSize;j++){
maze[i].push_back(WALL);
}
}
maze[start.x][start.y]=maze[end.x][end.y]=PATH;
//递归版
// DFS_generator((rand()%(mazeSize-2)+1)|1,(rand()%(mazeSize-2)+1)|1,maze);
//迭代版
DFS_generator(maze);
Point2 man(0,1);
while(1){
for(int i=0;i<mazeSize;i++){
for(int j=0;j<mazeSize;j++){
if(man.x==i&&man.y==j){
cout<<'@';
}else{
cout<<(maze[i][j]?'#':' ');
}
}
cout<<endl;
}
char c=_getwch();
// cout<<int(c)<<endl;
// _sleep(1000);
if(c=='w'){
if(man.x-1>=1&&maze[man.x-1][man.y]!=1){
man.x--;
}
}
if(c=='s'){
if(man.x+1<=mazeSize-2&&maze[man.x+1][man.y]!=1){
man.x++;
}
}
if(c=='a'){
if(man.y-1>=1&&maze[man.x][man.y-1]!=1){
man.y--;
}
}
if(c=='d'){
if(man.y+1<=mazeSize-2&&maze[man.x][man.y+1]!=1){
man.y++;
}
}
if(man.x+1==end.x&&man.y==end.y&&c=='s'){
man.x++;
break;
}
if(man.y+1==end.y&&man.x==end.x&&c=='d'){
man.y++;
break;
}
system("cls");
}
return 0;
}
2.
用字符打印指定大小的谢尔宾斯基三角形
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int n;
char a[2050][2050];
void g(int n,int x,int y){
if(n==1){
a[x+1][y]=a[x][y+1]='/';
a[x+1][y+2]=a[x+1][y+1]='_';
a[x][y+2]=a[x+1][y+3]='\\';
return;
}
g(n-1,x,y+(1<<(n-1)));
g(n-1,x+(1<<(n-1)),y);
g(n-1,x+(1<<(n-1)),y+(1<<n));
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
g(n,0,0);
for(int i=0;i<1<<n;i++){
for(int j=0;j<1<<(n+1);j++){
if(a[i][j]){
putchar(a[i][j]);
}
else putchar(' ');
}
cout<<endl;
}
return 0;
}