这道题里主要学习了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. Vue.js——60分钟webpack项目模板快速入门

    概述 browserify是一个 CommonJS风格的模块管理和打包工具,上一篇我们简单地介绍了Vue.js官方基于browserify构筑的一套开发模板.webpack提供了和browserify ...

  2. Asp.net MVC area

    妈的,今天去携程面试,技术面了三轮,竟然让我走了,没有然后了,你不要老子,干嘛还面那么多轮,害的老子一上午的时间没了,气死我了. 好了,总结下面试中的问题吧, 1.GC 2.设计模式 3.做过的项目的 ...

  3. SQLSERVER 使用XP开头的系统默认存储过程

    1. 根据官网上面的内容进行执行命令 EXEC xp_cmdshell 'dir *.exe'; 但是会报错 消息 ,级别 ,状态 ,过程 xp_cmdshell,行 [批起始行 ] SQL Serv ...

  4. (转)web性能优化

    前端是庞大的,包括 HTML. CSS. Javascript.Image .Flash等等各种各样的资源.前端优化是复杂的,针对方方面面的资源都有不同的方式.那么,前端优化的目的是什么 ? 1. 从 ...

  5. GPU并行编程小结

    http://peghoty.blog.163.com/blog/static/493464092013016113254852/ http://blog.csdn.net/augusdi/artic ...

  6. Longest Word in Dictionary through Deleting - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...

  7. Linux及安全实践四——ELF文件格式分析

    Linux及安全实践四——ELF文件格式分析 一.ELF文件格式概述 1. ELF:是一种对象文件的格式,用于定义不同类型的对象文件中都放了什么东西.以及都以什么样的格式去放这些东西. 二.分析一个E ...

  8. Xpack集成LDAP

    支持两种配置方式: The ldap realm supports two modes of operation, a user search mode and a mode with specifi ...

  9. shell 中的流程控制关键字

    if...else if [ $1x == "ab"x ]; then echo "you had enter ab" elif [ $1x == " ...

  10. Chapter12(动态内存)--C++Prime笔记

    1.分配再静态或栈内存中的对象由编译器自动创建销毁. 2.C++中动态内存的管理是通过 new:前者为对象非配空间并返回一个指向该对象的指针. delete:接受一个动态对象的指针,摧毁该对象,并释放 ...