这道题里主要学习了sort函数、sort的cmp函数写法、C++的map用法(其实和数组一样)

Your task is to read a picture of a chessboard position and print it in the chess notation.

Input Specification

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 Specification

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
代码:
 /*
* 2312_Help Me with the Game.cpp
*
* Created on: 2018年10月23日
* Author: Jeaosn
*/ #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <sstream>
using namespace std; map< char , int > mymap;
map< int , string > column;
char chess[][];
vector<string> white_KQRBN;
vector<string> white_P;
vector<string> black_KQRBN;
vector<string> black_P; string num2str(int i)
{
stringstream ss;
ss<<i;
return ss.str();
} bool cmp_white_KQRBN(string a ,string b){
if(a[] == b[]){
if(a[] == b[])
return a[] < b[]; //如果第一位第三位相同,按照第二位升序
return a[] < b[]; //如果第一位相同,第三位升序排列
}
return mymap[a[]] < mymap[b[]]; //首先按照首位升序
} bool cmp_black_KQRBN(string a ,string b){
if(a[] == b[]){
if(a[] == b[])
return a[] < b[]; //如果第一位第三位相同,按照第二位升序
return a[] > b[]; //如果第一位相同,第三位升序排列
}
return mymap[a[]] < mymap[b[]]; //首先按照首位升序
} bool cmp_white_P(string a ,string b){
if(a[] == b[]){
return a[] < b[]; //如果第一位相同,第三位升序排列
}
return a[] < b[]; //首先按照第二位升序
} bool cmp_black_P(string a ,string b){
if(a[] == b[]){
return a[] < b[]; //如果第一位相同,第三位升序排列
}
return a[] > b[]; //首先按照第二位升序
} int main()
{
mymap['K'] = ;mymap['Q'] = ;mymap['R'] = ;mymap['B'] = ;mymap['N'] = ;
column[] = "a";column[] = "b";column[] = "c";column[] = "d";
column[] = "e";column[] = "f";column[] = "g";column[] = "h"; for(int i = ;i >= ; i--)
for(int j = ;j <= ; j++)
cin >> chess[i][j]; for(int i = ;i <= ; i+=){
for(int j = ;j <= ; j+=){
if(chess[i][j] == 'K')
white_KQRBN.push_back( "K" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'Q')
white_KQRBN.push_back( "Q" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'R')
white_KQRBN.push_back( "R" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'B')
white_KQRBN.push_back( "B" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'N')
white_KQRBN.push_back( "N" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'P')
white_P.push_back( column[ (j/)+ ] + num2str(i/) ); if(chess[i][j] == 'k')
black_KQRBN.push_back( "K" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'q')
black_KQRBN.push_back( "Q" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'r')
black_KQRBN.push_back( "R" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'b')
black_KQRBN.push_back( "B" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'n')
black_KQRBN.push_back( "N" + column[ (j/)+ ] + num2str(i/) );
if(chess[i][j] == 'p')
black_P.push_back( column[ (j/)+ ] + num2str(i/) );
}
}
sort(white_KQRBN.begin() , white_KQRBN.end() ,cmp_white_KQRBN);
sort(black_KQRBN.begin() , black_KQRBN.end() ,cmp_black_KQRBN);
sort(white_P.begin() , white_P.end() ,cmp_white_P);
sort(black_P.begin() , black_P.end() ,cmp_black_P); cout << "White: ";
for(int i = ;i < white_KQRBN.size();i++){
cout << white_KQRBN[i] << ",";
}
for(int i = ;i < white_P.size();i++){
if(i == (white_P.size() - ))
cout << white_P[i] << endl;
else
cout << white_P[i] << ",";
} cout << "Black: ";
for(int i = ;i < black_KQRBN.size();i++){
cout << black_KQRBN[i] << ",";
}
for(int i = ;i < black_P.size();i++){
if(i == (black_P.size() - ) )
cout << black_P[i] << endl;
else
cout << black_P[i] << ",";
}
return ;
}

这道题里主要学习了sort函数、sort的cmp函数写法、C++的map用法(其实和数组一样)

sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game的更多相关文章

  1. Java中Collections类的排序sort函数两种用法

    java中的Colletions类主要实现列表List的排序功能.根据函数参数的传递,具体的排序可以分为 : 1.  自然排序(natural ordering). 函数原型:sort(List< ...

  2. Perl Sort函数用法总结和使用实例

    一) sort函数用法 sort LISTsort BLOCK LISTsort SUBNAME LIST sort的用法有如上3种形式.它对LIST进行排序,并返回排序后的列表.假如忽略了SUBNA ...

  3. sort函数用法

    原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...

  4. (C++)STL排序函数sort和qsort的用法与区别

    主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...

  5. Sort函数的用法

    快速排序sort的用法:(适用于int float double char ...) 记得加头文件! 记得加头文件! 记得加头文件! 头文件: #include <algorithm>   ...

  6. C/C++ sort函数的用法

    sort函数的用法(#include<algorithm>) 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比 ...

  7. C++ sort函数用法

    参考文档:http://hi.baidu.com/posinfo/item/dc3e73584c535cc9d2e10c27 C++ sort函数用法 FROM:http://hi.baidu.com ...

  8. c++ sort函数的用法

    C++ sort函数用法 FROM:http://hi.baidu.com/blackdemonfish/blog/item/e2c1d655d702a45ed0090652%2Ehtml 最近算法作 ...

  9. 1806最大数 string和sort函数用法

    1.C++自带sort函数用法 sort函数有三个参数: (1)第一个是要排序的数组的起始地址 (2)第二个是结束的地址(最后一位要排序的地址) (3)第三个参数是排序的方法,可以是从大到小也可是从小 ...

随机推荐

  1. 通过分析java heap dump解决生产问题

    最近在生产环境遇到一个问题,正常情况下,ECS CPU始终保持在10%以下,内存也只占用40%左右,但是连续2天出现了CPU占用100%的情况,然后系统卡住.看阿里云的ECS监控,能看到CPU飙到了1 ...

  2. JVM内存模型一

    JVM定义了若干个程序执行期间使用的数据区域.这个区域里的一些数据在JVM启动的时候创建,在JVM退出的时候销毁.而其他的数据依赖于每一个线程,在线程创建时创建,在线程退出时销毁. 程序计数器 程序计 ...

  3. [C/C++] 输入函数getline(cin,str) 与cin.getline(str,int)区别

    cin.getline()函数是处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度. getline(cin,str ...

  4. 通过域名访问部署在服务器上的javaweb项目

    因为对域名访问什么也不了解,遇到问题就有种不知道从哪里下手的茫然,也就更不知道错在哪里,前前后后一共折腾了一天多,最后问了阿里客服才成功弄出来,因此记录一下. 关于服务器的购买.配置,及域名的备案解析 ...

  5. oracle exp导出加上过滤条件

    exp username/password@dbname   file='d:\hehe.dmp' tables=(%) query="""where UPDATE_DT ...

  6. 【spring学习笔记二】Bean

    ### bean的三种实例化方式: 1.构造 2.静态工厂 3.实例工厂 其中,工厂就是工厂的概念,工厂函数factor-method会返回她生产出来的产品类. 而构造初始化也可以选择初始化方式和销毁 ...

  7. 【转】64位Ubuntu 16.04搭建嵌入式交叉编译环境arm-linux-gcc过程图解

    64位Ubuntu 16.04搭建嵌入式交叉编译环境arm-linux-gcc过程图解,开发裸机环境之前需要先搭建其开发环境,毕竟工欲善其事必先利其器嘛.  安装步骤 1.准备工具安装目录 下载 ar ...

  8. P3320 [SDOI2015]寻宝游戏 解题报告

    P3320 [SDOI2015]寻宝游戏 题目描述 小B最近正在玩一个寻宝游戏,这个游戏的地图中有\(N\)个村庄和\(N-1\)条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以 ...

  9. 跟踪分析Linux内核的启动过程--20135334赵阳林

    解决ubuntu下make menuconfig错误问题 http://blog.sina.com.cn/s/blog_726684020100r1oo.html 安装好相关的软件之后,键入make ...

  10. 使用apt-mirror搭建debian本地仓库

    apt-mirror能够将官方镜像下载到本地,并保证目录结构与其一致,但是不能对镜像仓库进行修改.如果想要修改镜像仓库,需要使用reprepro. 1.安装apt-mirror # aptitude ...