20240812

鸭棋

大模拟,实在不知道思维上有啥难的,就是难写!!!!!!!!!!!!!!!!!!!!!

#include <bits/stdc++.h>

using namespace std;

struct chess {
  string color, type;
} chessboard[10][9];

string nowcolor = "red";

bool legal(int a, int b) {
  if (a < 0 || a > 9 || b < 0 || b > 8) {
    return false;
  }
  return true;
}

vector<pair<int, int> > allmove(int a, int b) {
  vector<pair<int, int> > res;
  if (chessboard[a][b].type == "captain") {
    res.push_back({a + 1, b});
    res.push_back({a - 1, b});
    res.push_back({a, b + 1});
    res.push_back({a, b - 1});
  }
  else if (chessboard[a][b].type == "guard") {
    for (auto i : {1, -1}) {
      for (auto j : {1, -1}) {
        res.push_back({a + i, b + j});
      }
    }
  }
  else if (chessboard[a][b].type == "elephant") {
    for (auto i : {1, -1}) {
      for (auto j : {1, -1}) {
        if (legal(a + i, b + j) && chessboard[a + i][b + j].color == "NA") {
          res.push_back({a + 2 * i, b + 2 * j});
        }
      }
    }
  }
  else if (chessboard[a][b].type == "horse") {
    for (auto i : {1, -1}) {
      for (auto j : {1, -1}) {
        if (legal(a + i, b) && chessboard[a + i][b].color == "NA") {
          res.push_back({a + 2 * i, b + j});
        }
        if (legal(a, b + j) && chessboard[a][b + j].color == "NA") {
          res.push_back({a + i, b + 2 * j});
        }
      }
    }
  }
  else if (chessboard[a][b].type == "car") {
    for (auto d : {1, -1}) {
      for (int i = a + d; i >= 0 && i <= 9; i += d) {        
        res.push_back({i, b});
        if (chessboard[i][b].color != "NA") {
          break;
        }
      }
      for (int i = b + d; i >= 0 && i <= 8; i += d) {
        res.push_back({a, i});
        if (chessboard[a][i].color != "NA") {
          break;
        }
      }
    }
  }
  else if (chessboard[a][b].type == "duck") {
    for (auto i : {1, -1}) {
      for (auto j : {1, -1}) {
        if (legal(a + 2 * i, b + j) && legal(a + i, b) && chessboard[a + 2 * i][b + j].color == "NA" && chessboard[a + i][b].color == "NA") {
          res.push_back({a + 3 * i, b + 2 * j});
        }
        if (legal(a + i, b + 2 * j) && legal(a, b + j) && chessboard[a + i][b + 2 * j].color == "NA" && chessboard[a][b + j].color == "NA") {
          res.push_back({a + 2 * i, b + 3 * j});
        }
      }
    }
  }
  else if (chessboard[a][b].type == "soldier") {
    for (auto i : {1, 0, -1}) {
      for (int j : {1, 0, -1}) {
        if (i != 0 || j != 0) {
          res.push_back({a + i, b + j});
        }
      }
    }
  }
  vector<pair<int, int> > tmp;
  for (auto [x, y] : res) {
    if (legal(x, y)) {
      tmp.push_back({x, y});
    }
  }
  return tmp;
}

void buildChessboard() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 9; j++) {
      chessboard[i][j] = {"NA", "NA"};
    }
  }
  vector<string> c = {"car", "horse", "elephant", "guard", "captain"};
  for (int l = 0, r = 8; l <= r; l++, r--) {
    chessboard[0][l] = chessboard[0][r] = {"red", c[l]};
    chessboard[9][l] = chessboard[9][r] = {"blue", c[l]};
  }
  for (int i = 0; i < 9; i += 2) {
    chessboard[3][i] = {"red", "soldier"};
    chessboard[6][i] = {"blue", "soldier"};
  }
  chessboard[2][0] = chessboard[2][8] = {"red", "duck"};
  chessboard[7][0] = chessboard[7][8] = {"blue", "duck"};
}

string getname(int a, int b) {
  if (chessboard[a][b].color == "NA") {
    return "NA";
  }
  return chessboard[a][b].color + " " + chessboard[a][b].type;
}

void move(int a, int b, int c, int d) {
  if (chessboard[a][b].color != nowcolor) return ;
  if (chessboard[c][d].color == nowcolor) return ;
  chessboard[c][d] = chessboard[a][b];
  chessboard[a][b] = {"NA", "NA"};
}

bool checkmate() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 9; j++) {
      vector<pair<int, int> > pos = allmove(i, j);
      for (auto [x, y] : pos) {
        if (chessboard[x][y].type == "captain" && chessboard[x][y].color != chessboard[i][j].color) {
          return true;
        }
      }
    }
  }
  return false;
}

bool gameover() {
  int cnt = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 9; j++) {
      cnt += (chessboard[i][j].type == "captain");
    }
  }
  return cnt < 2;
}

bool valid(int a, int b, int c, int d) {
  if (gameover()) {
    return false;
  }
  if (chessboard[a][b].color != nowcolor) {
    return false;
  }
  if (chessboard[c][d].color == nowcolor) {
    return false;
  }
  vector<pair<int, int> > pos = allmove(a, b);
  for (auto [x, y] : pos) {
    if (x == c && y == d) {
      return true;
    }
  }
  return false;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  buildChessboard();
  int q;
  cin >> q;
  while (q--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (valid(a, b, c, d)) {
      cout << getname(a, b) << ";";
      cout << getname(c, d) << ";";
      move(a, b, c, d);
      cout << (checkmate() ? "yes" : "no") << ";";
      cout << (gameover() ? "yes" : "no") << "\n";
      if (nowcolor == "red") nowcolor = "blue";
      else nowcolor = "red";
    }
    else {
      cout << "Invalid command\n";
    }
  }
  return 0;
}
posted @ 2024-09-23 11:31  libohan0518  阅读(6)  评论(0编辑  收藏  举报