POJ_2996 Help Me with the Game 模拟题

本文介绍了一种将ASCII艺术形式的国际象棋棋盘转换为标准棋谱表示的方法。该方法通过解析输入的特殊字符来确定棋子的位置,并使用快速排序算法按特定规则对棋子进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题来源:[url]http://poj.org/problem?id=2996[/url]
Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2084 Accepted: 1352

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


较为恶心的模拟题,注意快排比较函数的写法。

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<map>
using namespace std;
char maze[9][9];
char temp[50];
typedef char str[4];
str white[65];
str black[65];

int priority(char a,char b)
{
if(a=='K')return -1;
if(b=='K')return 1;
if(a=='Q')return -1;
if(b=='Q')return 1;
if(a=='R')return -1;
if(b=='R')return 1;
if(a=='B')return -1;
if(b=='B')return 1;
if(a=='N')return -1;
if(b=='N')return 1;
return 0;
}

int cmp_w(const void *a,const void *b)
{
str *A = (str*)a;
str *B = (str*)b;
if(isupper((*A)[0])&&isupper((*B)[0]))
{
if((*A)[0]!=(*B)[0])
{
return priority((*A)[0],(*B)[0]);
}
else if((*A)[2]!=(*B)[2])
{
return (*A)[2]-(*B)[2];
}
else
{
return (*A)[1]-(*B)[1];
}
}
else if(isupper((*A)[0])&&islower((*B)[0]))
{
return -1;
}
else if(islower((*A)[0])&&isupper((*B)[0]))
{
return 1;
}
else if(islower((*A)[0])&&islower((*B)[0]))
{
if((*A)[1]!=(*B)[1])
{
return (*A)[1]-(*B)[1];
}
else
{
return (*A)[0]-(*B)[0];
}
}
return 0;
}

int cmp_b(const void *a,const void *b)
{
str *A = (str*)a;
str *B = (str*)b;
if(isupper((*A)[0])&&isupper((*B)[0]))
{
if((*A)[0]!=(*B)[0])
{
return priority((*A)[0],(*B)[0]);
}
else if((*A)[2]!=(*B)[2])//r
{
return (*B)[2]-(*A)[2];
}
else
{
return (*A)[1]-(*B)[1];
}
}
else if(isupper((*A)[0])&&islower((*B)[0]))
{
return -1;
}
else if(islower((*A)[0])&&isupper((*B)[0]))
{
return 1;
}
else if(islower((*A)[0])&&islower((*B)[0]))
{
if((*A)[1]!=(*B)[1])
{
return (*B)[1]-(*A)[1];
}
else
{
return (*A)[0]-(*B)[0];
}
}
return 0;
}

int main()
{
int cnt = 0;
int row = 0;
//freopen("datain.txt","r",stdin);
//freopen("dataout.txt","w",stdout);
while(gets(temp)!=NULL)
{
if(cnt==16)
{
int ct_w = 0;
int ct_b = 0;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(isupper(maze[i][j]))
{
if(maze[i][j]=='P')
{
white[ct_w][0] = 'a'+j;
white[ct_w][1] = '0'+(8-i);
white[ct_w++][2] = '\0';
}
else
{
white[ct_w][0] = toupper(maze[i][j]);
white[ct_w][1] = 'a'+j;
white[ct_w][2] = '0'+(8-i);
white[ct_w++][3] = '\0';
}
}
else if(islower(maze[i][j]))
{
if(maze[i][j]=='p')
{
black[ct_b][0] = 'a'+j;
black[ct_b][1] = '0'+(8-i);
black[ct_b++][2] = '\0';
}
else
{
black[ct_b][0] = toupper(maze[i][j]);
black[ct_b][1] = 'a'+j;
black[ct_b][2] = '0'+(8-i);
black[ct_b++][3] = '\0';
}
}
}
}
qsort(white,ct_w,sizeof(str),cmp_w);
qsort(black,ct_b,sizeof(str),cmp_b);
printf("White: ");
for(int i=0;i<ct_w;i++)
if(i!=0)printf(",%s",white[i]);
else printf("%s",white[i]);
printf("\n");
printf("Black: ");
for(int i=0;i<ct_b;i++)
if(i!=0)printf(",%s",black[i]);
else printf("%s",black[i]);
printf("\n");
cnt = 0;
row = 0;
continue;
}
else if(cnt%2==1)
{
int count = 0;
for(int i=2;i<33;i+=4)
{
maze[row][count++] = temp[i];
}//save the chessboard
row++;
}
cnt++;
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值