Help Me with the Game

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

题目大意:

    给定一个棋盘,大写字母代表白子,小写字母代表黑子,输出白子和黑子在场上的分布情况(排序后)。

    (+,-,|,.,:)构成其他部分,请无视就好。

    PS:棋盘的左下角坐标为(a,1)。 这里假设为(a,1)而不是(1,a)与程序对应。

    输出Nxy,N属于(KQRBNP) 若N==P 不输出N。

解题思路:

    1.定义了结构体数组W[],B[]。 

 struct Point{
char x,y,date;
}W[],B[];

    2.读取字符串,存入两个数组中

    3.根据题意对两数组进行排序(我先无视了KQRBNP这个排序,直接对(x,y)排序,再在输出字符串时,根据(KQRBNP)遍历数组来构造输出String.)

      白子排序规则:先按行号(1..8)从小到大排,在根据列号(a..h)从小到大排。

      黑子排序规则:先按行号(1..8)从大到小排,在根据列号(a..h)从小到大排。

    4.输出规定格式字符串(用的String类,感觉比C字符串方便很多)

Code:

 #include<string>
#include<iostream>
#include<algorithm>
using namespace std;
struct Point
{
char x,y,date;
} W[],B[];
bool cmp1(struct Point a,struct Point b)//黑子排序cmp
{
if (a.y!=b.y) return a.y>b.y;
return a.x<b.x;
}
bool cmp2(struct Point a,struct Point b)//白子排序cmp
{
if (a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
int main()
{
string tmp,str;
cin>>tmp;
int i,j,k1=,k2=;
for (i=; i>=; i--)
{
cin>>str>>tmp;
char t='a';
for (j=; j<=str.length()-; j+=,t++)
{
if (str[j]>='A'&&str[j]<='Z')
{
W[k1].x=t;
W[k1].y=i+'';
W[k1++].date=str[j];
}
if (str[j]>='a'&&str[j]<='z')
{
B[k2].x=t;
B[k2].y=i+'';
B[k2++].date=str[j]-;
}
}
}
string White="White: ",Black="Black: ";
sort(B+,B+k2,cmp1);
sort(W+,W+k1,cmp2);
tmp="KQRBNP";
for (i=; i<=tmp.length()-; i++)
{
for (j=; j<=k1-; j++)
{
if (W[j].date==tmp[i])//String类可以直接写加号来在末尾添加元素
{
if (W[j].date!='P') White+=W[j].date;
White+=W[j].x;
White+=W[j].y;
White+=',';
}
}
}
White.erase(White.length()-,);//去除多余的逗号
for (i=; i<=tmp.length()-; i++)
{
for (j=; j<=k1-; j++)
{
if (B[j].date==tmp[i])
{
if (B[j].date!='P') Black+=B[j].date;
Black+=B[j].x;
Black+=B[j].y;
Black+=',';
}
}
}
Black.erase(Black.length()-,);//去除多余的逗号
cout<<White<<endl<<Black;
return ;
}

POJ2993——Help Me with the Game(字符串处理+排序)的更多相关文章

  1. POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)

    Emag eht htiw Em Pleh DescriptionThis problem is a reverse case of the problem 2996. You are given t ...

  2. hdu 1106:排序(水题,字符串处理 + 排序)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  3. Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序

    package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...

  4. 深入理解苹果系统(Unicode)字符串的排序方法

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由iminder发表于云+社区专栏 Unicode编码 我们知道计算机是不能直接处理文本的,而是和数字打交道.因此,为了表示文本,就建立 ...

  5. java中字符串的排序(1)

    按照前段时间在快速.冒泡等排序的评论中提到是否可以进行字符串的排序,由于最近有考试,时间比较紧,所以今天才实现此功能.此功能是针对一串字符川进行的实现,运行后的结果如下所示: 具体的程序相对较为简单, ...

  6. List中存放字符串进行排序

    package com.bjpowernode.t03sort; import java.util.ArrayList;import java.util.Collections; /* * List中 ...

  7. JS面试Q&A(续):Javascript数组排序, 默认是字符串Unicode排序, 不适合数字

    Q:下面代码段的执行后data里面的数据是什么?为什么? var data= [40,1,5,200] data.sort(); A: data的内容是[1, 200, 40, 5] 因为,Javas ...

  8. Oracle 字符串分割排序冒泡算法

    例子: 一个字符串"11,15,13,17,12",以逗号分割,现在要排序成"11,12,13,15,17". 写了一个实现方法,记录下来以备后用: ----- ...

  9. linq order by charindex 排序 按给定字符串顺序排序

    //list=list.OrderBy(ee => SqlFunctions.CharIndex("书记,主任,支部委员,村委委员,系统工作人员", ee.ZhiWu)).T ...

随机推荐

  1. HR不会告诉你的秘密

    原文转载自http://blog.csdn.net/happy08god/article/details/5534326 下面,只是摘出来一些基本的观点. 1. 入职时的工资高低不重要,只要你努力工作 ...

  2. Poj 2707 Copier Reduction

    1.Link: http://poj.org/problem?id=2707 2.Content: Copier Reduction Time Limit: 1000MS   Memory Limit ...

  3. LESS学习总结

    之前在工作过程中,用到了Less,一直没有将学习心得整理归纳,今天终于空出时间整理了一下.   Less学习常用参考文档: Less 中文网 http://lesscss.cn/ 快速入门 | Les ...

  4. PHP json_encode中日语问题

    <?php header('Content-type:text/html;charset=utf-8'); $s = array('message'=>'4月以降.遺体の捜索活動が続けられ ...

  5. js鼠标及对象坐标控制属性详细解析

    对js鼠标及对象坐标控制属性进行了详细的分析介绍.  offsetTop获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算顶端位置. offsetLeft获取对象相对于版面或由 ...

  6. php截取字符串的实例代码(支持utf-8)

    分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...

  7. 运用百度开放平台接口根据ip地址获取位置

    使用百度开放平台接口根据ip地址获取位置 今天无意间发现在百度开放平台接口,就把一段代码拿了下来,有需要的可以试试看:http://opendata.baidu.com/api.php?query=5 ...

  8. Demo学习: ClientEvents

    ClientEvents 在控件的ClientEvents属性里嵌入JS代码,增加了开发的灵活性. 分别在TUniPanel和TUniTimer的 ClientEvents事件里添加了JS代码: 1. ...

  9. Spark Streaming揭秘 Day20 动态Batch size实现初探(上)

    Spark Streaming揭秘 Day20 动态Batch size实现初探(上) 今天开始,主要是通过对动态Batch size调整的论文的解析,来进一步了解SparkStreaming的处理机 ...

  10. python3 pyqt5 和eric5配置教程

    一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...