POJ2993——Help Me with the Game(字符串处理+排序)

时间:2023-03-08 20:26:28
POJ2993——Help Me with the Game(字符串处理+排序)

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

题目大意:

    给定一个棋盘,大写字母代表白子,小写字母代表黑子,输出白子和黑子在场上的分布情况(排序后)。

    (+,-,|,.,:)构成其他部分,请无视就好。

    PS:棋盘的左下角坐标为(a,1)。 这里假设为(a,1)而不是(1,a)与程序对应。

    输出Nxy,N属于(KQRBNP) 若N==P 不输出N。

解题思路:

    1.定义了结构体数组W[],B[]。 

 struct Point{
char x,y,date;
}W[],B[];

    2.读取字符串,存入两个数组中

    3.根据题意对两数组进行排序(我先无视了KQRBNP这个排序,直接对(x,y)排序,再在输出字符串时,根据(KQRBNP)遍历数组来构造输出String.)

      白子排序规则:先按行号(1..8)从小到大排,在根据列号(a..h)从小到大排。

      黑子排序规则:先按行号(1..8)从大到小排,在根据列号(a..h)从小到大排。

    4.输出规定格式字符串(用的String类,感觉比C字符串方便很多)

Code:

 #include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct Point
{
char x,y,date;
} W[],B[];
bool cmp1(struct Point a,struct Point b)//黑子排序cmp
{
if (a.y!=b.y) return a.y>b.y;
return a.x<b.x;
}
bool cmp2(struct Point a,struct Point b)//白子排序cmp
{
if (a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
int main()
{
string tmp,str;
cin>>tmp;
int i,j,k1=,k2=;
for (i=; i>=; i--)
{
cin>>str>>tmp;
char t='a';
for (j=; j<=str.length()-; j+=,t++)
{
if (str[j]>='A'&&str[j]<='Z')
{
W[k1].x=t;
W[k1].y=i+'';
W[k1++].date=str[j];
}
if (str[j]>='a'&&str[j]<='z')
{
B[k2].x=t;
B[k2].y=i+'';
B[k2++].date=str[j]-;
}
}
}
string White="White: ",Black="Black: ";
sort(B+,B+k2,cmp1);
sort(W+,W+k1,cmp2);
tmp="KQRBNP";
for (i=; i<=tmp.length()-; i++)
{
for (j=; j<=k1-; j++)
{
if (W[j].date==tmp[i])//String类可以直接写加号来在末尾添加元素
{
if (W[j].date!='P') White+=W[j].date;
White+=W[j].x;
White+=W[j].y;
White+=',';
}
}
}
White.erase(White.length()-,);//去除多余的逗号
for (i=; i<=tmp.length()-; i++)
{
for (j=; j<=k1-; j++)
{
if (B[j].date==tmp[i])
{
if (B[j].date!='P') Black+=B[j].date;
Black+=B[j].x;
Black+=B[j].y;
Black+=',';
}
}
}
Black.erase(Black.length()-,);//去除多余的逗号
cout<<White<<endl<<Black;
return ;
}