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. salvage 数据块打捞工具

    基本上所有数据库都是按块存储数据的,每种数据库的块都有自己有特征,我们可以找出特征,当数据库文件丢失,甚至文件系统完全损坏时,从硬盘扇区中把数据页打捞出来,从页从数据页中恢复出行数据.根据这个原理,开 ...

  2. Kernel Panic常见原因以及解决方法

    Technorati 标签: Kernel Panic 出现原因 1. Linux在中断处理程序中,它不处于任何一个进程上下文,如果使用可能睡眠的函数,则系统调度会被破坏,导致kernel panic ...

  3. winfrom 水晶按钮

    闲来无事,从网上找了不少自定义控件,然后整理了一下,做了一个水晶按钮 /// <summary> /// 表示 Windows 的按钮控 /// </summary> [Des ...

  4. php 字母大小写转换的函数

    分享下,在php编程中,将字母大小写进行转换的常用函数. 1.将字符串转换成小写strtolower(): 该函数将传入的字符串参数所有的字符都转换成小写,并以小定形式放回这个字符串 2.将字符转成大 ...

  5. 简单3D翻转

    1.先上图~~~ 2.代码 html部分 <body> <div id="my3d"> <div id="box"> < ...

  6. 《Java并发编程实战》读书笔记(更新中)

    一.简介 1.多线程编程要注意的几点: 安全性:永远不发生糟糕的事情 活跃性:某件正确的事情最终会发生(不会发生无限循环或者死锁) 性能:正确的事尽快发生(上下文切换消耗之类的) 二.线程安全 1.为 ...

  7. Oracle用户进程跟踪

    用户进程跟踪 分为 基于会话级别跟踪和 实例级别跟踪: 会话级别跟踪又包括 当前会话跟踪和 非当前会话跟踪 跟踪文件位置由user_dump_dest设定,大小由max_dump_file_size ...

  8. 使用OpenXml把Excel中的数据导出到DataSet中

    public class OpenXmlHelper { /// <summary> /// 读取Excel数据到DataSet中,默认读取所有Sheet中的数据 /// </sum ...

  9. Unity3D游戏开发——Asset Server搭建

    本系列文章由 Amazonzx 编写,欢迎转载,转载请注明出处. http://blog.csdn.net/amazonzx/article/details/7980117 Asset Server是 ...

  10. 【BZOJ 1087】[SCOI2005]互不侵犯King

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...