From ffe2271b6c05011c2d182cf2f4dc5786526d0bda Mon Sep 17 00:00:00 2001 From: lcw Date: Thu, 8 Feb 2024 22:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=96=87=E6=A1=A3=E4=B8=AD=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BA=86=E5=AF=BC=E5=85=A5=E5=9C=B0=E5=9B=BE=E7=9A=84?= =?UTF-8?q?=E9=A6=96=E9=83=BD=E5=88=86=E9=85=8D=E6=9C=BA=E5=88=B6=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E7=9B=B8=E5=BA=94=E6=9B=B4=E6=96=B0=E4=BA=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/processor.md | 19 ++++-- processor/logic/terrain.cpp | 126 +++++++++++++++++++++--------------- 2 files changed, 87 insertions(+), 58 deletions(-) diff --git a/docs/processor.md b/docs/processor.md index 6042ad6..dae89fb 100644 --- a/docs/processor.md +++ b/docs/processor.md @@ -474,12 +474,19 @@ m, ,gA99,-12,s,n-10,g 1,L_10,L_n5 若队伍标识数量少于队伍数量,则随机选择队伍数量减去队伍标识数量个队伍,并将选出的队伍中的全部玩家标记为“剩余玩家”,选出的队伍不参与队伍标识分配。对于剩余玩家,创建一个候选列表,从大到小枚举优先级,并将其全部为该优先级的剩余首都都加入候选列表中,直到候选列表中包含至少等于剩余玩家数量个候选首都为止。然后在列表中随机选取作为剩余伍玩家的首都。若剩余玩家数多于剩余首都数,则在空地中随机选取一些地方作为剩余玩家的首都。在随机选择空地为首都时,至少要满足以下条件: -1. 将山区和首都视为不可走的点时,生成的地图中的空地、要塞和沼泽联通; -2. 没有两名玩家的首都相邻。 +1. 将山区视为不可走的点时,生成的地图中的首都、空地、要塞和沼泽连通; +2. 将山区和首都视为不可走的点时,生成的地图中的空地、要塞和沼泽连通; +3. 没有两名玩家的首都相邻。 + +若无力进行合适的首都分配,则应生成随机地图。但当以下条件都满足时,应确保能进行合适的首都分配: + +1. 地图中任意候选首都都不相邻; +2. 队伍数量不超过队伍标识数量; +3. 每个队伍的玩家数量都不超过每个队伍标识所包含的全部的候选首都数量。 #### 错误处理 -不能保证给出的地图总是正确,请判断格式不正确的地图。 +不能保证给出的地图总是正确,请判断格式不正确的地图(若将山区视为不可走的点时,导入地图中的候选首都、空地、要塞和沼泽不连通,则视为该导入地图格式不正确)。 ### 随机地图的生成参数 @@ -491,7 +498,7 @@ m, ,gA99,-12,s,n-10,g 1,L_10,L_n5 值得注意的是,不论使用何种生成算法,应当至少满足以下条件: -1. 将山区和要塞视为不可走的点时,生成的地图中的空地和沼泽联通; +1. 将山区和要塞视为不可走的点时,生成的地图中的空地和沼泽连通; 2. 地图包括至少 $16$ 个空地。 下面假设地图的大小为 $h$ 行 $w$ 列。 @@ -511,7 +518,7 @@ $$ 第一步,在地图上随机选择恰好 $p_m$ 个格子,将类型设置为山区,剩下的格子的类型设置为空地。 -第二步,寻找一个将尽可能少的山区变为空地的方案,使得空地联通。 +第二步,寻找一个将尽可能少的山区变为空地的方案,使得空地连通。 第三步,在山区中随机选择 $p_c$ 个格子,将这些山区的格子变为要塞。并在空地中选择 $p_s$ 个格子,将它们变为沼泽。 @@ -525,7 +532,7 @@ $$ 值得注意的是,不论使用何种分配算法,应当至少满足以下条件: -1. 将山区和首都视为不可走的点时,生成的地图中的空地、要塞和沼泽联通; +1. 将山区和首都视为不可走的点时,生成的地图中的空地、要塞和沼泽连通; 2. 玩家的首都应当总是分配在空地上; 3. 没有两名玩家的首都相邻。 diff --git a/processor/logic/terrain.cpp b/processor/logic/terrain.cpp index b2fef20..eac4001 100644 --- a/processor/logic/terrain.cpp +++ b/processor/logic/terrain.cpp @@ -34,61 +34,63 @@ void upmin(T& x, const T& y) { x = y; } +const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; + +bool findCut(pos_t w, pos_t h, vector> &vertex_type) { + int idx = 0; + vector> dfn(h, vector(w)), low = dfn; + function tarjan = + [&](int x, int y, int fx, int fy, bool rt) { + dfn[x][y] = low[x][y] = ++idx; + int ch = 0; + for (int i = 0; i < 4; i++) { + const int xx = x + dx[i], yy = y + dy[i]; + if (xx < 0 || xx >= h || yy < 0 || yy >= w) + continue; + + if (vertex_type[xx][yy] == VertexType::Capital) + vertex_type[x][y] = VertexType::Cut; + + if (ban(vertex_type[xx][yy]) || (xx == fx && yy == fy)) + continue; + + if (!dfn[xx][yy]) { + ch++; + tarjan(xx, yy, x, y, 0); + upmin(low[x][y], low[xx][yy]); + if (!rt && low[xx][yy] >= dfn[x][y]) + vertex_type[x][y] = VertexType::Cut; + } else { + upmin(low[x][y], dfn[xx][yy]); + } + } + + if (rt && ch >= 2) + vertex_type[x][y] = VertexType::Cut; + }; + + bool flag = false; + for (pos_t i = 0; i < h; i++) { + for (pos_t j = 0; j < w; j++) { + if (!ban(vertex_type[i][j]) && !dfn[i][j]) { + if (flag) + return false; + flag = 1, tarjan(i, j, -1, -1, 1); + } + } + } + return true; +}; + pair> generateCapital( pos_t w, pos_t h, vector>& vertex_type, int r) { - auto findCut = [&]() { - int idx = 0; - vector> dfn(h, vector(w)), low = dfn; - function tarjan = - [&](int x, int y, int fx, int fy, bool rt) { - dfn[x][y] = low[x][y] = ++idx; - int ch = 0; - for (int i = 0; i < 4; i++) { - static const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; - const int xx = x + dx[i], yy = y + dy[i]; - if (xx < 0 || xx >= h || yy < 0 || yy >= w) - continue; - - if (vertex_type[xx][yy] == VertexType::Capital) - vertex_type[x][y] = VertexType::Cut; - - if (ban(vertex_type[xx][yy]) || (xx == fx && yy == fy)) - continue; - - if (!dfn[xx][yy]) { - ch++; - tarjan(xx, yy, x, y, 0); - upmin(low[x][y], low[xx][yy]); - if (!rt && low[xx][yy] >= dfn[x][y]) - vertex_type[x][y] = VertexType::Cut; - } else { - upmin(low[x][y], dfn[xx][yy]); - } - } - - if (rt && ch >= 2) - vertex_type[x][y] = VertexType::Cut; - }; - - bool flag = false; - for (pos_t i = 0; i < h; i++) { - for (pos_t j = 0; j < w; j++) { - if (!ban(vertex_type[i][j]) && !dfn[i][j]) { - if (flag) - return false; - flag = 1, tarjan(i, j, -1, -1, 1); - } - } - } - return true; - }; vector res(r); for (int i = 0; i <= r; i++) { - if (!findCut()) + if (!findCut(w, h, vertex_type)) return {false, vector()}; if (i == r) @@ -164,6 +166,21 @@ GameBoard ImportedTerrain::makeGameBoard(const InitInfo& init_info) { vector rest2; arrange(rest1, capitals.back(), rest2); + + for (pos_t x = 0; x < h; x++) { + for (pos_t y = 0; y < w; y++) { + if (g.at(x, y).type == TileType::Capital) { + for (int i = 0; i < 4; i++) { + const int xx = x + dx[i], yy = y + dy[i]; + if (xx < 0 || xx >= h || yy < 0 || yy >= w) + continue; + if (g.at(xx, yy).type == TileType::Capital) + throw BadCapitalAssign(); + } + } + } + } + int T = 5; while (T--) { @@ -229,19 +246,22 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) { }; vector>> vec(27, vector>(101)); + vector> vertex_type(t.h + , vector(t.w, VertexType::Ordinary)); for (pos_t i = 0; i < t.h; i++) { for (pos_t j = 0; j < t.w; j++) { auto& p = t.tiles[i][j]; - if (view(' ')) + if (view(' ')) { p.type = TileType::Blank; - else if (view('m')) + } else if (view('m')) { p.type = TileType::Mountain; - else if (view('n')) { + vertex_type[i][j] = VertexType::Mountain; + } else if (view('n')) { p.type = TileType::Blank; p.unit = read(); - } else if (view('s')) + } else if (view('s')) { p.type = TileType::Swamp; - else if (view('g')) { + } else if (view('g')) { p.type = TileType::Blank; bool tag = 1; int team = (pt < in.value.size() && isupper(in.value[pt]) @@ -264,6 +284,8 @@ ImportedTerrain importTerrain(const ImportTerrainConfig& in) { if (pt != in.value.size()) error(); + if (!findCut(t.w, t.h, vertex_type)) + error(); t.capitals.emplace_back(); for (int i = 0; i <= 26; i++) {