版本比较
永乐大典
正文
增加 1323 行删除 0 行
9,075 行未改动全部展开907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104029,075 行未改动全部展开}```::::::::::::::success[自制游戏]::::success[扫雷]```cpp#include <bits/stdc++.h>#include <cstdlib>#define ll long longusing namespace std;void CS() {#ifdef _WIN32 system("cls");#else // 假设在 Unix/Linux 系统 std::cout << "\033[2J\033[1;1H";#endif}mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());constexpr int maxn=1e3+10;char a[maxn][maxn];char ans[maxn][maxn];ll nsp;void init(ll n,ll m,ll k){ for(ll i=1;i<=n;++i) { for(ll j=1;j<=m;++j) { a[i][j]='.'; ans[i][j]='.'; } } while(k--) { while(1) { ll x=rnd()%n+1; ll y=rnd()%m+1; if(ans[x][y]=='.') { ans[x][y]='#'; break; } } } for(ll i=1;i<=n;++i) { for(ll j=1;j<=m;++j) { if(ans[i][j]=='#') continue; ll c=0; if(ans[i-1][j-1]=='#') c++; if(ans[i][j-1]=='#') c++; if(ans[i][j+1]=='#') c++; if(ans[i-1][j+1]=='#') c++; if(ans[i+1][j-1]=='#') c++; if(ans[i+1][j]=='#') c++; if(ans[i-1][j]=='#') c++; if(ans[i+1][j+1]=='#') c++; ans[i][j]=c+'0'; } }}ll nk;void print(ll n,ll m,ll op){ cout << "+ " ; for(ll i=1;i<=n;++i) { cout << i << ' '; if(nsp==1) continue; else if(nsp==2 && i<10) cout << ' '; else if(nsp==3 && i<10) cout << ' ' << ' '; else if(nsp==3 && i<100) cout << ' '; } cout << endl << " +" << endl; if(op==0) { for(ll i=1;i<=n;++i) { cout << i << " "; if(i<10) cout << ' '; if(i<100) cout << ' '; for(ll j=1;j<=m;++j) { cout << a[i][j] << " " ; } cout << endl; } } else { for(ll i=1;i<=n;++i) { cout << i << " "; for(ll j=1;j<=m;++j) { cout << ans[i][j] << ' ' ; } cout << endl; } } cout << "Maybe " << nk << " winmine" << (nk>1?"s\n":"\n");}ll k;void bl(ll _,ll b,ll n,ll m){ if(_!=1) { if(ans[_-1][b]=='0' && a[_-1][b]=='.') { a[_-1][b]='0'; bl(_-1,b,n,m); } } if(b!=1) { if(ans[_][b-1]=='0' && a[_][b-1]=='.') { a[_][b-1]='0'; bl(_,b-1,n,m); } } if(_!=n) { if(ans[_+1][b]=='0' && a[_+1][b]=='.') { a[_+1][b]='0'; bl(_+1,b,n,m); } } if(b!=m) { if(ans[_][b+1]=='0' && a[_][b+1]=='.') { a[_][b+1]='0'; bl(_,b+1,n,m); } } if(_!=1 && b!=1) { if(ans[_-1][b-1]=='0' && a[_-1][b-1]=='.') { a[_-1][b-1]='0'; bl(_-1,b-1,n,m); } } if(_!=n && b!=m) { if(ans[_+1][b+1]=='0' && a[_+1][b+1]=='.') { a[_+1][b+1]='0'; bl(_+1,b+1,n,m); } } if(_!=1 && b!=m) { if(ans[_-1][b+1]=='0' && a[_-1][b+1]=='.') { a[_-1][b+1]='0'; bl(_-1,b+1,n,m); } } if(_!=n && b!=1) { if(ans[_+1][b-1]=='0' && a[_+1][b-1]=='.') { a[_+1][b-1]='0'; bl(_+1,b-1,n,m); } }
if(_!=1) { if(ans[_-1][b]!='#' && a[_-1][b]=='.') { a[_-1][b]=ans[_-1][b]; } } if(b!=1) { if(ans[_][b-1]!='#' && a[_][b-1]=='.') { a[_][b-1]=ans[_][b-1]; } } if(_!=n) { if(ans[_+1][b]!='#' && a[_+1][b]=='.') { a[_+1][b]=ans[_+1][b]; } } if(b!=m) { if(ans[_][b+1]!='#' && a[_][b+1]=='.') { a[_][b+1]=ans[_][b+1]; } } if(_!=1 && b!=1) { if(ans[_-1][b-1]!='#' && a[_-1][b-1]=='.') { a[_-1][b-1]=ans[_-1][b-1]; } } if(_!=n && b!=1) { if(ans[_+1][b-1]!='#' && a[_+1][b-1]=='.') { a[_+1][b-1]=ans[_+1][b-1]; } } if(_!=n && b!=m) { if(ans[_+1][b+1]!='#' && a[_+1][b+1]=='.') { a[_+1][b+1]=ans[_+1][b+1]; } } if(_!=1 && b!=m) { if(ans[_-1][b+1]!='#' && a[_-1][b+1]=='.') { a[_-1][b+1]=ans[_-1][b+1]; } }}void mn(ll n,ll m){ ll x,y,op; cin >> op >> x >> y; if(op) { if(a[y][x]=='F') { a[y][x]='.'; nk++; return ; } nk--; a[y][x]='F'; return ; } else { if(ans[y][x]=='#') { CS(); print(n,m,1); cout << endl << "You are game over"; exit(0); } else { a[y][x]=ans[y][x]; if(a[y][x]=='0') { bl(y,x,n,m); } } } for(ll i=1;i<=n;++i) { for(ll j=1;j<=m;++j) { if(ans[i][j]=='#' || a[i][j]==ans[i][j]) continue; else return ; } } CS(); print(n,m,1); cout << endl << "Your Win"; exit(0);}
signed main(){ cout << "What types?(N*M+K)" << endl; ll n,m; cin >> n >> m >> k; nk=k; init(n,m,k);
ll a=n; while(a) { a/=10; nsp++; }
while(1) { CS(); print(n,m,0); mn(n,m); }}```::::::::success[贪吃蛇]:::success[单人]```cpp#include<bits/stdc++.h>using namespace std;using ll = long long;#include <cstdlib>void clears(){#ifdef _WIN32 system("cls");#else system("clear");#endif}#ifdef _WIN32#include <conio.h>bool keyPressed() { return _kbhit(); }char getKey() { return _getch(); }#else#include <termios.h>#include <unistd.h>#include <fcntl.h>#endifconstexpr int maxn=1010;ll N,M;char k[maxn][maxn];mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());ll len;ll TPx,TPy;vector<pair<ll,ll>> body;ll rou=0;ll countapple;void init(){ len=1; TPx=(M+1)/2; TPy=(N+1)/2; for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) { k[i][j]='.'; if(i==TPx && j==TPy) k[i][j]='@'; } while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } countapple=1;}void print(){ for(ll i=1;i<=N;++i) { for(ll j=1;j<=M;++j) cout << k[i][j]; cout << endl; } cout << "Round:" << rou<< endl; rou++; cout << "Score" << (len>2?"s":"") << ": " << len-1 << endl;}ll pglick=0;ll op;void mn(){ char ch; string s; ll f1=TPx,f2=TPy; getline(cin,s); ch=s[0]; if(ch=='w') { op=1; } else if(ch=='s') { op=2; } else if(ch=='a') { op=3; } else if(ch=='d') { op=4; } if(op==1) TPy--; else if(op==2) TPy++; else if(op==3) TPx--; else if(op==4) TPx++; if(k[TPy][TPx]=='$') { k[TPy][TPx]='@'; body.emplace_back(0,0); len++; countapple--; } for(auto &i:body) { ll u=i.first; ll v=i.second; i.first=f1; i.second=f2; f1=u; f2=v; } for(ll i=1;i<=N;++i) { for(ll j=1;j<=M;++j) { if(k[i][j]=='$') continue; k[i][j]='.'; if(j==TPx && i==TPy) k[i][j]='@'; } } for(auto &i:body) { k[i.second][i.first]='#'; } pglick++; if(pglick>=3 && countapple<5 && countapple+len<N*M) { pglick=0; while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } countapple++; }}bool ntgameover(){ if(len==N*M) { clears(); print(); cout << "WIN!!!"; exit(0); } if(TPx==M+1 || TPx==0 || TPy==0 || TPy==N+1) return 0; for(auto i:body) { if(i.first==TPx && i.second==TPy) { return 0; } } return 1;}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout << "Please write the size of game" << endl; cin >> N >> M; cin.ignore(); clears(); init(); while(ntgameover()) { clears(); print(); mn(); } cout << endl << endl << "GAME OVER";}```::::::success[双人]```cpp#include<bits/stdc++.h>using namespace std;using ll = long long;#include <cstdlib>void clears(){#ifdef _WIN32 system("cls");#else system("clear");#endif}#ifdef _WIN32#include <conio.h>bool keyPressed() { return _kbhit(); }char getKey() { return _getch(); }#else#include <termios.h>#include <unistd.h>#include <fcntl.h>#endifconstexpr int maxn=1010;ll N,M;char k[maxn][maxn];mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());ll len1,len2;//%ll P1x,P1y;ll P2x,P2y;vector<pair<ll,ll>> body1,body2;void init(){ P1x=1; P1y=1; P2x=M; P2y=N; for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) { k[i][j]='.'; if(i==P1y && j==P1x) k[i][j]='@'; if(i==P2y && j==P2x) k[i][j]='%'; } while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } len1=1; for(ll i=1;i<len1;++i) body1.emplace_back(0,0); len2=1; for(ll i=1;i<len2;++i) body2.emplace_back(0,0); ll T=0; while(T--) { got:; ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='?'; continue; } goto got; }}void print(){ for(ll i=1;i<=N;++i) { for(ll j=1;j<=M;++j) cout << k[i][j]; cout << endl; } cout << "Score1" << (len1>2?"s":"") << ": " << len1-1 << endl; cout << "Score2" << (len2>2?"s":"") << ": " << len2-1 << endl;}ll pglick=0;ll countapple=1;ll op1,op2;void mn(){ char ch; string s; ll f1=P1x,f2=P1y; ll f3=P2x,f4=P2y; getline(cin,s); for(auto ch:s){ if(ch=='w') { op1=1; } else if(ch=='s') { op1=2; } else if(ch=='a') { op1=3; } else if(ch=='d') { op1=4; } if(ch=='i') { op2=1; } else if(ch=='k') { op2=2; } else if(ch=='j') { op2=3; } else if(ch=='l') { op2=4; } } if(op1==1) P1y--; else if(op1==2) P1y++; else if(op1==3) P1x--; else if(op1==4) P1x++; if(op2==1) P2y--; else if(op2==2) P2y++; else if(op2==3) P2x--; else if(op2==4) P2x++; if(k[P1y][P1x]=='$') { k[P1y][P1x]='@'; body1.emplace_back(0,0); len1++; countapple--; } if(k[P2y][P2x]=='$') { k[P2y][P2x]='%'; body2.emplace_back(0,0); len2++; countapple--; } ll u,v; for(auto &i:body1) { u=i.first; v=i.second; i.first=f1; i.second=f2; f1=u; f2=v; } for(auto &i:body2) { u=i.first; v=i.second; i.first=f3; i.second=f4; f3=u; f4=v; } for(ll i=1;i<=N;++i) { for(ll j=1;j<=M;++j) { if(k[i][j]=='$' ||k[i][j]=='?') continue; k[i][j]='.'; if(j==P1x && i==P1y) k[i][j]='@'; if(j==P2x && i==P2y) k[i][j]='%'; } } pglick++; if(pglick>=5 /*&& countapple<=9 */&& countapple+len1<N*M) { pglick-=5; while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } countapple++; while(1) { ll x__=rnd()%N+1; ll y__=rnd()%M+1; if(k[x__][y__]=='.') { k[x__][y__]='$'; break; } } countapple++; } for(auto i:body1) { k[i.second][i.first]='#'; } for(auto i:body2) { k[i.second][i.first]='O'; }}ll ntgameover(){ if(P1x==P2x && P1y==P2y) return 0; if(P1x==M+1 || P1x==0 || P1y==0 || P1y==N+1) return 1; if(P2x==M+1 || P2x==0 || P2y==0 || P2y==N+1) return 2; for(auto i:body1) { if(i.first==P1x && i.second==P1y) { return 1; } if(i.first==P2x && i.second==P2y) { return 2; } } for(auto i:body2) { if(i.first==P1x && i.second==P1y) { return 1; } if(i.first==P2x && i.second==P2y) { return 2; } } return 3;}int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout << "Please write the size of game" << endl; cin >> N >> M; cin.ignore(); clears(); init(); ll t; while(t=ntgameover()) { if(t!=3) break; clears(); print(); mn(); } clears(); if(!t) cout << "Head meet head"; else if(t==1) cout << "P2 Wins"; else if(t==2) cout << "P1 Wins";}```::::::success[养蛊]```#include<bits/stdc++.h>using namespace std;using ll = long long;#include <cstdlib>
void clears(){#ifdef _WIN32 system("cls");#else system("clear");#endif}
#ifdef _WIN32#include <conio.h>bool keyPressed() { return _kbhit(); }char getKey() { return _getch(); }#else#include <termios.h>#include <unistd.h>#include <fcntl.h>#endif
constexpr int maxn=1010;ll N,M;char k[maxn][maxn];mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll len[5];ll Px[5], Py[5];vector<pair<ll,ll>> body[5];ll lastDir[5];bool alive[5];
char headChar[5] = {' ', '@', '%', '/', '?'};char bodyChar[5] = {' ', '#', 'O', '+', '-'};
ll countapple = 0;ll pglick = 0;
bool inBounds(ll x, ll y) { return x>=1 && x<=M && y>=1 && y<=N;}
void init(){ Px[1]=1; Py[1]=1; Px[2]=M; Py[2]=1; Px[3]=1; Py[3]=N; Px[4]=M; Py[4]=N;
for(int id=1; id<=4; ++id) { len[id]=1; body[id].clear(); lastDir[id]=-1; alive[id]=true; }
for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) k[i][j]='.';
for(int id=1; id<=4; ++id) { k[Py[id]][Px[id]] = headChar[id]; }
for(int t=0; t<4; ++t) { while(1) { ll x = rnd()%M+1; ll y = rnd()%N+1; if(k[y][x]=='.') { k[y][x]='$'; break; } } } countapple = 5; pglick = 0;}
void print(){ for(ll i=1;i<=N;++i) { for(ll j=1;j<=M;++j) cout << k[i][j]; cout << endl; } for(int id=1; id<=4; ++id) { if(alive[id]) { cout << "P" << id << " Score: " << len[id]-1; } else { cout << "P" << id << " Dead"; } if(id<4) cout << " | "; } cout << endl; cout << "Apples: " << countapple << endl;}
bool isOccupiedByOther(ll x, ll y, int selfId, bool includeHead=true) { for(int id=1; id<=4; ++id) { if(id == selfId || !alive[id]) continue; if(includeHead && Px[id]==x && Py[id]==y) return true; for(auto& b : body[id]) { if(b.first==x && b.second==y) return true; } } return false;}
bool isOccupiedBySelf(ll x, ll y, int selfId, bool includeTail=true) { ll end = includeTail ? body[selfId].size() : (body[selfId].size()>0 ? body[selfId].size()-1 : 0); for(ll i=0; i<end; ++i) { if(body[selfId][i].first==x && body[selfId][i].second==y) return true; } return false;}
bool isSafeFor(int id, ll x, ll y, bool includeTail=true, bool allowRisky=false) { if(!inBounds(x,y)) return false; if(isOccupiedByOther(x,y,id,true)) return false; if(isOccupiedBySelf(x,y,id,includeTail)) return false; if(!allowRisky) { for(int oid=1; oid<=4; ++oid) { if(oid==id || !alive[oid]) continue; if(abs(x-Px[oid]) + abs(y-Py[oid]) == 1) return false; } } return true;}
vector<pair<ll,ll>> findPath(int id, pair<ll,ll> start, pair<ll,ll> target, bool includeTail=false, bool allowRisky=false) { if(start == target) return {}; vector<vector<bool>> visited(N+2, vector<bool>(M+2, false)); vector<vector<pair<ll,ll>>> parent(N+2, vector<pair<ll,ll>>(M+2, {-1,-1})); queue<pair<ll,ll>> q; ll dx[] = {0,0,-1,1}; ll dy[] = {-1,1,0,0}; q.push(start); visited[start.second][start.first]=true; while(!q.empty()) { auto cur = q.front(); q.pop(); if(cur == target) { vector<pair<ll,ll>> path; while(cur != start) { path.push_back(cur); cur = parent[cur.second][cur.first]; if(cur.first==-1 && cur.second==-1) break; } reverse(path.begin(), path.end()); return path; } for(int i=0; i<4; ++i) { ll nx = cur.first + dx[i]; ll ny = cur.second + dy[i]; if(!inBounds(nx,ny)) continue; if(visited[ny][nx]) continue; if(!isSafeFor(id, nx, ny, includeTail, allowRisky)) continue; visited[ny][nx]=true; parent[ny][nx]=cur; q.push({nx,ny}); } } return {};}
ll countReachableSpace(int id, ll x, ll y, bool includeTail=false) { vector<vector<bool>> visited(N+2, vector<bool>(M+2, false)); queue<pair<ll,ll>> q; q.push({x,y}); visited[y][x]=true; ll cnt=0; ll dx[] = {0,0,-1,1}; ll dy[] = {-1,1,0,0}; while(!q.empty()) { auto cur = q.front(); q.pop(); cnt++; for(int i=0; i<4; ++i) { ll nx = cur.first + dx[i]; ll ny = cur.second + dy[i]; if(!inBounds(nx,ny)) continue; if(visited[ny][nx]) continue; if(!isSafeFor(id, nx, ny, includeTail, false)) continue; visited[ny][nx]=true; q.push({nx,ny}); } } return cnt;}
bool isPathSafe(int id, vector<pair<ll,ll>>& path, pair<ll,ll> target, double thresholdMultiplier) { if(path.empty()) return false; ll simX = Px[id], simY = Py[id]; vector<pair<ll,ll>> simBody = body[id]; ll simLen = len[id]; for(auto& step : path) { bool ate = (step == target); if(!simBody.empty()) { simBody.insert(simBody.begin(), {simX, simY}); if(!ate) { if(simBody.size() > simLen) simBody.pop_back(); } else simLen++; } simX = step.first; simY = step.second; } auto oldBody = body[id]; ll oldLen = len[id]; body[id] = simBody; len[id] = simLen; ll space = countReachableSpace(id, simX, simY, false); body[id] = oldBody; len[id] = oldLen; ll threshold = len[id] * thresholdMultiplier; return space > threshold;}
char followTail(int id) { if(body[id].empty()) return 0; pair<ll,ll> tail = body[id].back(); auto start = make_pair(Px[id], Py[id]); auto path = findPath(id, start, tail, true, false); if(!path.empty() && path.size()>1) { auto next = path[0]; if(next.second < Py[id]) return 'w'; if(next.second > Py[id]) return 's'; if(next.first < Px[id]) return 'a'; if(next.first > Px[id]) return 'd'; } return 0;}
ll getDirIndex(char dir) { if(dir=='w') return 0; if(dir=='s') return 1; if(dir=='a') return 2; if(dir=='d') return 3; return -1;}
ll manhattan(ll x1, ll y1, ll x2, ll y2) { return abs(x1-x2) + abs(y1-y2);}
// 统一评估函数:用于P4比较不同性格产生的方向ll evaluateMove(int id, ll nx, ll ny) { ll space = countReachableSpace(id, nx, ny, false); ll wallDist = min(min(nx, M-nx+1), min(ny, N-ny+1)); // 食物吸引 ll foodBonus = 0; vector<pair<ll,ll>> foods; for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) if(k[i][j]=='$') foods.push_back({j,i}); if(!foods.empty()) { pair<ll,ll> nearest = foods[0]; ll minDist = manhattan(Px[id], Py[id], nearest.first, nearest.second); for(auto& f : foods) { ll d = manhattan(Px[id], Py[id], f.first, f.second); if(d < minDist) { minDist = d; nearest = f; } } ll curDist = minDist; ll newDist = manhattan(nx, ny, nearest.first, nearest.second); if(newDist < curDist) foodBonus = 15; else if(newDist > curDist) foodBonus = -5; } // 方向变化 ll straightBonus = (lastDir[id] == getDirIndex('w') && nx == Px[id] && ny == Py[id]-1) ? 5 : 0; // 简化处理,实际可忽略 // 这里简化为空间+边界+食物 return space + wallDist*2 + foodBonus;}
// 执行一种性格的决策,返回方向和评分pair<char, ll> decideWithPersonality(int id, int personality) { ll dx[] = {0,0,-1,1}; ll dy[] = {-1,1,0,0}; char moves[] = {'w','s','a','d'}; bool allowRisky = false; double thresholdMultiplier = 1.0; if (personality == 1) { // 均衡 allowRisky = false; thresholdMultiplier = 1.2; } else if (personality == 2) { // 保守 allowRisky = false; thresholdMultiplier = 1.5; } else if (personality == 3) { // 激进 allowRisky = true; thresholdMultiplier = 1.0; }
// 收集苹果 vector<pair<ll,ll>> foods; for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) if(k[i][j]=='$') foods.push_back({j,i});
// 尝试吃苹果 if(!foods.empty()) { sort(foods.begin(), foods.end(), [&](const pair<ll,ll>& a, const pair<ll,ll>& b) { return manhattan(Px[id], Py[id], a.first, a.second) < manhattan(Px[id], Py[id], b.first, b.second); }); for(auto& food : foods) { auto path = findPath(id, {Px[id],Py[id]}, food, false, allowRisky); if(!path.empty() && isPathSafe(id, path, food, thresholdMultiplier)) { auto next = path[0]; char dir = (next.second < Py[id] ? 'w' : next.second > Py[id] ? 's' : next.first < Px[id] ? 'a' : 'd'); return {dir, 1000000}; // 吃苹果最高优先级 } } }
// 追尾 char tailMove = followTail(id); if(tailMove) { return {tailMove, 500000}; // 追尾次优先 }
// 方向评分 ll bestScore = -1; char bestMove = 'w'; pair<ll,ll> nearestFood = foods.empty() ? make_pair(Px[id], Py[id]) : foods[0]; ll curDistToFood = foods.empty() ? 0 : manhattan(Px[id], Py[id], nearestFood.first, nearestFood.second);
for(int i=0; i<4; ++i) { ll nx = Px[id] + dx[i]; ll ny = Py[id] + dy[i]; if(!isSafeFor(id, nx, ny, false, allowRisky)) continue; ll space = countReachableSpace(id, nx, ny, false); ll wallDist = min(min(nx, M-nx+1), min(ny, N-ny+1)); ll foodBonus = 0; if(!foods.empty()) { ll newDist = manhattan(nx, ny, nearestFood.first, nearestFood.second); if(newDist < curDistToFood) foodBonus = 15; else if(newDist > curDistToFood) foodBonus = -5; } ll straightBonus = (lastDir[id]==i) ? 5 : 0; ll turnPenalty = (lastDir[id]!=-1 && lastDir[id]!=i) ? 2 : 0; ll score = space + wallDist*2 + foodBonus + straightBonus - turnPenalty; if(score > bestScore) { bestScore = score; bestMove = moves[i]; } } if(bestScore > 0) { return {bestMove, bestScore}; }
// 随机安全方向 for(int i=0; i<4; ++i) { ll nx = Px[id] + dx[i]; ll ny = Py[id] + dy[i]; if(isSafeFor(id, nx, ny, true, allowRisky)) { return {moves[i], 0}; } } // 保底 ll maxWallDist = -1; char fallback = 'w'; for(int i=0; i<4; ++i) { ll nx = Px[id] + dx[i]; ll ny = Py[id] + dy[i]; if(inBounds(nx,ny)) { ll wd = min(min(nx, M-nx+1), min(ny, N-ny+1)); if(wd > maxWallDist) { maxWallDist = wd; fallback = moves[i]; } } } return {fallback, -1};}
// 统一决策函数char decideMove(int id) { if (id != 4) { // 固定性格 int personality = id; auto result = decideWithPersonality(id, personality); lastDir[id] = getDirIndex(result.first); return result.first; } else { // P4:结合三种性格,选择评分最高的方向 vector<pair<char, ll>> candidates; for (int p = 1; p <= 3; ++p) { auto res = decideWithPersonality(id, p); candidates.push_back(res); } // 选择评分最高的 ll bestScore = -1e18; char bestDir = 'w'; for (auto& c : candidates) { if (c.second > bestScore) { bestScore = c.second; bestDir = c.first; } } lastDir[id] = getDirIndex(bestDir); return bestDir; }}
void processDeaths() { bool dead[5] = {false}; // 边界碰撞 for(int id=1; id<=4; ++id) { if(!alive[id]) continue; if(!inBounds(Px[id], Py[id])) dead[id] = true; else if(isOccupiedBySelf(Px[id], Py[id], id, false)) dead[id] = true; } // 撞其他蛇身体 for(int id=1; id<=4; ++id) { if(!alive[id] || dead[id]) continue; for(int oid=1; oid<=4; ++oid) { if(oid==id || !alive[oid]) continue; for(auto& b : body[oid]) { if(Px[id]==b.first && Py[id]==b.second) { dead[id] = true; break; } } if(dead[id]) break; } } // 头头相撞 for(int i=1; i<=4; ++i) { if(!alive[i] || dead[i]) continue; for(int j=i+1; j<=4; ++j) { if(!alive[j] || dead[j]) continue; if(Px[i]==Px[j] && Py[i]==Py[j]) { dead[i] = dead[j] = true; } } }
// 将死亡蛇变为苹果 for(int id=1; id<=4; ++id) { if(alive[id] && dead[id]) { // 身体变苹果 for(auto& b : body[id]) { if(inBounds(b.first, b.second) && k[b.second][b.first] == bodyChar[id] && countapple<=N+M && len[1]+len[2]+len[3]+len[4]+countapple+10<=N*M) { k[b.second][b.first] = '$'; countapple++; len[id]--; } } // 清空 alive[id] = false; body[id].clear(); len[id] = 0; Px[id] = Py[id] = -1; } }}
void mn(){ ll oldX[5], oldY[5]; for(int id=1; id<=4; ++id) { if(!alive[id]) continue; oldX[id] = Px[id]; oldY[id] = Py[id]; }
char move[5]; if(alive[1]) move[1] = decideMove(1); if(alive[2]) move[2] = decideMove(2); if(alive[3]) move[3] = decideMove(3); if(alive[4]) move[4] = decideMove(4);
for(int id=1; id<=4; ++id) { if(!alive[id]) continue; if(move[id]=='w') Py[id]--; else if(move[id]=='s') Py[id]++; else if(move[id]=='a') Px[id]--; else if(move[id]=='d') Px[id]++; }
for(int id=1; id<=4; ++id) { if(!alive[id]) continue; if(inBounds(Px[id], Py[id]) && k[Py[id]][Px[id]]=='$') { k[Py[id]][Px[id]] = headChar[id]; body[id].push_back({0,0}); len[id]++; countapple--; } }
for(int id=1; id<=4; ++id) { if(!alive[id]) continue; if(!body[id].empty()) { for(ll i=body[id].size()-1; i>0; --i) { body[id][i] = body[id][i-1]; } body[id][0] = {oldX[id], oldY[id]}; } }
for(ll i=1;i<=N;++i) for(ll j=1;j<=M;++j) { if(k[i][j]=='$') continue; k[i][j]='.'; } for(int id=1; id<=4; ++id) { if(!alive[id]) continue; if(inBounds(Px[id], Py[id])) k[Py[id]][Px[id]] = headChar[id]; for(auto& b : body[id]) { if(inBounds(b.first, b.second)) k[b.second][b.first] = bodyChar[id]; } }
processDeaths();
pglick++; if(pglick >= 5 && countapple < N+M && countapple+len[1]+len[2]+len[3]+len[4]+2<=N*M) { pglick = 0; int toAdd = 2; while(toAdd > 0) { ll x = rnd()%M+1; ll y = rnd()%N+1; if(k[y][x]=='.') { k[y][x]='$'; countapple++; toAdd--; } } }}
ll aliveCount() { ll cnt = 0; for(int id=1; id<=4; ++id) if(alive[id]) cnt++; return cnt;}
int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout << "Please write the size of game (rows N, cols M):" << endl; cin >> N >> M; cin.ignore(); clears(); init(); ll delayMs; cout << "Delay in ms (0 for fastest): "; cin >> delayMs; while(aliveCount() >= 2) { clears(); print(); mn(); if(delayMs > 0) this_thread::sleep_for(chrono::milliseconds(delayMs)); } clears(); print(); if(aliveCount() == 1) { for(int id=1; id<=4; ++id) if(alive[id]) { cout << "Winner: P" << id << "!" << endl; break; } } else { cout << "All snakes died. Draw!" << endl; } return 0;}
```:::::::::