include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
include [HTML_REMOVED]
using namespace std;
string Ans;
// vector[HTML_REMOVED]> Res;
struct Data {
int field1;
int field2;
int field3;
int field4;
int field5;
};
std::vector[HTML_REMOVED] Res; // 更新为使用 Data 结构体
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
std::vector[HTML_REMOVED] a;
std::vector[HTML_REMOVED]> r_1; // 红色有染料
std::vector[HTML_REMOVED]> r_2; // 红色无染料
std::vector[HTML_REMOVED]> g_1; // 绿色有染料
std::vector[HTML_REMOVED]> g_2; // 绿色无染料
// 墙,燃料池,中立棋子不用更新
std::vector[HTML_REMOVED]> zl; //中立棋子
std::set[HTML_REMOVED]> qiang; // 墙
std::vector[HTML_REMOVED]> z_1; // 红色染料区
std::vector[HTML_REMOVED]> z_2; // 绿色染料区
// vector[HTML_REMOVED]> map(50, vector(50));
string s = “MAP:2,0,0,0,0,0,0,0,0,4;1,0,0,0,0,0,0,0,0,3;0,0,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,0,0;0,0,20,0,0,0,0,10,0,0;0,0,6,0,0,0,0,6,0,0;0,0,6,0,0,0,0,6,0,0;0,0,6,0,0,0,0,6,0,0;0,0,10,0,0,0,0,20,0,0;0,0,0,0,0,0,0,0,0,0;”;
std::vector[HTML_REMOVED]> Map(50, std::vector[HTML_REMOVED](50, 0));
std::vector[HTML_REMOVED]> Tmp_Map(50, std::vector[HTML_REMOVED](50, 0));
std::vector[HTML_REMOVED]> Ture_Map(50, std::vector[HTML_REMOVED](50, 0));
int len;
// 获取随机数
int get_random() {
std::random_device rd; // 获取随机数种子
std::mt19937 gen(rd()); // 使用梅森旋转算法生成随机数
std::uniform_int_distribution<> distr(0, 3); // 定义范围为0到3
return distr(gen);
}
// 检查位置是否越界
bool check(int x, int y) {
if (x < 0 || x >= len || y < 0 || y >= len) return false;
return true;
}
// 视野处理函数
void look_func(int i, int j) {
if (Map[i][j] == 5)
{
zl.push_back(make_pair(i, j));
}
if (Map[i][j] == 6)
{
qiang.insert(make_pair(i, j));
}
if (Map[i][j] == 10)
{
z_1.push_back(make_pair(i, j));
}
if (Map[i][j] == 20)
{
z_2.push_back(make_pair(i, j));
}
if (Map[i][j] == 11) {
// 有染料红子在红染料区
if (std::find(z_1.begin(), z_1.end(), std::make_pair(i, j)) == z_1.end()) {
z_1.push_back(std::make_pair(i, j));
}
if (std::find(r_1.begin(), r_1.end(), make_pair(i, j)) == r_1.end()) {
r_1.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 21) {
// 有染料红子在绿染料区
if (std::find(z_2.begin(), z_2.end(), make_pair(i, j)) == z_2.end()) {
z_2.push_back(make_pair(i, j));
}
if (std::find(r_1.begin(), r_1.end(), make_pair(i, j)) == r_1.end()) {
r_1.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 12) {
// 无染料红子在红染料区
if (std::find(z_1.begin(), z_1.end(), make_pair(i, j)) == z_1.end()) {
z_1.push_back(make_pair(i, j));
}
if (std::find(r_2.begin(), r_2.end(), make_pair(i, j)) == r_2.end()) {
r_2.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 22) {
// 无染料红子在绿染料区
if (std::find(z_2.begin(), z_2.end(), make_pair(i, j)) == z_2.end()) {
z_2.push_back(make_pair(i, j));
}
if (std::find(r_2.begin(), r_2.end(), make_pair(i, j)) == r_2.end()) {
r_2.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 13) {
// 有染料绿子在红染料区
if (std::find(z_1.begin(), z_1.end(), make_pair(i, j)) == z_1.end()) {
z_1.push_back(make_pair(i, j));
}
if (std::find(g_1.begin(), g_1.end(), make_pair(i, j)) == g_1.end()) {
g_1.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 23) {
// 有染料绿子在绿染料区
if (std::find(z_2.begin(), z_2.end(), make_pair(i, j)) == z_2.end()) {
z_2.push_back(make_pair(i, j));
}
if (std::find(g_1.begin(), g_1.end(), make_pair(i, j)) == g_1.end()) {
g_1.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 14) {
// 无染料绿子在红染料区
if (std::find(z_1.begin(), z_1.end(), make_pair(i, j)) == z_1.end()) {
z_1.push_back(make_pair(i, j));
}
if (std::find(g_2.begin(), g_2.end(), make_pair(i, j)) == g_2.end()) {
g_2.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 24) {
// 无染料绿子在绿染料区
if (std::find(z_2.begin(), z_2.end(), make_pair(i, j)) == z_2.end()) {
z_2.push_back(make_pair(i, j));
}
if (std::find(g_2.begin(), g_2.end(), make_pair(i, j)) == g_2.end()) {
g_2.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 15) {
// 有染料红子在中立区
if (std::find(zl.begin(), zl.end(), make_pair(i, j)) == zl.end()) {
zl.push_back(make_pair(i, j));
}
if (std::find(r_1.begin(), r_1.end(), make_pair(i, j)) == r_1.end()) {
r_1.push_back(make_pair(i, j));
}
}
if (Map[i][j] == 25) {
// 有染料绿子在中立区
if (std::find(zl.begin(), zl.end(), make_pair(i, j)) == zl.end()) {
zl.push_back(make_pair(i, j));
}
if (std::find(g_1.begin(), g_1.end(), make_pair(i, j)) == g_1.end()) {
g_1.push_back(make_pair(i, j));
}
}
}
// 获取地图长度
void get_len(std::string input)
{
std::string map_prefix = “MAP:”;
// 找到 MAP: 的位置
size_t start_pos = input.find(map_prefix);
// 提取地图部分
start_pos += map_prefix.length();
int slen = 1;
int px = 0;
while (input[start_pos + slen - 1] != ';')
{
if (input[start_pos + slen - 1] == ',')
px++;
slen++;
}
len = px + 1;
}
// 读取服务器字符串地图
void read_map(std::string input)
{
std::string map_prefix = "MAP:";
// 找到 MAP: 的位置
size_t start_pos = input.find(map_prefix);
// 提取地图部分
start_pos += map_prefix.length();
int pos = start_pos;
for (int i = 0; i < len; i++)
{
int tmp_val = 0;
for (int j = 0; j < len; j++)
{
if (input[pos] != ',' && input[pos] != ';')
tmp_val = input[pos] - '0';
if (input[pos + 1] != ',' && input[pos + 1] != ';')
tmp_val = tmp_val * 10 + input[pos + 1] - '0';
Map[i][j] = tmp_val;
if (tmp_val < 10)
pos += 2;
else
pos += 3;
}
}
}
// 读取服务器上棋子的位置
void read_chess(vector[HTML_REMOVED]> map)
{
r_1.clear();
r_2.clear();
g_1.clear();
g_2.clear();
for (int i = 0; i < len; i)
{
for (int j = 0; j < len; j)
{
if (Map[i][j] == 1)
{
r_1.push_back(make_pair(i, j));
}
if (Map[i][j] == 2)
{
r_2.push_back(make_pair(i, j));
}
if (Map[i][j] == 3)
{
g_1.push_back(make_pair(i, j));
}
if (Map[i][j] == 4)
{
g_2.push_back(make_pair(i, j));
}
}
}
}
// 55 视野
void look(std::vector[HTML_REMOVED]> my_1) {
// 以x,y为中心, 55 的区域, 有24个点
for (auto [x, y] : my_1) {
// 检查上下左右
if (check(x + 1, y)) {
if (Map[x + 1][y] != 0) {
look_func(x + 1, y);
}
}
if (check(x - 1, y)) {
if (Map[x - 1][y] != 0) {
look_func(x - 1, y);
}
}
if (check(x, y + 1)) {
if (Map[x][y + 1] != 0) {
look_func(x, y + 1);
}
}
if (check(x, y - 1)) {
if (Map[x][y - 1] != 0) {
look_func(x, y - 1);
}
}
// 检查四个角落
if (check(x + 1, y + 1)) {
if (Map[x + 1][y + 1] != 0) {
look_func(x + 1, y + 1);
}
}
if (check(x - 1, y - 1)) {
if (Map[x - 1][y - 1] != 0) {
look_func(x - 1, y - 1);
}
}
if (check(x + 1, y - 1)) {
if (Map[x + 1][y - 1] != 0) {
look_func(x + 1, y - 1);
}
}
if (check(x - 1, y + 1)) {
if (Map[x - 1][y + 1] != 0) {
look_func(x - 1, y + 1);
}
}
// 检查轴
if (check(x + 2, y)) {
if (Map[x + 2][y] != 0) {
look_func(x + 2, y);
}
}
if (check(x - 2, y)) {
if (Map[x - 2][y] != 0) {
look_func(x - 2, y);
}
}
if (check(x, y + 2)) {
if (Map[x][y + 2] != 0) {
look_func(x, y + 2);
}
}
if (check(x, y - 2)) {
if (Map[x][y - 2] != 0) {
look_func(x, y - 2);
}
}
// 检查对角线
if (check(x + 2, y + 2)) {
if (Map[x + 2][y + 2] != 0) {
look_func(x + 2, y + 2);
}
}
if (check(x - 2, y - 2)) {
if (Map[x - 2][y - 2] != 0) {
look_func(x - 2, y - 2);
}
}
if (check(x + 2, y - 2)) {
if (Map[x + 2][y - 2] != 0) {
look_func(x + 2, y - 2);
}
}
if (check(x - 2, y + 2)) {
if (Map[x - 2][y + 2] != 0) {
look_func(x - 2, y + 2);
}
}
// 检查区域
if (check(x + 2, y - 1)) {
if (Map[x + 2][y - 1] != 0) {
look_func(x + 2, y - 1);
}
}
if (check(x - 2, y + 1)) {
if (Map[x - 2][y + 1] != 0) {
look_func(x - 2, y + 1);
}
}
if (check(x + 1, y - 2)) {
if (Map[x + 1][y - 2] != 0) {
look_func(x + 1, y - 2);
}
}
if (check(x + 1, y + 2)) {
if (Map[x + 1][y + 2] != 0) {
look_func(x + 1, y + 2);
}
}
if (check(x - 1, y - 2)) {
if (Map[x - 1][y - 2] != 0) {
look_func(x - 1, y - 2);
}
}
if (check(x - 1, y + 2)) {
if (Map[x - 1][y + 2] != 0) {
look_func(x - 1, y + 2);
}
}
}
}
// 行为处理函数(只会选择一个行为)
bool action_func(int color, int work, int x, int y, int tx, int ty) {
// work为是否有染料,1为有染料,2为没有染料
// Map中 红子-有染料(1),红子-无染料(2),绿子-有染料(3),绿子-无染料(4),中立(5),红色染料池(10),绿色染料池(20),右侧一列为墙(6)
// color 为 1就说明是红方,2就说明是绿方
if (work == 1) {
// 先攻击有染料的
if(Map[tx][ty] == 3 && color == 1) {
// std::tuple[HTML_REMOVED] myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
if(Map[tx][ty] == 1 && color == 2) {
// std::tuple<int, int, int, int, int> myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
// 再攻击无染料的
if(Map[tx][ty] == 4 && color == 1) {
// std::tuple<int, int, int, int, int> myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
if(Map[tx][ty] == 2 && color == 2) {
// std::tuple<int, int, int, int, int> myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
// 再攻击中立的
if(Map[tx][ty] == 5 && color == 2) {
// std::tuple<int, int, int, int, int> myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
if(Map[tx][ty] == 5 && color == 2) {
// std::tuple<int, int, int, int, int> myTuple(3, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {3, x, y, tx, ty};
Res.push_back(myData);
return true;
}
return false;
}
if (work == 2) {
if (Map[tx][ty] == 10 && color == 1) {
// 红子有染料,红方
// 移动
// std::tuple<int, int, int, int, int> myTuple(2, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {2, x, y, tx, ty};
Res.push_back(myData);
return true;
}
if (Map[tx][ty] == 20 && color == 2) {
// 绿子有染料,绿方
// 移动
// std::tuple<int, int, int, int, int> myTuple(2, x, y, tx, ty);
// Res.push_back(myTuple);
Data myData = {2, x, y, tx, ty};
Res.push_back(myData);
return true;
}
return false;
}
return false;
}
// 33 行为
void action(int color, int work, int x, int y) {
// 以x,y为中心, 33 的区域, 有8个点
// 检查上下左右
bool flag = false;
if (!flag && check(x + 1, y)) {
if (Map[x + 1][y] != 0) {
if(action_func(color, work, x, y, x + 1, y)) flag = true;
}
}
if (!flag && check(x - 1, y)) {
if (Map[x - 1][y] != 0) {
if(action_func(color, work, x, y, x - 1, y)) flag = true;
}
}
if (!flag && check(x, y + 1)) {
if (Map[x][y + 1] != 0) {
if(action_func(color, work, x, y, x, y + 1)) flag = true;
}
}
if (!flag && check(x, y - 1)) {
if (Map[x][y - 1] != 0) {
if(action_func(color, work, x, y, x, y - 1)) flag = true;
}
}
// 检查四个角落
if (!flag && check(x + 1, y + 1)) {
if (Map[x + 1][y + 1] != 0) {
if(action_func(color, work, x, y, x + 1, y + 1)) flag = true;
}
}
if (!flag && check(x - 1, y - 1)) {
if (Map[x - 1][y - 1] != 0) {
if(action_func(color, work, x, y, x - 1, y - 1)) flag = true;
}
}
if (!flag && check(x + 1, y - 1)) {
if (Map[x + 1][y - 1] != 0) {
if(action_func(color, work, x, y, x + 1, y - 1)) flag = true;
}
}
if (!flag && check(x - 1, y + 1)) {
if (Map[x - 1][y + 1] != 0) {
if(action_func(color, work, x, y, x - 1, y + 1)) flag = true;
}
}
if (flag == false) {
int idx = get_random();
int nx = x + dx[idx], ny = y + dy[idx];
while (check(nx, ny) == false || Map[nx][ny] == 6) idx = get_random(), nx = x + dx[idx], ny = y + dy[idx];
// std::tuple<int, int, int, int, int> myTuple(1, x, y, nx, ny);
// Res.push_back(myTuple);
Data myData = {1, x, y, nx, ny};
Res.push_back(myData);
}
}
// 更新地图
void restart_map(int flag) {
std::vector[HTML_REMOVED]>my_1;
std::vector[HTML_REMOVED]>my_2;
// 为红方
if (flag == 1) {
my_1 = r_1;
my_2 = r_2;
}
// 为绿方
else {
my_1 = g_1;
my_2 = g_2;
}
// look(my_1);
// look(my_2);
for (auto [x, y] : my_1) {
action(flag, 1, x, y);
}
for (auto [x, y] : my_2) {
action(flag, 2, x, y);
}
}
// void anstoString(vector[HTML_REMOVED]>& Res) {
// for (const auto& t : Res) {
// std::ostringstream oss; // 创建字符串流
// std::string tmp;
// // 根据第一个值选择前缀
// if (std::get<0>(t) == 1) tmp = “m,”;
// else if (std::get<0>(t) == 2) tmp = “g,”;
// else if (std::get<0>(t) == 3) tmp = “a,”;
// else continue; // 如果不是1, 2, 3,跳过
// // 拼接其他值
// oss << tmp
// << std::get<1>(t) << “,”
// << std::get<2>(t) << “,”
// << std::get<3>(t) << “,”
// << std::get<4>(t) << “;”;
// // 输出结果
// std::cout << oss.str() << std::endl; // 打印字符串
// Ans += oss.str();
// }
// }
void anstoString(const std::vector[HTML_REMOVED]& Res) {
for (const auto& item : Res) {
std::string tmp;
std::ostringstream oss;
// 根据 field1 选择前缀
if (item.field1 == 1) tmp = “m,”;
else if (item.field1 == 2) tmp = “g,”;
else if (item.field1 == 3) tmp = “a,”;
else continue; // 如果不是1, 2, 3,跳过
// 拼接字符串并输出
oss << tmp
<< item.field2 << ","
<< item.field3 << ","
<< item.field4 << ","
<< item.field5 << ";";
// // 输出结果
std::cout << oss.str() << std::endl; // 打印字符串
Ans += oss.str();
}
}
int main()
{
get_len(s);
cout << len << endl;
read_map(s);
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
cout << Map[i][j] << " ";
}
cout << endl;
}
read_chess(Map);
restart_map(1);
anstoString(Res);
cout << Ans <<endl;
// for (const auto& t : Res) {
// std::cout << "Tuple: ("
// << std::get<0>(t) << ", "
// << std::get<1>(t) << ", "
// << std::get<2>(t) << ", "
// << std::get<3>(t) << ", "
// << std::get<4>(t) << ")\n";
// }
return 0;
}