Description
Input
Output
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;
}