首页 > 代码库 > POJ2993——Help Me with the Game
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[]。
1 struct Point{ 2 char x,y,date; 3 }W[100],B[100];
2.读取字符串,存入两个数组中
3.根据题意对两数组进行排序(我先无视了KQRBNP这个排序,直接对(x,y)排序,再在输出字符串时,根据(KQRBNP)遍历数组来构造输出String.)
白子排序规则:先按行号(1..8)从小到大排,在根据列号(a..h)从小到大排。
黑子排序规则:先按行号(1..8)从大到小排,在根据列号(a..h)从小到大排。
4.输出规定格式字符串(用的String类,感觉比C字符串方便很多)
Code:
1 #include<string> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 struct Point 6 { 7 char x,y,date; 8 } W[100],B[100]; 9 bool cmp1(struct Point a,struct Point b)//黑子排序cmp 10 { 11 if (a.y!=b.y) return a.y>b.y; 12 return a.x<b.x; 13 } 14 bool cmp2(struct Point a,struct Point b)//白子排序cmp 15 { 16 if (a.y!=b.y) return a.y<b.y; 17 return a.x<b.x; 18 } 19 int main() 20 { 21 string tmp,str; 22 cin>>tmp; 23 int i,j,k1=1,k2=1; 24 for (i=8; i>=1; i--) 25 { 26 cin>>str>>tmp; 27 char t=‘a‘; 28 for (j=2; j<=str.length()-1; j+=4,t++) 29 { 30 if (str[j]>=‘A‘&&str[j]<=‘Z‘) 31 { 32 W[k1].x=t; 33 W[k1].y=i+‘0‘; 34 W[k1++].date=str[j]; 35 } 36 if (str[j]>=‘a‘&&str[j]<=‘z‘) 37 { 38 B[k2].x=t; 39 B[k2].y=i+‘0‘; 40 B[k2++].date=str[j]-32; 41 } 42 } 43 } 44 string White="White: ",Black="Black: "; 45 sort(B+1,B+k2,cmp1); 46 sort(W+1,W+k1,cmp2); 47 tmp="KQRBNP"; 48 for (i=0; i<=tmp.length()-1; i++) 49 { 50 for (j=1; j<=k1-1; j++) 51 { 52 if (W[j].date==tmp[i])//String类可以直接写加号来在末尾添加元素 53 { 54 if (W[j].date!=‘P‘) White+=W[j].date; 55 White+=W[j].x; 56 White+=W[j].y; 57 White+=‘,‘; 58 } 59 } 60 } 61 White.erase(White.length()-1,1);//去除多余的逗号 62 for (i=0; i<=tmp.length()-1; i++) 63 { 64 for (j=1; j<=k1-1; j++) 65 { 66 if (B[j].date==tmp[i]) 67 { 68 if (B[j].date!=‘P‘) Black+=B[j].date; 69 Black+=B[j].x; 70 Black+=B[j].y; 71 Black+=‘,‘; 72 } 73 } 74 } 75 Black.erase(Black.length()-1,1);//去除多余的逗号 76 cout<<White<<endl<<Black; 77 return 0; 78 }