Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1791 | Accepted: 444 |
Description
Input
Output
Sample Input
2 1 4 G 10 5 R 6 4 3 1 5 H 4 5 G 10 5 C 7 5 0 0 0
Sample Output
YES NO
Hint
Source
Regionals 2011 >> Asia - Fuzhou
题链接:POJ4001 HDU4121 UVA1589 UVALive5829 Xiangqi
问题简述:
模拟题,判断中国象棋棋盘上的局面是否被将军死。
问题分析:
按照题意进行模拟即可,需要程序的细节,尽可能把程序写简洁。
程序说明:
有个坑,如果开始就已经对将了,红方不赢,程序需要处理。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* POJ4001 HDU4121 UVA1589 UVALive5829 Xiangqi */
#include
#include
#include
using namespace std;
const int drow[] = {1, 1, -1, -1, /*R*/1, 0, -1, 0, /*H*/1, 1, -1, -1, 2, 2, -2, -2, 1, 1, -1, -1, 1, 1, -1, -1};
const int dcol[] = {1, -1, 1, -1, /*R*/0, 1, 0, -1, /*H*/2, -2, 2, -2, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1};
const int N = 10;
const int M = 9;
char board[N + 1][M + 1];
int grow, gcol;
bool flag;
// 循环判断:将(General),如果开始就对将了,则红方不赢
bool general( int row, int col, int start, int end )
{
for ( int i = start ; i <= end ; i++ ) {
int nextrow = row+drow[i];
int nextcol = col+dcol[i];
while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {
if(board[nextrow][nextcol] == 'G')
return true;
if(board[nextrow][nextcol] != ' ')
break;
nextrow += drow[i];
nextcol += dcol[i];
}
}
return false;
}
// 循环判断:车(Chariot, Rook),将(General)
bool chariot( int row, int col, int start, int end )
{
for ( int i = start ; i <= end ; i++ ) {
int nextrow = row+drow[i];
int nextcol = col+dcol[i];
while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {
if(board[nextrow][nextcol] == 'R' || board[nextrow][nextcol] == 'G')
return true;
if(board[nextrow][nextcol] != ' ')
break;
nextrow += drow[i];
nextcol += dcol[i];
}
}
return false;
}
// 循环判断:炮(Cannon)
bool cannon( int row, int col, int start, int end )
{
for ( int i = start ; i <= end ; i++ ) {
int nextrow = row+drow[i];
int nextcol = col+dcol[i];
// 找炮台
while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {
if(board[nextrow][nextcol] != ' ')
break;
nextrow += drow[i];
nextcol += dcol[i];
}
// 循环判断
nextrow += drow[i];
nextcol += dcol[i];
while(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M) {
if(board[nextrow][nextcol] == 'C')
return true;
if(board[nextrow][nextcol] != ' ')
break;
nextrow += drow[i];
nextcol += dcol[i];
}
}
return false;
}
// 马(Horse)
bool horse(int row, int col, int start, int end)
{
for(int i = start; i <= end; i++) {
int nextrow = row + drow[i];
int nextcol = col + dcol[i];
if(nextrow >= 1 && nextrow <= N && nextcol >= 1 && nextcol <= M)
if(board[nextrow][nextcol] == 'H' && board[row +drow[i + 8]][col + dcol[i + 8]] == ' ')
return true;
}
return false;
}
void deal()
{
for(int i = 4; i <= 7; i++) {
int nextrow = grow + drow[i];
int nextcol = gcol + dcol[i];
if(nextrow < 1 || nextrow > 3 || nextcol < 4 || nextcol > 6)
continue;
// 车(Chariot, Rook),将(General)
flag = chariot(nextrow, nextcol, 4, 7);
if(flag)
continue;
// 炮(Cannon)
flag = cannon(nextrow, nextcol, 4, 7);
if(flag)
continue;
// 马(Horse)
flag = horse(nextrow, nextcol, 8, 15);
if(flag)
continue;
flag = false;
return;
}
flag = true;
}
inline char mygetchar()
{
char ret;
while ((ret = getchar()) == ' ' || ret == '\t' || ret == '\n');
return ret;
}
int main()
{
int n;
while(scanf("%d%d%d", &n, &grow, &gcol) == 3 && (n || grow || gcol)) {
memset(board, ' ', sizeof(board));
char piece;
int row, col;
while(n--) {
piece = mygetchar();
scanf("%d%d", &row, &col);
board[row][col] = piece;
}
flag = false;
if(!general(grow, gcol, 4, 4)) {
flag = false;
deal();
}
printf("%s\n", flag ? "YES" : "NO");
}
return 0;
}