poj 2996详解(题目很简单只是要认真看清楚题目,我写的代码有点长了,嘻嘻)
2014-05-09 14:20
405 查看
Help Me with the Game
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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3157 | Accepted: 2040 |
+---+---+---+---+---+---+---+---+|.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;}
相关文章推荐
- POJ-2590-Steps题目详解,思路分析及代码,规律题,重要的是找到规律~~
- 两道很有意思的题目POJ 2996,POJ2993
- POJ “顶嵌杯”全国嵌入式系统C语言编程大赛初赛 题目代码
- Ningbo [1220] SPY(题目有点难懂,读懂题目题很简单)
- poj 2828 线段树 单点更新 代码很简单,主要在于思路
- POJ “顶嵌杯” 题目 代码 [菜鸟级别]
- poj 3974 马拉车算法及代码详解
- POJ 2796 Feel Good 单调栈的应用 代码详解
- poj1847 // 很简单的最短路--第一次在协会写题,有点紧张。
- CCF炉石传说题目代码详解C++版(类封装通俗易懂)
- 拓展欧几里得详解 及其题目 POJ 1061 2115 2142 UVA 10673 10090
- POJ题目Java代码(一)
- C语言字符串库函数的实现也是笔试题常考的题目,以下代码没有严格测试,只是简单的实现:
- 【用膝盖写代码系列】(3):NOIP2012普及组题目详解
- poj 1149网络流建模(转载自Edelweiss代码自己编的有点丑陋0 0)
- 【用膝盖写代码系列】(2):NOIP2011普及组复赛题目详解
- CP1300:The Caverns of Mysteriousness [只是题目,无代码]
- 精选微软经典的算法面试100题(第1-20题) -代码详解(题目选自“结构之法”大侠的博客,答案都是本菜鸟自己做的)
- POJ 3254 Corn Fields(状态压缩)代码详解
- poj 1014 -- 01背包 完全背包 多重背包 代码详解