正文
增加 248 行删除 46 行
123456789101112131415168 行未改动25262728293031323334353637383940414243444546474849505152535455565758596014 行未改动7576777879808182838485868788899091927 行未改动1001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401413 行未改动1451461471481491501511521533 行未改动1571581591601611621631642 行未改动16716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619718 行未改动216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250```cpp//Pyramid Created by seanli1008 on 2025/10/3.//Copyright (c) 2025年 seanli1008. All rights reserved.#include <bits/stdc++.h>#include <windows.h>#include <conio.h>#include <chrono>using namespace std;mt19937 rnd(time(0));const int dx[]={0,0,-1,1};const int dy[]={1,-1,0,0};int x,y,stx,sty,edx,edy,maze[50][50];bool vis[50][50],chosen[100];bool is_valid(int x,int y){if(x<1 || x>20 || y<1 || y>20)return false;8 行未改动}void debug(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){if(maze[i][j]==1)cout<<". ";else if(maze[i][j]==2)cout<<"x ";elsecout<<"# ";}cout<<endl;}}void generate_map(){//=== generate main path ===vector<pair<int,int>> path;x=y=1;stx=sty=1;vis[x][y]=true;maze[x][y]=1;bool flag=true;for(int i=1;i<=50;i++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){if(d==0 || d==2)tmp.push_back(d);else{tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}}}int k=tmp.size();14 行未改动path.clear();x=y=1;vis[x][y]=true;maze[x][y]=1;for(int i=1;i<=50;i++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){if(d==0 || d==2)tmp.push_back(d);else{tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}}}int k=tmp.size();7 行未改动maze[x][y]=1;path.push_back({x,y});}}//=== generate main path ===//debug();edx=x,edy=y;int delta=rnd()%5+1,sign=rnd()%2,tot=20;if(sign==0)tot-=delta;elsetot+=delta;for(int i=1;i<=tot;i++){int pos=rnd()%50;while(chosen[pos])pos=rnd()%50;int what_the_heck=rnd()%3;if(what_the_heck==0)pos=min(pos-5,1);chosen[pos]=true;int r=path[pos].first,c=path[pos].second;int dep=rnd()%5+8;int surprise=rnd()%3;if(surprise==0){dep+=10;pos=min(pos-10,1);}for(int j=1;j<=dep;j++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(r+dx[d],c+dy[d]) && !vis[r+dx[d]][c+dy[d]] && maze[r+dx[d]][c+dy[d]]!=1 && maze[r+dx[d]][c+dy[d]]!=2){if(d==0 || d==4){tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}elsetmp.push_back(d);}}int k=tmp.size();if(k==0)3 行未改动vis[r][c]=true;maze[r][c]=2;}}//debug();}void print(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){3 行未改动else if(i==edx && j==edy)cout<<"! ";else if(maze[i][j]==0)cout<<"# ";elsecout<<". ";}else2 行未改动cout<<endl;}}int main(){cout<<"Pyramid developed by seanli1008"<<endl;Sleep(1000);system("cls");cout<<"You are a treasure hunter. There is a priceless treasure in this pyramid. Please find it. "<<endl;Sleep(1000);cout<<"However, it's really dark. you can only see things near you. It's garunteed that you can get the treasure. "<<endl;Sleep(1000);cout<<"Use up/down/left/right to move. '#' stands for wall and '.' stands for land"<<endl;cout<<"o is yourself. ! stands for treasure. "<<endl;Sleep(1000);cout<<"If you understand the rules, enter 'ready' to start the game. "<<endl;string _;cin>>_;if(_!="ready")return 0;system("cls");cout<<"loading map... (it may take a bit of time)"<<endl;generate_map();system("cls");x=1,y=1;auto start=chrono::high_resolution_clock::now();while(x!=edx || y!=edy){print();int op;while(true){if(_kbhit()){int ch=_getch();18 行未改动}}}if(op==1){//upif(is_valid(x-1,y) && maze[x-1][y]!=3 && maze[x-1][y]!=0)x--;}if(op==2){//downif(is_valid(x+1,y) && maze[x+1][y]!=3 && maze[x+1][y]!=0)x++;}if(op==3){//leftif(is_valid(x,y-1) && maze[x][y-1]!=3 && maze[x][y-1]!=0)y--;}if(op==4){//rightif(is_valid(x,y+1) && maze[x][y+1]!=3 && maze[x][y+1]!=0)y++;}system("cls");auto end=chrono::high_resolution_clock::now();auto duration=chrono::duration_cast<std::chrono::microseconds>(end-start);if(duration.count()>=50000000.0){cout<<"The sands under you starts shifting..."<<endl;Sleep(1000);system("cls");generate_map();x=y=1;start=chrono::high_resolution_clock::now();}}cout<<"WoW! You find the treasure! Congratulations!"<<endl;return 0;}```1234567891011121314151617181920212223242526272829303132333435363738394041428 行未改动5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913014 行未改动1451461471481491501511521531541551561571581591601611621631641651661671687 行未改动1761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222233 行未改动2272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662673 行未改动2712722732742752762772782792802 行未改动28328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934018 行未改动359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452# Change Log- 10/3: finish the first version- 10/4: improve map generator- 10/4: update new mechanism: change the map every 30s (The sands under you starts shifting...)- 10/4: added more obstacles & fixed some bugs- 10/5: fixed output speed bug- 10/5: fixed standard step output bug- 10/5: updated point mechanism- 10/5: improved point mechanism- 10/6: fixed bugs after sand shift- 10/10: added new mechanism: map- 10/10: fixed some bugs & improved gaming experience# Code```cpp//Pyramid Created by seanli1008 on 2025/10/3.//Copyright (c) 2025年 seanli1008. All rights reserved.#include <bits/stdc++.h>#include <windows.h>#include <conio.h>#include <chrono>using namespace std;mt19937 rnd(time(0));const int dx[]={-1,1,0,0};const int dy[]={0,0,-1,1};int x,y,stx,sty,edx,edy,maze[50][50],dis[50][50];bool vis[50][50],chosen[100];bool is_valid(int x,int y){if(x<1 || x>20 || y<1 || y>20)return false;8 行未改动}void debug(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){if(edx==i && edy==j)cout<<"! ";else if(x==i && y==j)cout<<"o ";else if(maze[i][j]==1 || maze[i][j]==2)cout<<". ";else if(maze[i][j]==666)cout<<"M ";elsecout<<"# ";}cout<<endl;}}bool visited[50][50];bool bfs(int a,int b){if(a!=0 && b!=0)maze[a][b]=0;queue<pair<int,int>> que;memset(dis,0x3f,sizeof(dis));memset(visited,false,sizeof(visited));dis[1][1]=0;visited[1][1]=true;que.push({1,1});while(!que.empty()){auto cur=que.front();que.pop();if(cur.first==edx && cur.second==edy)return true;for(int i=0;i<4;i++){int nx=cur.first+dx[i],ny=cur.second+dy[i];if(is_valid(nx,ny) && maze[nx][ny]!=0 && !visited[nx][ny]){dis[nx][ny]=dis[cur.first][cur.second]+1;visited[nx][ny]=true;que.push({nx,ny});}}}if(a!=0 && b!=0)maze[a][b]=1;//cout<<a<<' '<<b<<' '<<dis[edx][edy]<<endl;if(dis[edx][edy]==0x3f3f3f3f)return false;return true;}void generate_map(){memset(vis,false,sizeof(vis));memset(maze,0,sizeof(maze));memset(chosen,false,sizeof(chosen));//=== generate main path ===vector<pair<int,int>> path;x=y=1;stx=sty=1;vis[x][y]=true;maze[x][y]=1;bool flag=true;for(int i=1;i<=60;i++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){if(d==2 || d==0){tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}else{tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}}}int k=tmp.size();14 行未改动path.clear();x=y=1;vis[x][y]=true;maze[x][y]=1;for(int i=1;i<=60;i++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(x+dx[d],y+dy[d]) && !vis[x+dx[d]][y+dy[d]] && !stuck(x+dx[d],y+dy[d])){if(d==2 || d==0){tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}else{tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}}}int k=tmp.size();7 行未改动maze[x][y]=1;path.push_back({x,y});}}edx=x,edy=y;//=== generate main path ===//cout<<"Main Line: "<<endl;//debug();//=== generate branch ===int delta=rnd()%5+1,sign=rnd()%2,tot=12;if(sign==0)tot-=delta;elsetot+=delta;for(int i=1;i<=tot;i++){int pos=rnd()%60;while(chosen[pos] || stuck(path[pos].first,path[pos].second))pos=rnd()%60;int what_the_heck=rnd()%2;if(what_the_heck==0)pos=max(0,min(pos-10,20));chosen[pos]=true;int r=path[pos].first,c=path[pos].second;int dep=rnd()%5+20;int surprise=rnd()%3;if(surprise==0){dep+=25;pos=max(0,min(pos-15,10));}for(int j=1;j<=dep;j++){vector<int> tmp;for(int d=0;d<4;d++){if(is_valid(r+dx[d],c+dy[d]) && !vis[r+dx[d]][c+dy[d]] && maze[r+dx[d]][c+dy[d]]!=1 && maze[r+dx[d]][c+dy[d]]!=2){if(d==2 || d==0){tmp.push_back(d);tmp.push_back(d);}else{tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);tmp.push_back(d);}}}int k=tmp.size();if(k==0)3 行未改动vis[r][c]=true;maze[r][c]=2;}}//=== generate branch ===//cout<<"Branches generated: "<<endl;//debug();//=== generate obstacles to add difficulty ===for(int i=1;i<=20;i++){int pos=rnd()%60,cnt=1;bool ok=true;while(maze[pos]==0 || pos==1 || pos==60 || chosen[pos]==true || !bfs(path[pos].first,path[pos].second)){pos=rnd()%60;cnt++;if(cnt>5){ok=false;break;}}if(ok){maze[path[pos].first][path[pos].second]=0;chosen[pos]=true;}}//cout<<"Obstacles added: "<<endl;//cout<<dis[edx][edy]<<endl;//debug();//=== generate obstacles to add difficulty ===//=== random map pick_up ===int mp_pos_x=rnd()%20+1,mp_pos_y=rnd()%20+1;int surprisingly_hahaha=rnd()%2;while(maze[mp_pos_x][mp_pos_y]==0 || abs(mp_pos_x-edx)+abs(mp_pos_y-edy)<=10 || maze[mp_pos_x][mp_pos_y]==surprisingly_hahaha){mp_pos_x=rnd()%20+1;mp_pos_y=rnd()%20+1;}maze[mp_pos_x][mp_pos_y]=666;//=== random map pick_up ===}void print(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){3 行未改动else if(i==edx && j==edy)cout<<"! ";else if(maze[i][j]==0)cout<<"# ";else if(maze[i][j]==666)cout<<"M ";elsecout<<". ";}else2 行未改动cout<<endl;}}int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<"Pyramid developed by seanli1008"<<endl;Sleep(1000);system("cls");cout<<"If you don't know the rules, enter 'R' to check. If you knew the rule, please enter 'S' to start the game. "<<endl;string op;cin>>op;Sleep(300);system("cls");if(op=="R"){cout<<"You are a treasure hunter. There is a priceless treasure in this pyramid. Please find it. "<<endl;Sleep(1000);cout<<"However, it's really dark. you can only see things near you. It's garunteed that you can get the treasure. "<<endl;Sleep(1000);cout<<"What's even worse, the pyramid will change its map completely every 20s. "<<endl;cout<<"You'd better find the treasure in 20s or you will be sent to the entrance and you have to explore again. "<<endl;Sleep(1000);cout<<"Luckily, there's a map in the pyramid. You can pick up the map and see the whole pyramid for 3s. "<<endl;Sleep(1000);cout<<"Use up/down/left/right to move. '#' stands for wall and '.' stands for land. "<<endl;cout<<"o is yourself. ! stands for treasure. "<<endl;Sleep(1000);cout<<"If you understand the rules, enter 'ready' to start the game. Good Luck!"<<endl;string _;cin>>_;if(_!="ready")return 0;system("cls");}else if(op!="S")return 0;cout<<"loading map... (it may takes a bit of time)"<<endl;Sleep(200);generate_map();system("cls");//debug();//Sleep(5000);//system("cls");x=1,y=1;int cnt=0;auto start=chrono::high_resolution_clock::now();std::chrono::high_resolution_clock::time_point mp_start;bool have_map=false,bonus=false;while(x!=edx || y!=edy){if(!have_map)print();elsedebug();int op;while(true){if(_kbhit()){int ch=_getch();18 行未改动}}}if(op==1){//upif(is_valid(x-1,y) && maze[x-1][y]!=3 && maze[x-1][y]!=0){x--;cnt++;if(maze[x][y]==666 && !bonus){mp_start=chrono::high_resolution_clock::now();have_map=true;bonus=true;}}}else if(op==2){//downif(is_valid(x+1,y) && maze[x+1][y]!=3 && maze[x+1][y]!=0){x++;cnt++;if(maze[x][y]==666 && !bonus){mp_start=chrono::high_resolution_clock::now();have_map=true;bonus=true;}}}else if(op==3){//leftif(is_valid(x,y-1) && maze[x][y-1]!=3 && maze[x][y-1]!=0){y--;cnt++;if(maze[x][y]==666 && !bonus){mp_start=chrono::high_resolution_clock::now();have_map=true;bonus=true;}}}else{//rightif(is_valid(x,y+1) && maze[x][y+1]!=3 && maze[x][y+1]!=0){y++;cnt++;if(maze[x][y]==666 && !bonus){mp_start=chrono::high_resolution_clock::now();have_map=true;bonus=true;}}}system("cls");auto end=chrono::high_resolution_clock::now();auto duration=chrono::duration_cast<std::chrono::microseconds>(end-start);if(duration.count()>=20000000.0){cout<<"The sands under you starts shifting..."<<endl;cout<<"The sands under you starts shifting..."<<endl;cout<<"The sands under you starts shifting..."<<endl;cout<<"The Whole Map: "<<endl;debug();cout<<"The sands under you starts shifting..."<<endl;cout<<"The sands under you starts shifting..."<<endl;cout<<"The sands under you starts shifting..."<<endl;Sleep(5000);system("cls");generate_map();x=y=1;bfs(0,0);cnt=0;start=chrono::high_resolution_clock::now();}auto mp_end=chrono::high_resolution_clock::now();auto mp_duration=chrono::duration_cast<std::chrono::microseconds>(mp_end-mp_start);if(mp_duration.count()>3000000.0)have_map=false;}auto end=chrono::high_resolution_clock::now();auto duration=chrono::duration_cast<std::chrono::microseconds>(end-start);cout<<"WoW! You find the treasure! Congratulations!"<<endl;cout<<"Your time: "<<duration.count()/1000000.0<<"s"<<endl;cout<<"Your total step: "<<cnt<<endl;double mn_time=0.0;if(bonus)mn_time=(double)dis[edx][edy]/3.5;elsemn_time=(double)dis[edx][edy]/3.0;double pts=((mn_time/(duration.count()/1000000.0))*100.0+((double)dis[edx][edy]/2.0)/((double)cnt/2.0)*100.0)/2;if(bonus)pts+=5.0;pts=min(pts,100.0);cout<<"Your point: "<<fixed<<setprecision(2)<<pts<<"/100"<<endl;cout<<"Standard time (only for reference, not very accurate): "<<mn_time<<'s'<<endl;cout<<"Shortest Path: "<<dis[edx][edy]<<endl;cout<<"The Whole Map: "<<endl;debug();return 0;}```