【UVA】 UVA 1589

来源:互联网 发布:顾比均线指标源码 编辑:程序博客网 时间:2024/06/16 13:15

Xiangqi is one of the most popular two-player board games in China. The game represents a battle between two armies with the goal of capturing the enemy’s “general” piece. In this problem, you are given a situation of later stage in the game. Besides, the red side has already “delivered a check”. Your work is to check whether the situation is “checkmate”. Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10 × 9 board and the pieces are placed on the intersections (points). The top left point is (1,1) and the bottom right point is (10,9). There are two groups of pieces marked by black or red Chinese characters, belonging to the two players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No two pieces can occupy the same point at the same time. A piece can be moved onto a point occupied by an enemy piece, in which case the enemy piece is“captured” and removed from the board. When the general is in danger of being captured by the enemy player on the enemy player’s next move, the enemy player is said to have “delivered a check”. If the general’s player can make no move to prevent the general’s capture by next enemy move, the situation is called “checkmate”.
这里写图片描述

We only use 4 kinds of pieces introducing as follows:
这里写图片描述General: the generals can move and capture one point either vertically or horizontally and cannot leave the “palace” unless the situation called “flying general” (see the figure above). “Flying general” means that one general can “fly” across the board to capture the enemy general if they stand on the same line without intervening pieces.
这里写图片描述Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not jump over intervening pieces
这里写图片描述Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping exactly one piece (whether it is friendly or enemy) over to its target.
这里写图片描述Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if there is any pieces lying on a point away from the horse horizontally or vertically it cannot move or capture in that direction (see the figure below), which is called “hobbling the horse’s leg”.
这里写图片描述

Now you are given a situation only containing a black general, a red general and several red chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s move. Your job is to determine that whether this situation is “checkmate”.
Input
The input contains no more than 40 test cases. For each test case, the first line contains three integers representing the number of red pieces N (2 ≤ N ≤ 7) and the position of the black general. The following N lines contain details of N red pieces. For each line, there are a char and two integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the red side has delivered the check. There is a blank line between two test cases. The input ends by ‘0 0 0’.
Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.
Hint: In the first situation, the black general is checked by chariot and “flying general”. In the second situation, the black general can move to (1, 4) or (1, 6) to stop check. See the figure on the right.
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
这里写图片描述
真的是痛苦万分,觉得自己写的都对,没想到debug的时候 一次一次的找到自己的错误,用各种数据hack自己,还是考虑的不够全面。代码我这次尽量写的清晰,分了函数出来写,但还是有些冗长,不连贯,不过较之前写的略有进步。
下面是我的一些错误:
1.将能把他旁边的棋子吃掉来获取生存。。一开始并没有这么判断,在UVA上的debug就出错了。
2.我最开始是没有加入黑子的将,只是单纯地用数字来记录它的坐标。。后来导致了将可以走一步来吃子,别马腿的情况都实现不了啦!后来加入了一个S(其实应该是将(J),不过就这样吧。。)
3.函数写扯了,最后阶段就是感觉情况都考虑了,却过不了,就自己找一些数据去hack自己的函数。。发现了炮,车函数都有漏洞。。改了又改,勉强过去。
这里写图片描述
下面附代码:

