字符串模拟
排序
http://poj.org/problem?id=2996
Time Limit: 1000MS | Memory Limit: 65536K | |
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
/* Author : yan * Question : POJ 2996 Help Me with the Game * Data && Time : Tuesday, December 28 2010 07:42 AM */ #include<stdio.h> #include<string.h> typedef struct _node { char value[4]; }; struct _node white[16]; struct _node black[16]; struct _node tmp[16]; char cache[35]; int w_cnt,b_cnt; int tmp_index; struct _node s1,s2; void print() { int j; for(j=0;j<tmp_index;j++) printf(",%s",tmp[j].value); } int cmp_black(const void *a,const void *b) { s1=*(struct _node*)a; s2=*(struct _node*)b; if(s1.value[2]!=s2.value[2]) return s2.value[1]-s1.value[1]; else return s1.value[1]-s2.value[1]; } int cmp_black_2(const void *a,const void *b) { s1=*(struct _node*)a; s2=*(struct _node*)b; if(s1.value[1]!=s2.value[1]) return s2.value[1]-s1.value[1]; else return s1.value[0]-s2.value[0]; } int cmp_white(const void *a,const void *b) { s1=*(struct _node*)a; s2=*(struct _node*)b; if(s1.value[2]!=s2.value[2]) return s1.value[1]-s2.value[1]; else return s1.value[1]-s2.value[1]; } int cmp_white_2(const void *a,const void *b) { s1=*(struct _node*)a; s2=*(struct _node*)b; if(s1.value[1]!=s2.value[1]) return s1.value[1]-s2.value[1]; else return s1.value[0]-s2.value[0]; } int main() { int i,j; int cnt=-1; //freopen("input","r",stdin); while(cnt++<16) { scanf("%s",cache); if( cnt%2!=0 ) { for(j=2;j<31;j+=4) { if(cache[j]>='a'&&cache[j]<='z') { //printf("%c%c%d/n",cache[j]-32,(j/4)+'a',9-(cnt/2+1)); if(cache[j]!='p') sprintf(black[b_cnt++].value,"%c%c%d",cache[j]-32,(j/4)+'a',9-(cnt/2+1)); else sprintf(black[b_cnt++].value,"%c%d",(j/4)+'a',9-(cnt/2+1)); //printf("%s/n",black[b_cnt-1].value); } if(cache[j]>='A'&&cache[j]<='Z') { //printf("%c%c%d/n",cache[j],(j/4)+'a',9-(cnt/2+1)); if(cache[j]!='P') sprintf(white[w_cnt++].value,"%c%c%d",cache[j],(j/4)+'a',9-(cnt/2+1)); else sprintf(white[w_cnt++].value,"%c%d",(j/4)+'a',9-(cnt/2+1)); //printf("%s/n",white[w_cnt-1].value); } } } } printf("White: "); //处理white //处理'K'开头的 for(j=0;j<w_cnt;j++) if(white[j].value[0]=='K') { printf("%s,",white[j].value); break; } //处理'Q'开头的 for(j=0;j<w_cnt;j++) if(white[j].value[0]=='Q') { printf("%s",white[j].value); break; } //处理'R'开头的 tmp_index=0; for(j=0;j<w_cnt;j++) if(white[j].value[0]=='R') tmp[tmp_index++]=white[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_white); print(); //处理'B'开头的 tmp_index=0; for(j=0;j<w_cnt;j++) if(white[j].value[0]=='B') tmp[tmp_index++]=white[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_white); print(); //处理'N开头的 tmp_index=0; for(j=0;j<w_cnt;j++) if(white[j].value[0]=='N') tmp[tmp_index++]=white[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_white); print(); //处理长度为2的 tmp_index=0; for(j=0;j<w_cnt;j++) if(strlen(white[j].value)==2) tmp[tmp_index++]=white[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_white_2); print(); printf("/n"); printf("Black: "); //处理black //处理'K'开头的 for(j=0;j<b_cnt;j++) if(black[j].value[0]=='K') { printf("%s,",black[j].value); break; } //处理'Q'开头的 for(j=0;j<b_cnt;j++) if(black[j].value[0]=='Q') { printf("%s",black[j].value); break; } //处理'R'开头的 tmp_index=0; for(j=0;j<b_cnt;j++) if(black[j].value[0]=='R') tmp[tmp_index++]=black[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_black); print(); //处理'B'开头的 tmp_index=0; for(j=0;j<b_cnt;j++) if(black[j].value[0]=='B') tmp[tmp_index++]=black[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_black); print(); //处理'N'开头的 tmp_index=0; for(j=0;j<b_cnt;j++) if(black[j].value[0]=='N') tmp[tmp_index++]=black[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_black); print(); //处理长度为2的 tmp_index=0; for(j=0;j<b_cnt;j++) if(strlen(black[j].value)==2) tmp[tmp_index++]=black[j]; qsort(tmp,tmp_index,sizeof(tmp[0]),cmp_black_2); print(); printf("/n"); return 0; }