版本比较
永乐大典
正文
增加 126 行删除 73 行
9,829 行未改动全部展开9830983198329833983498359836983758 行未改动989698979898989999009901990299039904114 行未改动全部展开10019100201002110022100231002410025100261002712 行未改动1004010041100421004310044100451004610047100481004910050100511005218 行未改动10071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101415 行未改动10147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101875 行未改动1019310194101951019610197101981019910200102012 行未改动102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022613 行未改动102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102797 行未改动1028710288102891029010291102921029310294102951029610297102981029910300103011030241 行未改动1034410345103461034710348103499,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) {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) {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;return space > threshold;}char followTail(int id) {18 行未改动if(dir=='d') return 3;return -1;}char decideMove(int id) {ll dx[] = {0,0,-1,1};ll dy[] = {-1,1,0,0};char moves[] = {'w','s','a','d'};bool allowRisky = (id==3);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; }}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;}ll bestScore = -1;char bestMove = 'w';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 straightBonus = (lastDir[id]==i) ? 20 : 0;ll turnPenalty = (lastDir[id]!=-1 && lastDir[id]!=i) ? 5 : 0;ll score = space + wallDist*2 + straightBonus - turnPenalty;if(score > bestScore) {bestScore = score;bestMove = moves[i];}}if(bestScore > 0) {lastDir[id] = getDirIndex(bestMove);return bestMove;}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];}}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;}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]);}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]) {k[b.second][b.first] = '$';countapple++;}}// 清空该蛇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);// 移动存活蛇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}); // 增加长度len[id]++;countapple--;ate = true;}// 更新身体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) {pglick = 0;int toAdd = 5;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;}:::::::::9,829 行未改动全部展开98309831983298339834983598369837983858 行未改动989798989899990099019902990399049905114 行未改动全部展开10020100211002210023100241002510026100271002812 行未改动10041100421004310044100451004610047100481004918 行未改动100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101915 行未改动101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102455 行未改动1025110252102531025410255102561025710258102592 行未改动102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028113 行未改动10295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103327 行未改动103401034110342103431034410345103461034710348103491035010351103521035341 行未改动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<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, 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 = len[id] * thresholdMultiplier;return space > threshold;}char followTail(int id) {18 行未改动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];5 行未改动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;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]) {// 身体变苹果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;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] = 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]='.';7 行未改动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]=='.') {41 行未改动cout << "All snakes died. Draw!" << endl;}return 0;}```:::::::::