版本比较
永乐大典
正文
增加 126 行删除 73 行
9,829 行未改动全部展开98309831983298339834983598369837983858 行未改动9897989898999900990099019902990399049905114 行未改动全部展开1002010021100221002310023100241002510026100271002812 行未改动100411004210043100441004410045100461004710048100451004610047100481004918 行未改动100681006910070100711007510072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108100791010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291008710088100891009010091101301013110132101331013410135101361013710138101391014010093100941009510096100971009810099101411014210143101441014510104101051014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661011610117101181016710168101691017010171101721017310174101751012510126101761017710178101791018010181101821018310133101341018410185101861013710187101881018910190101915 行未改动101971019810199102001015110152102011020210203101551015610157101581015910160101611016210204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010164101651016610167101681016910170101711023110232101741023310234101771023510236102371023810239102401018310241102421024310244102455 行未改动102511025210253102541019710255102561025710258102592 行未改动1026210263102641026510208102661026710268102111021210213102141021510269102701021710271102721027310274102751027610222102771027810279102801028113 行未改动102951029610297102981024410299103001024610301103021030310304103051030610307103081025510309103101025810311103121026110313103141031510264103161026610317103181031910320103211032210323103241032510326103271032810275103291033010331103327 行未改动10340103411034210343102911034410345102941034610296103471034810298103491035010351103521035341 行未改动10395103961039710398103991040010401104029,829 行未改动全部展开}```::::::success[养蛊]```#include<bits/stdc++.h>using namespace std;using ll = long long;#include <cstdlib>58 行未改动 for(int id=1; id<=4; ++id) { k[Py[id]][Px[id]] = headChar[id]; }
for(int t=0; t<5; ++t) { for(int t=0; t<4; ++t) { while(1) { ll x = rnd()%M+1; ll y = rnd()%N+1; if(k[y][x]=='.') {114 行未改动全部展开 } return cnt;}
bool isPathSafe(int id, vector<pair<ll,ll>>& path, pair<ll,ll> target) {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];12 行未改动 len[id] = simLen; ll space = countReachableSpace(id, simX, simY, false); body[id] = oldBody; len[id] = oldLen; ll threshold; if(id==1) threshold = len[id]*1.8; else if(id==2) threshold = len[id]*2.2; else if(id==3) threshold = len[id]*1.3; else threshold = len[id]*2.0; ll threshold = len[id] * thresholdMultiplier; return space > threshold;}
char followTail(int id) {18 行未改动 if(dir=='d') return 3; return -1;}
char decideMove(int id) {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 = (id==3); 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()) { pair<ll,ll> nearest = foods[0]; ll minDist = abs(Px[id]-nearest.first) + abs(Py[id]-nearest.second); for(auto& f : foods) { ll d = abs(Px[id]-f.first) + abs(Py[id]-f.second); if(d < minDist) { minDist=d; nearest=f; } 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}; // 吃苹果最高优先级 } } auto path = findPath(id, {Px[id],Py[id]}, nearest, false, allowRisky); if(!path.empty() && isPathSafe(id, path, nearest)) { auto next = path[0]; char dir = (next.second < Py[id] ? 'w' : next.second > Py[id] ? 's' : next.first < Px[id] ? 'a' : 'd'); lastDir[id] = getDirIndex(dir); return dir; } }
// 追尾 char tailMove = followTail(id); if(tailMove) { lastDir[id] = getDirIndex(tailMove); return 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) ? 20 : 0; ll turnPenalty = (lastDir[id]!=-1 && lastDir[id]!=i) ? 5 : 0; ll score = space + wallDist*2 + straightBonus - turnPenalty; 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) { lastDir[id] = getDirIndex(bestMove); return bestMove; 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)) { lastDir[id] = i; return moves[i]; return {moves[i], 0}; } }
// 保底 ll maxWallDist = -1; char fallback = 'w'; for(int i=0; i<4; ++i) { ll nx = Px[id] + dx[i];5 行未改动 fallback = moves[i]; } } } lastDir[id] = getDirIndex(fallback); return fallback; return {fallback, -1};}
char decideMoveRandom(int id) { ll dx[] = {0,0,-1,1}; ll dy[] = {-1,1,0,0}; char moves[] = {'w','s','a','d'}; vector<char> safeDirs; for(int i=0; i<4; ++i) { if(isSafeFor(id, Px[id]+dx[i], Py[id]+dy[i], true, false)) safeDirs.push_back(moves[i]);// 统一决策函数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; } if(!safeDirs.empty()) { char dir = safeDirs[rnd()%safeDirs.size()]; lastDir[id] = getDirIndex(dir); return dir; } char dir = moves[rnd()%4]; lastDir[id] = getDirIndex(dir); return dir;}
// 处理死亡蛇,将其身体变为苹果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;5 行未改动 } 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;2 行未改动 } } }
// 将死亡蛇的身体变为苹果 // 将死亡蛇变为苹果 for(int id=1; id<=4; ++id) { if(alive[id] && dead[id]) { // 将头部所在位置(如果在地图内)和身体都变成苹果 if(inBounds(Px[id], Py[id]) && k[Py[id]][Px[id]] == headChar[id]) { k[Py[id]][Px[id]] = '$'; countapple++; } // 身体变苹果 for(auto& b : body[id]) { if(inBounds(b.first, b.second) && k[b.second][b.first] == bodyChar[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;13 行未改动 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] = decideMoveRandom(4); 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; bool ate = false; if(inBounds(Px[id], Py[id]) && k[Py[id]][Px[id]]=='$') { k[Py[id]][Px[id]] = headChar[id]; body[id].push_back({0,0}); // 增加长度 body[id].push_back({0,0}); len[id]++; countapple--; ate = true; } // 更新身体 }
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]='.';7 行未改动 k[b.second][b.first] = bodyChar[id]; } }
// 处理死亡 processDeaths();
// 每3回合生成5个苹果 pglick++; if(pglick >= 3) { if(pglick >= 5 && countapple < N+M && countapple+len[1]+len[2]+len[3]+len[4]+2<=N*M) { pglick = 0; int toAdd = 5; int toAdd = 2; while(toAdd > 0) { ll x = rnd()%M+1; ll y = rnd()%N+1; if(k[y][x]=='.') {41 行未改动 cout << "All snakes died. Draw!" << endl; } return 0;}
```:::::::::