您的位置:首页 > 其它

POJ 2996 Help Me with the Game

2010-12-28 13:08 323 查看
字符串模拟

排序

http://poj.org/problem?id=2996

Help Me with the Game

Time Limit: 1000MSMemory Limit: 65536K
Description

Your task is to read a picture of a chessboard position and print it in the chess notation.
Input

The 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 (".").
Output

The 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 the pieces 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 that this 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 digit between 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 promotions of 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,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;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: