https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4464题目链接。
题目复制过来我自己都不忍心看了,效果太差。
就是给一个象棋残局,判断是否将死,黑方只剩老将,红方有将,车,马,炮类型的棋,除了将,其他不是一定有。
想法就是标记棋盘,看哪些地方红方可以威胁到,然后判断黑方还有没有能走的地方了。
这里用了两个标记数组,一个标记了棋子的位置,注意,先不放黑将,因为他在不在棋盘上对红方是没有影响的,然后1表示这个地方有人,0,表示没有。
接着根据每个棋子的特点对点进行标记,能威胁到就标记为1,但是这里要考虑到,他自己本身是无法保护自己的,比如一个车在黑将的面前,虽然威胁黑将,但是黑将只要把车吃了,就可以了,所以车所在位置的保护是由其他棋子提供的,因为每次更新标记值,这个棋子无法保护自己。其他的就没什么了。
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int leap[20][20], leap2[20][20];
struct G{
int x, y;
}g;
struct C{
int x, y;
}c[20];
struct R{
int x, y;
}r[20];
struct H{
int x, y;
}h[20];
int main()
{
int i, j, m, n, ans, t, blackgx, blackgy, k;
int countg, counth, countc, countr;
int x, y;
char a[10];
while (scanf("%d%d%d", &n, &blackgx, &blackgy) != EOF)
{
if (n == 0 && blackgx == 0 && blackgy == 0)break;
countg = countc = countr = counth = 0;
memset(leap, 0, sizeof(leap));
for (i = 1; i <= n; i++)
{
scanf("%s", a);
cin >> x >> y;
leap[x][y]=1;
if (a[0] == 'G')
{
g.x = x; g.y = y;
}
else if (a[0] == 'R')
{
r[countr].x = x;
r[countr].y = y;
countr++;
}
else if (a[0] == 'C')
{
c[countc].x = x;
c[countc].y = y;
countc++;
}
else if (a[0] == 'H')
{
h[counth].x = x;
h[counth].y = y;
counth++;
}
}
memset(leap2, 0, sizeof(leap2));
for (i = g.x - 1; i >= 1; i--)
{
leap2[i][g.y] = 1;
if (leap[i][g.y])break;
}
for (k = 0; k < countr; k++)
{
x = r[k].x; y = r[k].y;
for (i = x + 1; i <= 10; i++)
{
leap2[i][y] = 1;
if (leap[i][y])break;
}
for (i = x - 1; i >= 1; i--)
{
leap2[i][y] = 1;
if (leap[i][y])break;
}
for (j = y + 1; j <= 9; j++)
{
leap2[x][j] = 1;
if (leap[x][j])break;
}
for (j = y - 1; j >= 1; j--)
{
leap2[x][j] = 1;
if (leap[x][j])break;
}
}
for (k = 0; k < counth; k++)
{
x = h[k].x; y = h[k].y;
if (x + 2 <= 10)
{
if (!leap[x + 1][y])
{
if (y + 1 <= 9)
leap2[x + 2][y + 1] = 1;
if (y - 1 >= 1)
leap2[x + 2][y - 1] = 1;
}
}
if (x - 2 >= 1)
{
if (!leap[x - 1][y])
{
if (y + 1 <= 9)
leap2[x - 2][y + 1] = 1;
if (y - 1 >= 1)
leap2[x - 2][y - 1] = 1;
}
}
if (y + 2 <= 9)
{
if (!leap[x][y + 1])
{
if (x + 1 <= 10)
leap2[x + 1][y + 2] = 1;
if (x - 1 >= 1)
leap2[x - 1][y + 2] = 1;
}
}
if (y - 2 >= 1)
{
if (!leap[x][y - 1])
{
if (x + 1 <= 10)
leap2[x + 1][y - 2] = 1;
if (x - 1 >= 1)
leap2[x - 1][y - 2] = 1;
}
}
}
for (k = 0; k < countc; k++)
{
x = c[k].x; y = c[k].y;
int temp;
temp = 0;
for (i = x + 1; i <= 10; i++)
{
if (temp == 0)
{
if (leap[i][y])
temp = 1;
}
else if (temp == 1)
{
leap2[i][y] = 1;
if (leap[i][y])break;
}
}
temp = 0;
for (i = x - 1; i >= 1; i--)
{
if (temp == 0)
{
if (leap[i][y])
temp = 1;
}
else
{
leap2[i][y] = 1;
if (leap[i][y])break;
}
}
temp = 0;
for (j = y + 1; j <= 9; j++)
{
if (temp == 0)
{
if (leap[x][j])
temp = 1;
}
else
{
leap2[x][j] = 1;
if (leap[x][j])break;
}
}
temp = 0;
for (j = y - 1; j >= 1; j--)
{
if (temp == 0)
{
if (leap[x][j])
temp == 1;
}
else
{
leap2[x][j] = 1;
if (leap[x][j])break;
}
}
}
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
int flag = 0;
for (i = 0; i < 4; i++)
{
x = blackgx + dir[i][0];
y = blackgy + dir[i][1];
if (x >= 1 && x <= 3 && y >= 4 && y <= 6)
{
if (leap2[x][y] == 0)
flag = 1;
}
}
if (flag == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}