这道题花了很久总是WA,最后发现竟然是在一个判断时括号的位置不正确QAQ,真的想死的心都有了。
总体思想:记录所有下红色棋子的位置和种类,然后对于“将”能走的四个位置分别判断是否都有红色棋子能够到达。这里注意一个问题,“将”在移动一格后可能会吃掉一个红子,所以在每次对红色棋子去检查时需要判断它是否已经被吃掉了。
下面附上代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<string>
using namespace std;
int a[20][20];
int n, r, c;
typedef struct{
char t;
int r;
int c;
}chees;
chees chs[10];
int dirx[] = { 1, -1, 0, 0 }, diry[] = { 0, 0, 1, -1 };
bool find_c(int x, int y){//炮
if (x != r && y != c)
return false;
int i;
//up
i = x - 1;
while (i > 1 && !a[i][y])
i--;
if (i > 1){
i--;
while (i>0 && !a[i][y]){
if (i == r&& y == c)
return true;
i--;
}
}
//down
i = x + 1;
while (i < 10 && !a[i][y])
i++;
if (i < 10){
i++;
while (i<11 && !a[i][y]){
if (i == r&& y == c)
return true;
i++;
}
}
//left
i = y - 1;
while (i > 1 && !a[x][i])
i--;
if (i > 1){
i--;
while (i>0 && !a[x][i]){
if (x == r&& i == c)
return true;
i--;
}
}
//right
i = y + 1;
while (i < 9 && !a[x][i])
i++;
if (i < 9){
i++;
while (i<10 && !a[x][i]){
if (x == r&& i == c)
return true;
i++;
}
}
return false;
}
bool find_h(int x, int y){//马
if (x>2 && !a[x - 1][y]){
if (x - 2 == r && (y - 1 == c || y + 1 == c))
return true;
}
if (x<9 && !a[x + 1][y]){
if (x + 2 == r && (y - 1 == c || y + 1 == c))
return true;
}
if (y>2 && !a[x][y - 1]){
if (y - 2 == c && (x + 1 == r || x - 1 == r))
return true;
}
if (y<8 && !a[x][y + 1]){
if (y + 2 == c && (x + 1 == r || x - 1 == r))
return true;
}
return false;
}
bool find_r(int x, int y){//车
if (x != r && y != c)
return false;
for (int i = x - 1; i > 0; i--){//up
if (a[i][y])
break;
if (i == r && y == c)
return true;
}
for (int i = x + 1; i < 11; i++){//down
if (a[i][y])
break;
if (i == r && y == c)
return true;
}
for (int i = y - 1; y > 0; i--){//left
if (a[x][i])
break;
if (x == r && i == c)
return true;
}
for (int i = y + 1; i < 10; i++){//down
if (a[x][i])
break;
if (x == r && i == c)
return true;
}
return false;
}
bool find_g(int x, int y){//帅
if (y != c)
return false;
for (int i = x - 1; i > 0; --i){
if (a[i][y])
return false;
if (i == r && y == c)
return true;
}
return false;
}
bool solve(){
for (int i = 0; i < n; i++){
if (chs[i].r == r && chs[i].c == c)
continue;
if (chs[i].t == 'G' && find_g(chs[i].r, chs[i].c))
return false;
else if (chs[i].t == 'R' && find_r(chs[i].r, chs[i].c))
return false;
else if (chs[i].t == 'H' && find_h(chs[i].r, chs[i].c))
return false;
else if (chs[i].t == 'C' && find_c(chs[i].r, chs[i].c))
return false;
}
return true;
}
int main()
{
while (cin >> n >> r >> c && n){
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++){
cin >> chs[i].t >> chs[i].r >> chs[i].c;
a[chs[i].r][chs[i].c] = 1;
}
bool flag = false;
for (int i = 0; i < 4; i++){
int tr = r + dirx[i];
int tc = c + diry[i];
if (tr>3 || tr<1 || tc>6 || tc < 4)
continue;
r = tr;
c = tc;
int t = a[r][c];
a[r][c] = 0;
if (solve()){
flag = true;
break;
}
a[r][c] = t;
r -= dirx[i];
c -= diry[i];
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}