您的位置:首页 > 编程语言

poj 2996详解(题目很简单只是要认真看清楚题目,我写的代码有点长了,嘻嘻)

2014-05-09 14:20 405 查看
Help Me with the Game
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 3157Accepted: 2040
DescriptionYour task is to read a picture of a chessboard position and print it in the chess notation.InputThe 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 (".").OutputThe 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 thepieces 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 thatthis 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 digitbetween 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 promotionsof 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,e4Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6
题目大意:
白棋子用大写字母表示,黑棋子用小写字母表示,棋盘从上到下为8~1行,从左到右为a~h列。
对于相同白棋先按行小的排列在前,再按列小的排列前;对于相同黑棋先按行大的排列前,再按列小的排列前。
求棋盘棋子的布局。
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct Node{char ch; int row;}k_[1],K_[1],q_[1],Q_[1],r_[2],R_[2],b_[2],B_[2],n_[2],N_[2],p_[8],P_[8];   //小写表示黑棋,大写表示白棋char a[20][35];       //棋盘布局int mp1[9]={0,1,3,5,7,9,11,13,15};      //棋子在棋盘图中的列int mp2[9]={0,2,6,10,14,18,22,26,30};   //棋子在棋盘途中的行int ro[9]={0,8,7,6,5,4,3,2,1};                         //确定棋子所在行char mp[9]={'0','a','b','c','d','e','f','g','h'};      //确定棋子所在列int cmp1(Node tmp1,Node tmp2){    //白棋子排列方式if(tmp1.row==tmp2.row)return tmp1.ch<tmp2.ch;return tmp1.row<tmp2.row;}int cmp2(Node tmp1,Node tmp2){    //黑棋子排列方式if(tmp1.row==tmp2.row)return tmp1.ch<tmp2.ch;return tmp1.row>tmp2.row;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);for(int i=0;i<17;i++)scanf("%s",a[i]);int flag[13];   //标记各个棋子个数for(int i=0;i<12;i++)flag[i]=0;for(int i=1;i<=8;i++)for(int j=1;j<=8;j++){if(a[mp1[i]][mp2[j]]=='.'||a[mp1[i]][mp2[j]]==':')continue;//确定黑棋布局if(a[mp1[i]][mp2[j]]=='k'){k_[flag[0]].row=ro[i]; k_[flag[0]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='q'){q_[flag[1]].row=ro[i]; q_[flag[1]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='r'){r_[flag[2]].row=ro[i]; r_[flag[2]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='b'){b_[flag[3]].row=ro[i]; b_[flag[3]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='n'){n_[flag[4]].row=ro[i]; n_[flag[4]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='p'){p_[flag[5]].row=ro[i]; p_[flag[5]++].ch=mp[j];}//确定白起布局else if(a[mp1[i]][mp2[j]]=='K'){K_[flag[6]].row=ro[i]; K_[flag[6]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='Q'){Q_[flag[7]].row=ro[i]; Q_[flag[7]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='R'){R_[flag[8]].row=ro[i]; R_[flag[8]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='B'){B_[flag[9]].row=ro[i]; B_[flag[9]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='N'){N_[flag[10]].row=ro[i]; N_[flag[10]++].ch=mp[j];}else if(a[mp1[i]][mp2[j]]=='P'){P_[flag[11]].row=ro[i];P_[flag[11]++].ch=mp[j];}}sort(r_,r_+flag[2],cmp2); sort(b_,b_+flag[3],cmp2); sort(n_,n_+flag[4],cmp2); sort(p_,p_+flag[5],cmp2);    //黑棋排序sort(R_,R_+flag[8],cmp1); sort(B_,B_+flag[9],cmp1); sort(N_,N_+flag[10],cmp1); sort(P_,P_+flag[11],cmp1);  //白棋排序//输出结果,注意格式int sign=0;printf("White: ");if(flag[6]){printf("K%c%d",K_[0].ch,K_[0].row);sign=1;}if(flag[7]){if(sign)printf(",Q%c%d",Q_[0].ch,Q_[0].row);else{printf("Q%c%d",Q_[0].ch,Q_[0].row);sign=1;}}if(flag[8]){if(sign){for(int i=0;i<flag[8];i++)printf(",R%c%d",R_[i].ch,R_[i].row);}else{printf("R%c%d",R_[0].ch,R_[0].row);for(int i=1;i<flag[8];i++)printf(",R%c%d",R_[i].ch,R_[i].row);sign=1;}}if(flag[9]){if(sign){for(int i=0;i<flag[9];i++)printf(",B%c%d",B_[i].ch,B_[i].row);}else{printf("B%c%d",B_[0].ch,B_[0].row);for(int i=1;i<flag[9];i++)printf(",B%c%d",B_[i].ch,B_[i].row);sign=1;}}if(flag[10]){if(sign){for(int i=0;i<flag[10];i++)printf(",N%c%d",N_[i].ch,N_[i].row);}else{printf("N%c%d",N_[0].ch,N_[0].row);for(int i=1;i<flag[10];i++)printf(",N%c%d",N_[i].ch,N_[i].row);sign=1;}}if(flag[11]){if(sign){for(int i=0;i<flag[11];i++)printf(",%c%d",P_[i].ch,P_[i].row);}else{printf("%c%d",P_[0].ch,P_[0].row);for(int i=1;i<flag[11];i++)printf(",%c%d",P_[i].ch,P_[i].row);sign=1;}}printf("\n");printf("Black: ");sign=0;if(flag[0]){printf("K%c%d",k_[0].ch,k_[0].row);sign=1;}if(flag[1]){if(sign)printf(",Q%c%d",q_[0].ch,q_[0].row);else{printf("Q%c%d",q_[0].ch,q_[0].row);sign=1;}}if(flag[2]){if(sign){for(int i=0;i<flag[2];i++)printf(",R%c%d",r_[i].ch,r_[i].row);}else{printf("R%c%d",r_[0].ch,r_[0].row);for(int i=1;i<flag[2];i++)printf(",R%c%d",r_[i].ch,r_[i].row);sign=1;}}if(flag[3]){if(sign){for(int i=0;i<flag[3];i++)printf(",B%c%d",b_[i].ch,b_[i].row);}else{printf("B%c%d",b_[0].ch,b_[0].row);for(int i=1;i<flag[3];i++)printf(",B%c%d",b_[i].ch,b_[i].row);sign=1;}}if(flag[4]){if(sign){for(int i=0;i<flag[4];i++)printf(",N%c%d",n_[i].ch,n_[i].row);}else{printf("N%c%d",n_[0].ch,n_[0].row);for(int i=1;i<flag[4];i++)printf(",N%c%d",n_[i].ch,n_[i].row);sign=1;}}if(flag[5]){if(sign){for(int i=0;i<flag[5];i++)printf(",%c%d",p_[i].ch,p_[i].row);}else{printf("%c%d",p_[0].ch,p_[0].row);for(int i=1;i<flag[5];i++)printf(",%c%d",p_[i].ch,p_[i].row);sign=1;}}printf("\n");return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: