这道题里主要学习了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. Jquery插件收集【m了慢慢学】

    1. Simple Effects for Drop-Down Lists 一个jQuery插件用于将普通的select控件转成一个带有一些简单扩展效果的下拉列表. 2. X-editable 这个插 ...

  2. 用JavaScript添加选择按钮的背景颜色和juqery添加选择按钮的背景色

    在项目开发中经常遇到要选择的按钮,选择完之后被选择的按钮的背景色会发生变化,表示被选择 样式图如下: 每点击一个数字,相应的背景色变为蓝色,其他的依旧是白色,先用JavaScript实现 html代码 ...

  3. <script>document.write(location.href)</script>

    <script>document.write(location.href)</script> 什么意思?

  4. POJ1637_Sightseeing tour

    给一个联通图,有的是单向边,有的是双向边,问是否存在欧拉回路. 乍一看毫无思路,可以这样来搞,对于每条无向边,我们随便指定一个方向,看看是否能够做到所有点的度数之和为偶数. 接下来,对于我们指定的边, ...

  5. ACdream原创群赛__15

    这场感觉题目确实还算可以,不过,说好的每题10s效果上却不理想.这个时限还算比较紧.因为时间不是按绝对的多出几秒来计算,而是几倍来计算的. 比赛做的不好,后面又去做了一下. A:典型的数位DP,一直坑 ...

  6. javascript和php使用ajax通信传递JSON

    JS和PHP直接通信常用ajax完成,以实现js上UI的动态变化.通信使用JSON或者XML传递数据.下面详细描述两者直接JSON字符串的传递. 下面案例是要传递这样的json数据: { " ...

  7. 【Cf #291 B】R2D2 and Droid Army(二分,线段树)

    因为题目中要求使连续死亡的机器人最多,令人联想到二分答案. 考虑如何检验这之中是否存在一段连续的长度为md的区间,其中花最多k步使得它们都死亡. 这个条件等价于区间中m个最大值的和不超过k. 枚举起点 ...

  8. phpredis -- redis_cluster

    https://github.com/phpredis/phpredis/tree/feature/redis_cluster https://github.com/phpredis/phpredis

  9. bzoj3473: 字符串 && bzoj3277串

    3473: 字符串 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 121  Solved: 53[Submit][Status][Discuss] D ...

  10. 《剑指offer》— JavaScript(22)从上往下打印二叉树

    从上往下打印二叉树 题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路 借助两个辅助队列,一个用来存放结点,一个用来存放结点值: 先将根节点加入到队列中,然后遍历队列中的元素,遍历 ...