思路:把‘黑将’不能走的地方用'*'填掉,标記在map[][]上,map1[][]上存儲棋子的位置。下棋中一般不會出現的情況如
3 1 5
R 4 4
R 4 6
G 10 5
這時黑棋未被将死.這種情況應該列印‘NO’,但原題的測試樣例沒有管這種不合下棋常理的情況,是以能AC.
開始輸入用getchar(),老是有問題,改成cin>>...後就AC了。。。
//#define LOCAL
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<iostream>
using namespace std;
int N, bgx, bgy, gx, gy;
char map[15][15];
char map1[15][15];
struct C{
char ch;
int x, y;
}C1,C2;
struct H
{
char ch;
int x, y;
}H1,H2;
struct R
{
char ch;
int x, y;
}R1,R2;
//輸出中間結果調試用
void myprintf(char map[][15]){
for (int i = 1; i <= 10; i++){
for (int j =1; j <= 9; j++){
printf("%c ", map[i][j]);
}
printf("\n");
}
printf("\n");
}
//馬:先判斷馬是否為'hobbled horse',再判斷填'*'處是否越界
void Horse(H H3){
if (H3.ch!='0')
{
if (map1[H3.x + 1][H3.y] != '1'){
if (H3.y - 1>0) map[H3.x + 2][H3.y - 1] = '*';
map[H3.x + 2][H3.y + 1] = '*';
}
if (map1[H3.x - 1][H3.y] != '1'){
if (H3.x - 2>0 && H3.y - 1>0) map[H3.x - 2][H3.y - 1] = '*';
if (H3.x - 2>0) map[H3.x - 2][H3.y + 1] = '*';
}
if (map1[H3.x][H3.y - 1] != '1') {
if (H3.y - 2>0) map[H3.x + 1][H3.y - 2] = '*';
if (H3.x - 1>0 && H3.y - 2>0) map[H3.x - 1][H3.y - 2] = '*';
}
if (map1[H3.x][H3.y + 1] != '1') {
map[H3.x + 1][H3.y + 2] = '*';
if (H3.x - 1>0) map[H3.x - 1][H3.y + 2] = '*';
}
}
}
void CR(R R3){
if (R3.ch!='0'){
for (int i = R3.x - 1; i >0; i--){
map[i][R3.y] = '*';
if (map1[i][R3.y] == '1') break;
}
for (int i = R3.x + 1; i <4; i++){
map[i][R3.y] = '*';
if (map1[i][R3.y] == '1') break;
}
for (int i = R3.y - 1; i >3; i--){
map[R3.x][i] = '*';
if (map1[R3.x][i] == '1') break;
}
for (int i = R3.y + 1; i<7; i++){
map[R3.x][i] = '*';
if (map1[R3.x][i] == '1') break;
}
}
}
void Cannon(C C3){
if (C3.ch != '0'){
if (C3.x>bgx){
for (int i = C3.x-1;i>0; i--){
int d4=0;
if (map1[i][C3.y] == '1'){
i--;
for (i; i > 0; i--){
map[i][C3.y] = '*';
if (map1[i][C3.y] == '1'){
d4 = 1; break;
}
}
}
if (d4) break;
}
}
else if (C3.x < bgx){
if (map1[C3.x + 1][C3.y] == '1'&&map1[C3.x + 2][C3.y] != '1')
map[C3.x + 2][C3.y] = '*';
}
if (C3.y < bgy){
for (int i = C3.y+1; i<10; i++){
int d4 = 0;
if (map1[C3.x][i] == '1'){
i++;
for (i ; i <10; i++){
map[C3.x][i] = '*';
if (map1[C3.x][i] == '1'){
d4 = 1; break;
}
}
}
if (d4) break;
}
}
else if (C3.y > bgy){
for (int i = C3.y-1; i>0; i--){
int d4 = 0;
if (map1[C3.x][i] == '1'){
i--;
for (i ; i > 0; i--){
map[C3.x][i] = '*';
if (map1[C3.x][i] == '1'){
d4 = 1; break;
}
}
}
if (d4) break;
}
}
}
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while (scanf("%d%d%d", &N, &bgx, &bgy) == 3 && N ||bgx||bgy){
memset(map, '0', sizeof(map));
memset(map1, '0', sizeof(map1));
bool d2=1;
int first1,first2,first3;
first1 = first2 = first3 = 1;
C1.ch = C2.ch = H1.ch = H2.ch = R1.ch = R2.ch = '0';
while (N--){
char ch;
cin >> ch;
int x, y;
if (ch == 'G'){
scanf("%d%d", &gx, &gy);
map1[gx][gy] = '1';
}
else if (ch == 'R'){
scanf("%d%d", &x, &y);
map1[x][y] = '1';
if (first1) { first1 = 0; R1.ch = 'R', R1.x = x, R1.y = y; }
else{ R2.ch = 'R', R2.x = x,R2.y = y; }
}
else if (ch == 'H'){
scanf("%d%d", &x, &y);
map1[x][y] = '1';
if (first2) { first2 = 0; H1.ch = 'H', H1.x = x,H1.y = y; }
else{ H2.ch = 'H',H2.x = x,H2.y = y; }
}
else if (ch == 'C'){
scanf("%d%d", &x, &y);
map1[x][y] = '1';
if (first3) { first3 = 0; C1.ch = 'C', C1.x = x, C1.y = y; }
else{ C2.ch = 'C', C2.x = x, C2.y = y; }
}
}
//myprintf(map1);
int d1 = 0;
for (int i = gx-1; i >0; i--){
if (map1[i][gy] == '1'){
d1 = i; break;
}
}
if (d1 == bgx) d1 = 0;
if (!d1){
for (int i = 1; i < 4; i++){
map[i][gy] = '*'; //将與帥相對的情況
}
}
else {
for (int i = d1; i < 4; i++){
map[i][gy] = '*';
}
}
Horse(H1);
Horse(H2);
CR(R1);
CR(R2);
Cannon(C1);
Cannon(C2);
//myprintf(map);
//把将的palace用'*'圍起來,友善下面判斷
for (int i = 3; i <= 7; i++){
map[0][i] = '*';
map[4][i] = '*';
}
for (int i = 1; i <= 3; i++){
map[i][3] = '*';
map[i][7] = '*';
}
if (map[bgx][bgy-1] == '*'&&map[bgx][bgy+1] == '*'&&map[bgx-1][bgy] == '*'&&map[bgx+1][bgy] == '*')
d2 = 0;
//myprintf(map);
if (!d2) printf("YES\n");
else printf("NO\n");
}
//printf("Time used=%.3f\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}