Help Me with the Game
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,e4 Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cstdlib> 5 using namespace std; 6 struct node 7 { 8 int hang; 9 int lie; 10 int data; 11 int jb; 12 } white[33],black[33]; 13 int sw(char p)//只是为了让级别好记,好排序 14 { 15 if(p=='K' || p=='k') return 1; 16 if(p=='Q' || p=='q') return 2; 17 if(p=='R' || p=='r') return 3; 18 if(p=='B' || p=='b') return 4; 19 if(p=='N' || p=='n') return 5; 20 if(p=='P' || p=='p') return 6; 21 return 0; 22 } 23 int cmpw(const void*a,const void *b)//这个排序很是坑爹,最初一直没想这样写,结果还是这样,又快又方便 24 { 25 if(((node*)a)->jb!=((node*)b)->jb) 26 return ((node*)a)->jb-((node*)b)->jb; 27 if(((node*)a)->hang!=((node*)b)->hang) 28 return ((node*)a)->hang-((node*)b)->hang; 29 return ((node*)a)->lie-((node*)b)->lie; 30 } 31 int cmpb(const void*a,const void *b) 32 { 33 if(((node*)a)->jb!=((node*)b)->jb) 34 return ((node*)a)->jb-((node*)b)->jb; 35 if(((node*)a)->hang!=((node*)b)->hang) 36 return ((node*)b)->hang-((node*)a)->hang; 37 return ((node*)a)->lie-((node*)b)->lie; 38 } 39 int main() 40 { 41 int wi=0,bi=0,i; 42 char ch; 43 for(i=8; i>0; i--) 44 { 45 scanf("+---+---+---+---+---+---+---+---+\n"); 46 int sum=0; 47 while(scanf("%c",&ch)&&ch!='\n') 48 { 49 if('A'<=ch&&ch<='Z') 50 { 51 white[wi].hang=i; 52 white[wi].lie=sum;//明显第几列和“|”的数目挂钩 53 white[wi].data=ch; 54 white[wi].jb=sw(ch); 55 wi++; 56 } 57 else if('a'<=ch&&ch<='z') 58 { 59 black[bi].hang=i; 60 black[bi].lie=sum; 61 black[bi].data=ch; 62 black[bi].jb=sw(ch); 63 bi++; 64 } 65 else if(ch=='|')//再剩余的字符就不用看了 66 sum++; 67 } 68 } 69 scanf("+---+---+---+---+---+---+---+---+"); 70 qsort(white,wi,sizeof(node),cmpw);//排序,这个让我纠结了好久,郁闷 71 qsort(black,bi,sizeof(node),cmpb); 72 wi--; 73 bi--; 74 printf("White: ");//这种坑爹的输出,好吧……只是很长,不复杂 75 for(i=0; i<wi; i++) 76 { 77 if(white[i].data!='P') 78 printf("%c",white[i].data); 79 printf("%c",white[i].lie+'a'-1); 80 printf("%d,",white[i].hang); 81 82 } 83 if(white[i].data!='P') 84 printf("%c",white[i].data); 85 printf("%c",white[i].lie+'a'-1); 86 printf("%d\n",white[i].hang); 87 printf("Black: "); 88 for(i=0; i<bi; i++) 89 { 90 if(black[i].data!='p') 91 printf("%c",black[i].data-32); 92 printf("%c",black[i].lie+'a'-1); 93 printf("%d,",black[i].hang); 94 } 95 if(black[i].data!='p') 96 printf("%c",black[i].data-32); 97 printf("%c",black[i].lie+'a'-1); 98 printf("%d\n",black[i].hang); 99 return 0; 100 }