#include <iostream>using namespace std;char mp[15][15];int n,bx,by,rx,ry;char ch;int x,y;bool House(int x,int y) //马的判断{    if((mp[x+1][y-2]=='H' && mp[x+1][y-1]=='.') || (mp[x+1][y+2]=='H' && mp[x+1][y+1]=='.'))        return true;    if((mp[x+2][y+1]=='H' && mp[x+1][y+1]=='.') || (mp[x+2][y-1]=='H' && mp[x+1][y-1]=='.'))        return true;    if(x!=1)    {        if((mp[x-1][y-2]=='H' && mp[x-1][y-1]=='.') || (mp[x-1][y+2]=='H' && mp[x-1][y+1]=='.'))            return true;        if(x==3)        {            if((mp[x-2][y-1]=='H' && mp[x-1][y-1]=='.') || (mp[x-2][y+1]=='H' && mp[x-1][y+1]=='.'))                return true;        }    }    return false;}bool Chariot(int x,int y)//车的判断{    for(int i=x+1;i<=10;i++)    {        if(mp[i][y]!='.' && mp[i][y]=='R')            return true;        else if(mp[i][y]!='.') break;    }    for(int i=x-1;i>=1;i--)    {        if(mp[i][y]!='.' && mp[i][y]=='R')            return true;        else if(mp[i][y]!='.') break;    }    for(int i=y+1;i<=9;i++)    {        if(mp[x][i]!='.' && mp[x][i]=='R')            return true;        else if(mp[x][i]!='.') break;    }    for(int i=y-1;i>=1;i--)    {        if(mp[x][i]!='.' && mp[x][i]=='R')            return true;        else if(mp[x][i]!='.') break;    }    return false;}bool Cannon(int x,int y)//炮的判断{    for(int i=1;i<=10;i++)    {        if(mp[i][y]=='C')        {            int num = 0;            if(i>x)            {                for(int j=i-1;j>x;j--)                {                    if(mp[j][y]!='.')                        num++;                }            }            else            {                for(int j=i+1;j<x;j++)                {                    if(mp[j][y]!='.')                        num++;                }            }            if(num==1) return true;        }    }    for(int i=1;i<=9;i++)    {        if(mp[x][i]=='C')        {            int num = 0;            if(i>y)            {                for(int j=i-1;j>y;j--)                {                    if(mp[x][j]!='.')                        num++;                }            }            else            {                for(int j=i+1;j<y;j++)                {                    if(mp[x][j]!='.')                        num++;                }            }            if(num==1) return true;        }    }    return false;}bool General(int x,int y)//飞将判断{    for(int i=x+1;i<=10;i++)    {        if(mp[i][y]=='G')            return true;        if(mp[i][y]!='.') return false;    }    return false;}int main(){    ios::sync_with_stdio(false);    while(cin>>n>>bx>>by)    {        bool flag = 0;        if(n==0&&bx==0&&by==0)break;        for(int i=1;i<=10;i++)        {            for(int j=1;j<=10;j++)            {                mp[i][j]='.';            }        }        while(n--)        {            cin>>ch>>x>>y;            mp[x][y]=ch;            if(ch=='G')            {                rx = x;                ry = y;            }        }        mp[bx][by] = 'S';        if(General(bx,by)) //判的开始会不会被黑子飞将,但题目上好像说明保证        {                 //红子已经将军,不判也能过,所有这一步不要也行。            flag=1;       //上面的两次AC就是一次写了这个判断,一个没写        }        //本来有四个方向,如果在棋盘边,方向就受限。        if(bx!=1)        {            int cx = bx-1;            int cy = by;            char t = mp[cx][cy];            mp[cx][cy] = mp[bx][by];            mp[bx][by] = '.';            if(!House(cx,cy) && !Chariot(cx,cy) && !Cannon(cx,cy) && !General(cx,cy) )            {                flag=1;            }            mp[bx][by] = mp[cx][cy];            mp[cx][cy] = t;        }        if(bx!=3)        {            int cx = bx+1;            int cy = by;            char t = mp[cx][cy];            mp[cx][cy] = mp[bx][by];            mp[bx][by] = '.';            if(!House(cx,cy) && !Chariot(cx,cy) && !Cannon(cx,cy) && !General(cx,cy))            {                flag=1;            }            mp[bx][by] = mp[cx][cy];            mp[cx][cy] = t;        }        if(by!=4)        {            int cx = bx;            int cy = by-1;            char t = mp[cx][cy];            mp[cx][cy] = mp[bx][by];            mp[bx][by] = '.';            if(!House(cx,cy) && !Chariot(cx,cy) && !Cannon(cx,cy) && !General(cx,cy))            {                flag=1;            }            mp[bx][by] = mp[cx][cy];            mp[cx][cy] = t;        }        if(by!=6)        {            int cx = bx;            int cy = by+1;            char t = mp[cx][cy];            mp[cx][cy] = mp[bx][by];            mp[bx][by] = '.';            if(!House(cx,cy) && !Chariot(cx,cy) && !Cannon(cx,cy) && !General(cx,cy))            {                flag=1;            }            mp[bx][by] = mp[cx][cy];            mp[cx][cy] = t;        }        if(flag) cout<<"NO"<<endl;        else            cout<<"YES"<<endl;    }    return 0;}
0 0