标签:
2 1 4 G 10 5 R 6 4 3 1 5 H 4 5 G 10 5 C 7 5 0 0 0
YES NO 其实这样子的题做一做还是蛮有意思的,实现一个完整程序中的一小部分,对编程能力还是有锻炼的。我的思路是把红方棋子的攻击范围标出来,当然不能让将帅直接面对,注意下细节就好了。不过作为一道细节题我竟然一次直接过了,难道我的强项真的是考细节的水题吗??好吧,我水题是做得好。#include<iostream> #include<cstring> #include<string> using namespace std; int map[13][13]; //初始化为0,1表示有棋子 int sign[13][13]; //初始化为0,1表示在红方棋子攻击范围内 struct node { char c; int x, y; }; int main() { int n, bx, by; while (cin >> n >> bx >> by&&n) { memset(map, 0, sizeof(map)); //初始化 memset(sign, 0, sizeof(sign)); node qizi[10]; int rjx, rjy; for (int i = 0; i < n; i++) { cin >> qizi[i].c >> qizi[i].x >> qizi[i].y; if (qizi[i].c == 'G') { rjx = qizi[i].x; rjy = qizi[i].y; } map[qizi[i].x][qizi[i].y] = 1; } if (rjy == by) //对特殊情况,黑将和红帅如果直接相对,直接是NO { int ok = 0; for (int i = bx + 1; i < rjx; i++) if (map[i][rjy]) ok = 1; if (!ok) { cout << "NO" << endl; continue; } } for (int i = 0; i < n; i++) //处理出红方棋子的攻击范围,棋子本身不在其内(可以被吃) { if (qizi[i].c == 'G') //处理出将的攻击范围 { for (int i = rjx - 1; i >= bx; i--) { sign[i][rjy] = 1; if (map[i][rjy]) break; } } else if (qizi[i].c == 'R') //处理出车的攻击范围 { int xx = qizi[i].x; int yy = qizi[i].y; for (int i = xx + 1; i <= 10; i++) { sign[i][yy] = 1; if (map[i][yy]) break; } for (int i = xx - 1; i >= 1; i--) { sign[i][yy] = 1; if (map[i][yy]) break; } for (int i = yy + 1; i <= 9; i++) { sign[xx][i] = 1; if (map[xx][i]) break; } for (int i = yy - 1; i >= 1; i--) { sign[xx][i] = 1; if (map[xx][i]) break; } } if (qizi[i].c == 'C') //处理出炮的攻击范围 { int xx = qizi[i].x; int yy = qizi[i].y; int ok; ok = 0; for (int i = xx + 1; i <= 10; i++) { if (ok == 1) sign[i][yy] = 1; if (map[i][yy]) ok++; } ok = 0; for (int i = xx - 1; i >= 1; i--) { if (ok == 1) sign[i][yy] = 1; if (map[i][yy]) ok++; } ok = 0; for (int i = yy + 1; i <= 9; i++) { if (ok == 1) sign[xx][i] = 1; if (map[xx][i]) ok++; } ok = 0; for (int i = yy - 1; i >= 1; i--) { if (ok == 1) sign[xx][i] = 1; if (map[xx][i]) ok++; } } if (qizi[i].c == 'H') //处理出马的攻击范围 { int xx = qizi[i].x; int yy = qizi[i].y; if (!map[xx - 1][yy]) { if (xx - 2 >= 1 && yy - 1 >= 1) sign[xx - 2][yy - 1] = 1; if (xx - 2 >= 1 && yy + 1 <= 9) sign[xx - 2][yy + 1] = 1; } if (!map[xx + 1][yy]) { if (xx + 2 <= 10 && yy - 1 >= 1) sign[xx + 2][yy - 1] = 1; if (xx + 2 <= 10 && yy + 1 <= 9) sign[xx + 2][yy + 1] = 1; } if (!map[xx][yy - 1]) { if (yy - 2 >= 1 && xx - 1 >= 1) sign[xx - 1][yy - 2] = 1; if (yy - 2 >= 1 && xx + 1 <= 10) sign[xx + 1][yy - 2] = 1; } if (!map[xx][yy + 1]) { if (yy + 2 <= 9 && xx - 1 >= 1) sign[xx - 1][yy + 2] = 1; if (yy + 2 <= 9 && xx + 1 <= 10) sign[xx + 1][yy + 2] = 1; } } } /*for (int i = 1; i <= 10; i++) //测试 { for (int j = 1; j <= 9; j++) cout << sign[i][j] << " "; cout << endl; }*/ int cnt = 0; //判断黑将四个方向是否可以走 if (bx - 1 >= 1) { if (sign[bx - 1][by]) cnt++; } else cnt++; if (bx + 1 <= 3) { if (sign[bx + 1][by]) cnt++; } else cnt++; if (by - 1 >= 4) { if (sign[bx][by - 1]) cnt++; } else cnt++; if (by + 1 <= 6) { if (sign[bx][by + 1]) cnt++; } else cnt++; //cout << cnt << endl; if (cnt == 4) cout << "YES" << endl; else cout << "NO" << endl; } }
标签:
原文地址:http://blog.csdn.net/qq_18738333/article/details/44869823