Help Me with the Game(POJ--2996

Description

Your task is to read a picture of a chessboard position and print it in the chess notation.

Input

The input consists of an ASCII-art picture of a chessboard with chess pieces on positions described by the input. The pieces of the white player are shown in upper-case letters, while the black player's pieces are lower-case letters. The letters are one of "K" (King), "Q" (Queen), "R" (Rook), "B" (Bishop), "N" (Knight), or "P" (Pawn). The chessboard outline is made of plus ("+"), minus ("-"), and pipe ("|") characters. The black fields are filled with colons (":"), white fields with dots (".").

Output

The output consists of two lines. The first line consists of the string "White: ", followed by the description of positions of the pieces of the white player. The second line consists of the string "Black: ", followed by the description of positions of the pieces of the black player. 

The description of the position of the pieces is a comma-separated list of terms describing the pieces of the appropriate player. The description of a piece consists of a single upper-case letter that denotes the type of the piece (except for pawns, for that this identifier is omitted). This letter is immediatelly followed by the position of the piece in the standard chess notation -- a lower-case letter between "a" and "h" that determines the column ("a" is the leftmost column in the input) and a single digit between 1 and 8 that determines the row (8 is the first row in the input). 

The pieces in the description must appear in the following order: King("K"), Queens ("Q"), Rooks ("R"), Bishops ("B"), Knights ("N"), and pawns. Note that the numbers of pieces may differ from the initial position because of capturing the pieces and the promotions of pawns. In case two pieces of the same type appear in the input, the piece with the smaller row number must be described before the other one if the pieces are white, and the one with the larger row number must be described first if the pieces are black. If two pieces of the same type appear in the same row, the one with the smaller column letter must appear first.
题意:输入一个国际象棋棋盘,棋盘上有相应的棋子,小写字母是黑棋,大写字母是白棋,要求按一定顺序输出黑白棋的位置,先输出国王的位置(K),再输出皇后的位置(Q),再输出车的位置(R),再输出象的位置(B),再输出马的位置(N),最后再按一定顺序输出兵的位置。对于白棋,如果棋的类型相同则按行升序输出;对于黑棋,如果棋的类型相同则按行降序输出。如果棋的类型相同行序也相同则按列升序输出。对于列数是要求按字典序输出小写字母。
思路:遍历整个图,并记录在一个结构体数组里并排序,最后按要求输出。

Sample Input

+---+---+---+---+---+---+---+---+
|.r.|:::|.b.|:q:|.k.|:::|.n.|:r:|
+---+---+---+---+---+---+---+---+
|:p:|.p.|:p:|.p.|:p:|.p.|:::|.p.|
+---+---+---+---+---+---+---+---+
|...|:::|.n.|:::|...|:::|...|:p:|
+---+---+---+---+---+---+---+---+
|:::|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|...|:::|...|:::|.P.|:::|...|:::|
+---+---+---+---+---+---+---+---+
|:P:|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|.P.|:::|.P.|:P:|...|:P:|.P.|:P:|
+---+---+---+---+---+---+---+---+
|:R:|.N.|:B:|.Q.|:K:|.B.|:::|.R.|
+---+---+---+---+---+---+---+---+

Sample Output

White: Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,a2,c2,d2,f2,g2,h2,a3,e4
Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6

#include <cstdio>
#include <algorithm>
using namespace std;
char map[20][50];
struct node
{
    int type;                           //记录棋的类型即等级
    int x,y;
    bool black;                      //标记黑白棋
    bool operator < (const node a)const   //重载运算符<,const相当于本身,a相当于另一个同类型的结构体
    {
        if(type!=a.type)           //如果棋的类型不同则按等级排序
            return type<a.type;
        if(x!=a.x)                      //如果行数不同则按行序排序
        {
            if(!black)
                return x<a.x;
            else
                return a.x<x;
        }
        return y<a.y;              //如果棋的类型和行数都相同则按列序排序
    }
} ant[2][100];                      //ant[0]代表黑棋,ant[1]代表白棋
int pri(char ch)                   //划分等级
{
    if(ch=='K'||ch=='k')
        return 0;
    if(ch=='Q'||ch=='q')
        return 1;
    if(ch=='R'||ch=='r')
        return 2;
    if(ch=='B'||ch=='b')
        return 3;
    if(ch=='N'||ch=='n')
        return 4;
    return 5;
}
int main()
{
    //freopen("aa.text","r",stdin);
    int a=0,b=0;
    for(int i=1; i<=17; i++)
        scanf("%s",map[i]+1);
    for(int i=2; i<=16; i+=2)
        for(int j=3; j<=31; j+=4)
        {
            if(map[i][j]>='a'&&map[i][j]<='z')
            {

                ant[0][a].type=pri(map[i][j]);
                ant[0][a].x=9-i/2;
                ant[0][a].y=(j-3)/4+1;
                ant[0][a].black=true;
                a++;
            }
            else if(map[i][j]>='A'&&map[i][j]<='Z')
            {
                ant[1][b].type=pri(map[i][j]);
                ant[1][b].x=9-i/2;
                ant[1][b].y=(j-3)/4+1;
                ant[1][b].black=false;
                b++;
            }
        }
    sort(ant[0],ant[0]+a);                    //给黑棋排序
    sort(ant[1],ant[1]+b);                    //给白棋排序
    printf("White: ");
    for(int i=0; i<b; i++)
    {
        if(ant[1][i].type==0)
            printf("K%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
        else if(ant[1][i].type==1)
            printf("Q%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
        else if(ant[1][i].type==2)
            printf("R%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
        else if(ant[1][i].type==3)
            printf("B%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
        else if(ant[1][i].type==4)
            printf("N%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
        else
            printf("%c%d%c",ant[1][i].y-1+'a',ant[1][i].x,i==b-1?'\n':',');
    }
    printf("Black: ");
    for(int i=0; i<a; i++)
    {
        if(ant[0][i].type==0)
            printf("K%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
        else if(ant[0][i].type==1)
            printf("Q%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
        else if(ant[0][i].type==2)
            printf("R%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
        else if(ant[0][i].type==3)
            printf("B%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
        else if(ant[0][i].type==4)
            printf("N%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
        else
            printf("%c%d%c",ant[0][i].y-1+'a',ant[0][i].x,i==b-1?'\n':',');
    }
    return 0;
}<strong>
</strong>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值