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. 仿《雷霆战机》飞行射击手游开发--GameObject

    转载请注明:http://www.cnblogs.com/thorqq/p/5646509.html 在上一篇中,我们介绍了各种游戏对象的功能及类的集成关系,现在我们来看看GameObject的源代码 ...

  2. java线程安全和线程同步

    第一部分 线程安全(1)——变量安全 http://blog.csdn.net/cuiran/article/details/6150357 第二部分 线程安全(2)——ThreadLocal变量 h ...

  3. ASP.NET 将DataTable解析成JSON简介

    这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...

  4. 将Ecshop后台fckeditor升级更改为kindeditor 4.1.10编辑器

    ecshop在win8部分电脑上,不管用任何浏览器,都打不开,即使升级到最新版本都不行,问题应该吃在fckeditor兼容上.fckeditor 很久未升级,换掉该编辑器是最佳方法 第一步:下载kin ...

  5. 解决CxGrid Filter 后,通过 Dataset 循环时得出的结果与 Grid显示不同步的问题.

    // 方案1: 强制cxgrid 使用 dataset的 Filter GridMaster.DataController.Filter.AutoDataSetFilter := True; /// ...

  6. compared woth QPSK, what is the advantages of QAM(16QAM or 64QAM?)

    1.QPSK QPSK是英文Quadrature Phase Shift Keying的缩略语简称,意为正交相移键控,是一种数字调制方式.在数字信号的调制方式中QPSK四相移键控是目前最常用的一种卫星 ...

  7. Android开发中Eclipse里的智能提示设置

    今天开始学习一下Android开发,直接在Android Developers下载的一个开发工具包,然后再下了一个JDK,配置完环境变量等一系列的工作后环境就搭建好了,在新建好第一个Android项目 ...

  8. FFT Golang 实现

    最近项目要用到快速傅立叶变换,自己写了个算法,测试了下,性能和精度还可以接受 len,time= 1048576 378.186167ms diff=-0.00000000000225974794 I ...

  9. PDF合并

    要求:将多个table导出到一个PDF里,然后打印. 问题分析:要求将四个table放一个PDF打印,四个table的列各不相同,第一个是表头,其他三个是列表,列比表头多很多,如果直接生成一个exce ...

  10. DataGrid中取HyperLinkColumn列的值,处理DataGrid中绑定的特殊字符

    DataGrid中取HyperLinkColumn列的值. /// <summary> /// 对datagrid中标签进行编码,处理特殊字符 /// </summary> / ...