问题来源:
http://poj.org/problem?id=2996
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;
}
分享到:
相关推荐
北大POJ2996-Help Me with the Game 解题报告+AC代码
2遍dp poj_3613解题报告 poj_3613解题报告
poj题目2775文件子目录源代码,递归经典题目,
poj典型题目解题思路详解 包含源代码和解题时应注意的问题及题目陷阱设计分析
poj 1699的代码和方法说明,个人原创
C_(POJ_1854)(分治).cpp
poj上第1990题目源码,用到了2个树状数组,这题数据结构是关键,想到了题目就很简单了
poj2996代码,欢迎下载 下载,下载
D_(POJ_1723)(思维)(sort).cpp
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦
D_(POJ_1723)(思维)(第k大数).cpp
POJ 3131 双向BFS解立体八数码问题
O(nlogn)凸包问题 poj2187
poj两道题的c++实现。已经测试过可以通过oj
北大POJ1027-The Same Game 解题报告+AC代码
POJ题目及算法,包括动态规划、深搜广搜等算法。含相关注释。
这是北大在线测试的第1002题,方便记忆的电话号码的解题例程,题目中有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,这个程序的任务就是找到它们
问题:求平面上多个矩形的总面积。 算法:线段树(经典的线段树题目)
poj 3310 的代码和方法说明,个人原创
http://poj.grids.cn/problem?id=2774 POJ 2774 木棒加工 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给定了。当然,我们希望得到的小段越长越好,你的任务是计算能够...