POJ2993——Help Me with the Game(字符串处理+排序)
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(字符串处理+排序)的更多相关文章
- 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 ...
- hdu 1106:排序(水题,字符串处理 + 排序)
排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序
package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...
- 深入理解苹果系统(Unicode)字符串的排序方法
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由iminder发表于云+社区专栏 Unicode编码 我们知道计算机是不能直接处理文本的,而是和数字打交道.因此,为了表示文本,就建立 ...
- java中字符串的排序(1)
按照前段时间在快速.冒泡等排序的评论中提到是否可以进行字符串的排序,由于最近有考试,时间比较紧,所以今天才实现此功能.此功能是针对一串字符川进行的实现,运行后的结果如下所示: 具体的程序相对较为简单, ...
- List中存放字符串进行排序
package com.bjpowernode.t03sort; import java.util.ArrayList;import java.util.Collections; /* * List中 ...
- JS面试Q&A(续):Javascript数组排序, 默认是字符串Unicode排序, 不适合数字
Q:下面代码段的执行后data里面的数据是什么?为什么? var data= [40,1,5,200] data.sort(); A: data的内容是[1, 200, 40, 5] 因为,Javas ...
- Oracle 字符串分割排序冒泡算法
例子: 一个字符串"11,15,13,17,12",以逗号分割,现在要排序成"11,12,13,15,17". 写了一个实现方法,记录下来以备后用: ----- ...
- linq order by charindex 排序 按给定字符串顺序排序
//list=list.OrderBy(ee => SqlFunctions.CharIndex("书记,主任,支部委员,村委委员,系统工作人员", ee.ZhiWu)).T ...
随机推荐
- 服务器发布WebService返回DataTable
初始化Datatable时,需要为Datatable命名.否则在客户端使用时,会报“datatable不能序列化...”导致表格无法从服务器端读取到. 例如: 服务器端: DataTable dt = ...
- item44:将与参数无关的代码抽离template
编写non-template代码中,重复十分明显:可以很直观的看到代码的重复,然后将它们写成一个新的class或者函数,然后供调用. 编写template代码中,重复是隐晦的:只存在一份templat ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- JavaScript jQuery 事件、动画、扩展
事件 因为JavaScript在浏览器中以单线程模式运行,页面加载后,一旦页面上所有的JavaScript代码被执行完后,就只能依赖触发事件来执行JavaScript代码. 浏览器在接收到用户的鼠标或 ...
- 纯CSS3代码实现简单的图片轮播
以4张图片为例:1.基本布局:将4张图片左浮动横向并排放入一个div容器内,图片设置统一尺寸,div宽度设置4个图片的总尺寸,然后放入相框容器div,相框设置1个图片的大小并设置溢出隐藏,以保证正确显 ...
- Keep two divs sync scroll and example
srcDiv has visible horizontal scrollbar.(style="overflow:auto;") targetDiv has no scrollba ...
- Javascript中最常用的55个经典技巧
Javascript中最常用的55个经典技巧1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table ...
- H264相关代码
H.264格式的视频打包成RTP后进行发送,编译环境为VC6++ #include <stdio.h> #include <stdlib.h> #include <str ...
- 机器学习基石的泛化理论及VC维部分整理(第五讲)
第五讲 Training versus Testing 一.问题的提出 \(P_{\mathcal{D}}\left [ BAD \mathcal{D} \right ] \leq 2M \cd ...
- 使用Yeoman搭建 AngularJS 应用 (5) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/scaffold-app.html 基架 (Scaffolding) 在Yeoman中的意思是为基于你特殊的配置需求,为网站程序生成文件的工 ...