Description
Input
Output
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
模拟题,找出棋盘中的位置然后排个序输出即可。
#include<cstdio> #include<algorithm> #include<iostream> #include<vector> using namespace std; char s[20][50]; struct abc { int x; char y,z; abc(){}; abc(int x,char y,char z):x(x),y(y),z(z){}; }; bool cmp(const abc&a,const abc&b) { if (a.x==b.x) return a.y<b.y; return a.x<b.x; } bool cmp2(const abc&a,const abc&b) { if (a.x==b.x) return a.y<b.y; return a.x>b.x; } vector<abc> p[2][6]; int get(char c) { if (c<='Z') c+=32; if (c=='k') return 0; if (c=='q') return 1; if (c=='r') return 2; if (c=='b') return 3; if (c=='n') return 4; if (c=='p') return 5; } int main() { while (true) { if (scanf("%s",s[0])==EOF) break; else for (int i=1;i<17;i++) scanf("%s",s[i]); for (int i=1;i<17;i+=2) for (int j=2;j<33;j+=4) if (s[i][j]!='.'&&s[i][j]!=':') { int z,zz; if (s[i][j]<='Z') z=0; else {z=1; s[i][j]-=32;} zz=get(s[i][j]); p[z][zz].push_back(abc(9-(i+1)/2,'a'+(j-2)/4,s[i][j])); } for (int j=0;j<6;j++) sort(p[0][j].begin(),p[0][j].end(),cmp); for (int j=0;j<6;j++) sort(p[1][j].begin(),p[1][j].end(),cmp2); printf("White: "); int f=0; for (int j=0;j<6;j++) { for (int i=0;i<p[0][j].size();i++) { if (f) printf(","); else f=1; if (j<5) printf("%c",p[0][j][i].z); printf("%c%d",p[0][j][i].y,p[0][j][i].x); } } f=0; printf("\nBlack: "); for (int j=0;j<6;j++) { for (int i=0;i<p[1][j].size();i++) { if (f) printf(","); else f=1; if (j<5) printf("%c",p[1][j][i].z); printf("%c%d",p[1][j][i].y,p[1][j][i].x); } } printf("\n"); } return 0; }