这道题里主要学习了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. 2012r2 以及 2012r2 withupdate 已经安装更新的差异

    0. 2012r2 不管带不带 update 1 他的版本号 都是 6.3.9600 如图示 2012r2的发布时间是 2013年 2012r2withupdate的发布时间是 2014年. 查看补丁 ...

  2. APP接口

    <?phpClass Response{ /*** 返回json数据* @param $code 状态码* @param $message 描述信息* @param $data 数据* @par ...

  3. (暂时弃坑)(半成品)ACM数论之旅18---反演定理 第二回 Mobius反演(莫比乌斯反演)((づ ̄3 ̄)づ天才第一步,雀。。。。)

    莫比乌斯反演也是反演定理的一种 既然我们已经学了二项式反演定理 那莫比乌斯反演定理与二项式反演定理一样,不求甚解,只求会用 莫比乌斯反演长下面这个样子(=・ω・=) d|n,表示n能够整除d,也就是d ...

  4. rpc 协议规范 之 rmi http webservice 和 一些框架

    RPC(Remote Procedure Call)是远程调用,是一种思想,也是一种协议规范.简单地说就是能使应用像调用本地方法一样的调用远程的过程或服务,可以应用在分布式服务.分布式计算.远程服务调 ...

  5. Python内部类,内部类调用外部类属性,方法

    一 Python中内部类 典型定义: class MyOuter: age=18 def __init__(self,name): self.name=name class MyInner: def ...

  6. 【刷题】BZOJ 2588 Spoj 10628. Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  7. PHP 字符串数组按照拼音排序的问题

    拼音排序的规则: 字符串包括特殊字符.数字.英文字符.中文字符等等,排序结果要求,特殊字符排在第一梯队,将其按照首个字符ascii码表进行排序,数字字符排在第二梯队,将首个字符数字按照数字大小排序,英 ...

  8. 【BZOJ5334】数学计算(线段树)

    [BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...

  9. 【hdu4057】 恨7不成妻

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 (题目链接) 题意 求区间${[a,b]}$中的某些数的平方和,这些数要满足1.不是7的倍数,2.不含有7 ...

  10. 【bzoj2594】 Wc2006—水管局长数据加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=2594 (题目链接) 题意 给出一个带边权的无向简单,要求维护两个操作,删除${u,v}$之间的连边: ...