main.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #define X 10
  5. #define Y 9
  6. using std::cin;
  7. int dx[4] = {1, 0, -1, 0};
  8. int dy[4] = {0, 1, 0, -1};
  9. int hx[8] = {2, 2, 1, -1, -2, -2, 1, -1};
  10. int hy[8] = {1, -1, 2, 2, 1, -1, -2, -2};
  11. char board[X + 1][Y + 1];
  12. bool is_out(int x, int y) {
  13. return x < 1 || X < x || y < 1 || Y < y;
  14. }
  15. bool solve(int x, int y) {
  16. // Out of black palace
  17. if (x < 1 || 3 < x || y < 4 || 6 < y) return true;
  18. // Check cannon and chariot
  19. for (int i = 0; i < 4; i++) {
  20. int nx = x + dx[i];
  21. int ny = y + dy[i];
  22. char c = 0;
  23. int gap = 0;
  24. while (gap < 2 && !is_out(nx, ny)) {
  25. c = board[nx][ny];
  26. nx += dx[i];
  27. ny += dy[i];
  28. if (c == 0) continue;
  29. switch (c) {
  30. case 'G': case 'R':
  31. if (gap == 0) return true;
  32. break;
  33. case 'C':
  34. if (gap == 1) return true;
  35. break;
  36. }
  37. gap++;
  38. }
  39. }
  40. for (int i = 0; i < 8; i++) {
  41. int nx = x + hx[i];
  42. int ny = y + hy[i];
  43. if (is_out(nx, ny)) continue;
  44. if (board[nx][ny] == 'H') {
  45. nx -= hx[i] / 2; // Red to black, not black to red!
  46. ny -= hy[i] / 2;
  47. if (board[nx][ny] == 0) return true;
  48. }
  49. }
  50. return false;
  51. }
  52. void print_board() {
  53. printf(" 123456789\n");
  54. for (int x = 1; x <= X; x++) {
  55. printf("%2d", x);
  56. for (int y = 1; y <= Y; y++) {
  57. if (board[x][y] == 0) {
  58. printf(".");
  59. } else {
  60. printf("%c", board[x][y]);
  61. }
  62. }
  63. printf("\n");
  64. }
  65. }
  66. int main() {
  67. int N, gx, gy;
  68. for (;;) {
  69. cin >> N >> gx >> gy;
  70. if (N == 0 && gx == 0 && gy == 0) break;
  71. memset(board, 0, sizeof(board));
  72. char ch;
  73. int x, y;
  74. for (int n = 0; n < N; n++) {
  75. cin >> ch >> x >> y;
  76. board[x][y] = ch;
  77. }
  78. bool checkmate = true;
  79. for (int nx = gx + 1, ny = gy; !is_out(nx, ny); nx++) {
  80. char c = board[nx][ny]; // For "flying general"
  81. if (c == 0) continue;
  82. if (c == 'G') checkmate = false;
  83. break;
  84. }
  85. for (int i = 0; i < 4; i++) {
  86. int nx = gx + dx[i];
  87. int ny = gy + dy[i];
  88. checkmate &= solve(nx, ny);
  89. }
  90. printf(checkmate ? "YES\n" : "NO\n");
  91. }
  92. }