Help Me with the Game

本文介绍了一种方法,用于解析给定的ASCII艺术形式的国际象棋棋盘,并将其转换为标准的棋盘表示法。文章详细描述了输入格式、输出格式以及具体的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Source








/*
题意:给定一个棋盘,输出个个棋子的位置。行数字表示,列字母表示。


注意:1、大写字母表示白色,小写字母表示黑色,表中的标点并没什么卵用。


2、该棋盘的排序和矩阵的行相反,即矩阵最后一行为棋盘第一行。


3、白棋按照行和列的升序排列。


4、黑棋行按照行降序排列,列升序排序。


5、最后的输出不带标点,列的字母为大写。


思路:数组存储棋子的类型,白棋按照从最后一行到第一行开始扫,
黑棋按照从第一行到最后一行扫。如果存在棋子,输出其位置。*/
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char a[6]={'K','Q','R','B','N','P'};//白棋
char b[6]={'k','q','r','b','n','p'};//黑棋;
char map[40][40];//存储棋子的类型;
int main()
{
     int i,j,s1=0,s2=0,k;
     for(i=1;i<=17;i++)
     {
         for(j=1;j<=33;j++)
         {
             cin>>map[i][j];
             if(map[i][j]=='P')
                 s1++;//统计P的个数;
             if(map[i][j]=='p')
                 s2++;//统计p的个数;
         }
     }
     cout<<"White: ";
     for(k=0;k<6;k++)//K,Q,R,B,N,P的循环;
     {
         for(i=17;i>=1;i--)//白棋从下往上寻找;
         {
             for(j=1;j<=33;j++)//从第一列升序查找;
             {
                 if(k<=4&&i%2==0&&j%4==3)//只有i是偶数时才有棋子,y同样;
                 {
                     if(map[i][j]==a[k])
                     {
                         printf("%c%c%d,",a[k],'a'+(j+1)/4-1,9-i/2);
                     }
                 }
                 if(k==5&&i%2==0&&j%4==3)//a[k]=P时;
                 {
                     if(map[i][j]==a[k])
                     {
                         s1--;
                         if(s1==0)
                        printf("%c%d\n",'a'+(j+1)/4-1,9-i/2);//最后一个p的位置;
                          else
                            printf("%c%d,",'a'+(j+1)/4-1,9-i/2);
                     }
                 }
             }
         }
     }
     cout<<"Black: ";
     for(k=0;k<6;k++)
     {
         for(i=1;i<=17;i++)
         {
             for(j=1;j<=33;j++)
             {
                 if(k<=4&&i%2==0&&j%4==3)
                 {
                     if(map[i][j]==b[k])
                     {
                         printf("%c%c%d,",a[k],'a'+(j+1)/4-1,9-i/2);
                     }
                 }
                 if(k==5&&i%2==0&&j%4==3)
                 {
                     if(map[i][j]==b[k])
                     {
                         s2--;
                         if(s2==0)
                            printf("%c%d\n",'a'+(j+1)/4-1,9-i/2);
                         else
                            printf("%c%d,",'a'+(j+1)/4-1,9-i/2);
                     }
                 }
             }
         }
     }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值