ACM: 模拟题 poj 2996

                                                            Help Me with the Game

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.

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

 

题意: 国际象棋,棋盘黑白相间,“:”代表黑 , “.” 代表白. 现在把图转换成字符串描述.

 

解题思路:

               1. 关键是排序:

                             白棋: 种类等级K>Q>R>B>N>P.

                                      等级一样,行号小的排前面,行号相等,列号小的排前面.

                             黑棋: 等级同白棋.

                                      等级一样,行号大的排前面,行号相等,列号小的排前面.

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <stdlib.h>
using namespace std;
#define MAX 150

struct node
{
    char style;
    int x, y;
    int level;
};

node black[65];
node white[65];
int b_num;
int w_num;
char str[20][MAX];

int getid(char ch)
{
    if(ch == 'K' || ch == 'k') return 0;
    else if(ch == 'Q' || ch == 'q') return 1;
    else if(ch == 'R' || ch == 'r') return 2;
    else if(ch == 'B' || ch == 'b') return 3;
    else if(ch == 'N' || ch == 'n') return 4;
    else if(ch == 'P' || ch == 'p') return 5;
}

int whitecmp(const void *a,const void *b)
{
    node *aa = (node*)a;
    node *bb = (node*)b;
    if(aa->level != bb->level)
        return aa->level - bb->level;
    else if(aa->x != bb->x)
        return aa->x - bb->x;
    else
        return aa->y - bb->y;
}

int blackcmp(const void *a,const void *b)
{
    node *aa = (node*)a;
    node *bb = (node*)b;
    if(aa->level != bb->level)
        return aa->level - bb->level;
    else if(aa->x != bb->x)
        return bb->x - aa->x;
    else
        return aa->y - bb->y;    
}

void init()
{
    memset(black,-1,sizeof(black));
    memset(white,-1,sizeof(white));
    b_num = 0;
    w_num = 0;
    for(int i = 0; i < 17; ++i)
    {
        scanf("%s",str[i]);
    }
    
    for(int i = 1,row = 1; i < 17; i += 2,row++)
    {
        int len = strlen(str[i]);
        for(int j = 2,column = 1; j < len; j += 4,column++)
        {
            if(str[i][j] == ':' || str[i][j] == '.')
                continue;
            else if(str[i][j] > 'Z')
            {
                black[b_num].style = str[i][j];
                black[b_num].x = 9-row;
                black[b_num].y = column;
                black[b_num++].level = getid(str[i][j]);
            }
            else
            {
                white[w_num].style = str[i][j];
                white[w_num].x = 9-row;
                white[w_num].y = column;
                white[w_num++].level = getid(str[i][j]);
            }
        }
    }
    qsort(black,b_num,sizeof(black[0]),blackcmp);
    qsort(white,w_num,sizeof(white[0]),whitecmp);
}

void run()
{
    printf("White: ");
    for(int i = 0; i < w_num; ++i)
    {
        if(white[i].style != 'P')
            printf("%c%c%d",white[i].style,white[i].y+'a'-1,white[i].x);
        else
            printf("%c%d",white[i].y+'a'-1,white[i].x);
        if(i < w_num-1)
            printf(",");
    }
    printf("\n");
    
    printf("Black: ");
    for(int i = 0; i < b_num; ++i)
    {
        if(black[i].style != 'p')
            printf("%c%c%d",black[i].style-'a'+'A',black[i].y+'a'-1,black[i].x);
        else
            printf("%c%d",black[i].y+'a'-1,black[i].x);
        if(i < b_num-1)
            printf(",");
    }
    printf("\n");
}

int main()
{
//    freopen("input.txt","r",stdin);
    init();
    run();
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值