Untitled
unknown
plain_text
a year ago
5.2 kB
4
Indexable
#include <iostream>
#include <unordered_map>
#include <set>
#include <vector>
#include <string>
#define MAX_N 25
using namespace std;
struct Country {
string name;
int numSolider;
int group;
};
Country map[MAX_N][MAX_N];
unordered_map<string, int> mp;
set<int> lienminh[626];
set<int> groups[626];
set<int> enemy[626];
int cntGr;
int n;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
void init(int N, int mSoldier[25][25], char mMonarch[25][25][11]) {
n = N;
cntGr = 1;
mp.clear();
for (int i = 0; i < 626; i++) {
groups[i].clear();
enemy[i].clear();
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
string s = "";
for (char ch = 0; mMonarch[i][j][ch] != '\0'; ch++) {
s += mMonarch[i][j][ch];
}
mp[s] = i * N + j;
map[i][j].name = s;
map[i][j].group = 0;
map[i][j].numSolider = mSoldier[i][j];
}
}
}
void destroy() {
}
int ally(char mMonarchA[11], char mMonarchB[11]) {
string sA = "", sB = "";
for (int ch = 0; mMonarchA[ch] != '\0'; ch++) {
sA += mMonarchA[ch];
}
for (int ch = 0; mMonarchB[ch] != '\0'; ch++) {
sB += mMonarchB[ch];
}
int rA = mp[sA] / n;
int cA = mp[sA] % n;
int rB = mp[sB] / n;
int cB = mp[sB] % n;
int grA = map[rA][cA].group;
int grB = map[rB][cB].group;
if (grA == 0 && grB == 0) { // Cả 2 nước chưa có Group
map[rA][cA].group = cntGr;
map[rB][cB].group = cntGr;
groups[cntGr].insert(mp[sA]);
groups[cntGr].insert(mp[sB]);
cntGr++;
return 1;
} else if (grA > 0 && grB == 0) { // Nước A đã có group và nước B không có
int gr = map[rA][cA].group;
groups[gr].insert(mp[sB]);
map[rB][cB].group = gr;
return 1;
} else if (grB > 0 && grA == 0) { // Nước B đã có group và nước A không có
int gr = map[rB][cB].group;
groups[gr].insert(mp[sA]);
map[rA][cA].group = gr;
return 1;
} else if (grB == grA ) { // Cùng group
return -1;
}
else return -2;
}
int attack(char mMonarchA[11], char mMonarchB[11], char mGeneral[11]) {
string sA = "", sB = "";
for (int ch = 0; mMonarchA[ch] != '\0'; ch++) {
sA += mMonarchA[ch];
}
for (int ch = 0; mMonarchB[ch] != '\0'; ch++) {
sB += mMonarchB[ch];
}
int rA = mp[sA] / n;
int cA = mp[sA] % n;
int rB = mp[sB] / n;
int cB = mp[sB] % n;
// nếu 2 nước đã là liên minh
if (map[rA][cA].group == map[rB][cB].group) {
return -1;
}
int numAttack = 0; // lính của nước A
int numDef = map[rB][cB].numSolider; // lính của nước B
int res = -1;
bool canAttack = false;
vector<pair<int, int>> friendA; // đồng minh của A
vector<pair<int, int>> friendB; // đồng minh của B
for (int k = 0; k < 8; k++) {
int nx = rB + dx[k];
int ny = cB + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
if (map[rA][cA].group == map[nx][ny].group && map[nx][ny].group != 0) { // Đồng minh của A kề cạnh B
canAttack = true;
friendA.push_back(make_pair(nx, ny));
} else if (map[rB][cB].group == map[nx][ny].group && map[nx][ny].group != 0) { // Đồng minh của B kề cạnh B
//cout<<"Def dong minh: "<<nx<<" "<<ny<<endl;
friendB.push_back(make_pair(nx, ny));
}
}
}
if (canAttack) {
int grA = map[rA][cA].group;
int grB = map[rB][cB].group;
enemy[grA].insert(grB);
enemy[grB].insert(grA);
for (auto it : friendA) {
// Giảm một nửa quân đội nuoc tham gia tấn công
numAttack += map[it.first][it.second].numSolider / 2;
map[it.first][it.second].numSolider -= map[it.first][it.second].numSolider / 2;
}
for (auto it : friendB) {
// Giảm một nửa quân đội các nước tham gia phòng thủ
numDef += map[it.first][it.second].numSolider / 2;
map[it.first][it.second].numSolider -= map[it.first][it.second].numSolider / 2;
}
//cout<<"numDef: "<<numDef<<" numAttack: "<<numAttack<<endl;
if (numDef >= numAttack) { // Phòng thủ thành công
res = 0;
map[rB][cB].numSolider = numDef - numAttack;
} else { // Tấn công thành công
int newNumSolider = numAttack - numDef;
string sG = "";
for (char ch = 0; mGeneral[ch] != '\0'; ch++) {
sG += mGeneral[ch];
}
int groupA = map[rA][cA].group;
groups[groupA].insert(mp[sB]);
map[rB][cB].name = sG;
map[rB][cB].numSolider = newNumSolider;
map[rB][cB].group = groupA;
mp[sG] = mp[sB];
mp.erase(sB);
res = 1;
}
} else {
res = -2;
}
return res;
}
int recruit(char mMonarch[11], int mNum, int mOption) {
string s = "";
for (int ch = 0; mMonarch[ch] != '\0'; ch++) {
s += mMonarch[ch];
}
int r = mp[s] / n;
int c = mp[s] % n;
int res = 0;
if (mOption == 0) {
map[r][c].numSolider += mNum;
res = map[r][c].numSolider;
} else {
int gr = map[r][c].group;
for (auto pos : groups[gr]) {
int row = pos / n;
int col = pos % n;
//cout<<"Row, col: "<<row<<" "<<col<<endl;
map[row][col].numSolider += mNum;
res += map[row][col].numSolider;
}
}
return res;
}Editor is loading...
Leave a Comment