sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game
这道题里主要学习了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的更多相关文章
- Java中Collections类的排序sort函数两种用法
java中的Colletions类主要实现列表List的排序功能.根据函数参数的传递,具体的排序可以分为 : 1. 自然排序(natural ordering). 函数原型:sort(List< ...
- Perl Sort函数用法总结和使用实例
一) sort函数用法 sort LISTsort BLOCK LISTsort SUBNAME LIST sort的用法有如上3种形式.它对LIST进行排序,并返回排序后的列表.假如忽略了SUBNA ...
- sort函数用法
原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...
- (C++)STL排序函数sort和qsort的用法与区别
主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...
- Sort函数的用法
快速排序sort的用法:(适用于int float double char ...) 记得加头文件! 记得加头文件! 记得加头文件! 头文件: #include <algorithm> ...
- C/C++ sort函数的用法
sort函数的用法(#include<algorithm>) 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比 ...
- C++ sort函数用法
参考文档:http://hi.baidu.com/posinfo/item/dc3e73584c535cc9d2e10c27 C++ sort函数用法 FROM:http://hi.baidu.com ...
- c++ sort函数的用法
C++ sort函数用法 FROM:http://hi.baidu.com/blackdemonfish/blog/item/e2c1d655d702a45ed0090652%2Ehtml 最近算法作 ...
- 1806最大数 string和sort函数用法
1.C++自带sort函数用法 sort函数有三个参数: (1)第一个是要排序的数组的起始地址 (2)第二个是结束的地址(最后一位要排序的地址) (3)第三个参数是排序的方法,可以是从大到小也可是从小 ...
随机推荐
- TADOConnection.Close - connection still active on MS-SQL server
I have several Delphi programs (XE3), that use a TADOConnection to connect to a MS-SQL Server. I rec ...
- 【Web Shell】- 技术剖析中国菜刀 - Part II
在第一部分,简单描述了中国菜刀的基本功能.本文我将剖析中国菜刀的平台多功能性.传输机制.交互模式和检测.我希望通过我的讲解,您能够根据您的环境检测出并清除它. 平台 那么中国菜刀可以在哪些平台上运行? ...
- WDS迁移注意事项
先说背景:公司使用WDS来部署操作系统,目前DHCP和WDS都安装在同一台服务器上,但是此服务器已过保,所以筹划迁移,将WDS和DHCP分别迁移到两台服务器上.迁移计划是保持WDS暂时不动,DHCP先 ...
- Windows下 OpenSSL的安装与简单使用
1. openssl的最新版本 最新版本是 openssl1.1.1 官方地址 https://www.openssl.org/source/ TLS1.3的协议(RFC8446)在2018.8.12 ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- 【转】Latex编译报错后中断编译并改正,然后重复出现不明原因报错的解决方法
转自:https://www.douban.com/note/419828344/ 目录: 一.问题描述 二.测试情况(可以跳过,直接看建议) 三.建议 四.参考资料 正文: 问题描述: 错漏某个符号 ...
- 学习Spring Boot:(三)配置文件
前言 Spring Boot使用习惯优于配置(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来. 正文 使用配置文件注入属性 Spring Boo ...
- bzoj2300【HAOI2011】防线修建
题目描述 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还犹豫不决,到底该把哪些城市作为保护对象呢?又由于 ...
- Golang构建HTTP服务(一)--- net/http库源码笔记
搭建一个简单的Go Web服务器 Go语言标准库 - net/http 在学习Go语言有一个很好的起点,Go语言官方文档很详细,今天我们学习的Go Web服务器的搭建就需要用到Go语言官方提供的标准库 ...
- 3.CentOS的一些小笔记
1.一般来说,主文件夹都在/home下面,比如登陆的账户为LyndonMario,则我的主文件夹为 /home/LyndonMario. 2.ctrl+space可以调出输入法. 3.CentOS中的 ...