Tju_Oj_2312Help Me with the Game
Posted jeasoniscoding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
代码:
1 /* 2 * 2312_Help Me with the Game.cpp 3 * 4 * Created on: 2018年10月23日 5 * Author: Jeaosn 6 */ 7 8 #include <iostream> 9 #include <stdlib.h> 10 #include <stdio.h> 11 #include <string> 12 #include <cstring> 13 #include <algorithm> 14 #include <vector> 15 #include <map> 16 #include <sstream> 17 using namespace std; 18 19 map< char , int > mymap; 20 map< int , string > column; 21 char chess[40][40]; 22 vector<string> white_KQRBN; 23 vector<string> white_P; 24 vector<string> black_KQRBN; 25 vector<string> black_P; 26 27 string num2str(int i) 28 { 29 stringstream ss; 30 ss<<i; 31 return ss.str(); 32 } 33 34 bool cmp_white_KQRBN(string a ,string b){ 35 if(a[0] == b[0]){ 36 if(a[2] == b[2]) 37 return a[1] < b[1]; //如果第一位第三位相同,按照第二位升序 38 return a[2] < b[2]; //如果第一位相同,第三位升序排列 39 } 40 return mymap[a[0]] < mymap[b[0]]; //首先按照首位升序 41 } 42 43 bool cmp_black_KQRBN(string a ,string b){ 44 if(a[0] == b[0]){ 45 if(a[2] == b[2]) 46 return a[1] < b[1]; //如果第一位第三位相同,按照第二位升序 47 return a[2] > b[2]; //如果第一位相同,第三位升序排列 48 } 49 return mymap[a[0]] < mymap[b[0]]; //首先按照首位升序 50 } 51 52 bool cmp_white_P(string a ,string b){ 53 if(a[1] == b[1]){ 54 return a[0] < b[0]; //如果第一位相同,第三位升序排列 55 } 56 return a[1] < b[1]; //首先按照第二位升序 57 } 58 59 bool cmp_black_P(string a ,string b){ 60 if(a[1] == b[1]){ 61 return a[0] < b[0]; //如果第一位相同,第三位升序排列 62 } 63 return a[1] > b[1]; //首先按照第二位升序 64 } 65 66 int main() 67 { 68 mymap[‘K‘] = 1;mymap[‘Q‘] = 2;mymap[‘R‘] = 3;mymap[‘B‘] = 4;mymap[‘N‘] = 5; 69 column[1] = "a";column[2] = "b";column[3] = "c";column[4] = "d"; 70 column[5] = "e";column[6] = "f";column[7] = "g";column[8] = "h"; 71 72 for(int i = 17 ;i >= 1; i--) 73 for(int j = 1 ;j <= 33; j++) 74 cin >> chess[i][j]; 75 76 for(int i = 2 ;i <= 17; i+=2){ 77 for(int j = 3 ;j <= 33; j+=4){ 78 if(chess[i][j] == ‘K‘) 79 white_KQRBN.push_back( "K" + column[ (j/4)+1 ] + num2str(i/2) ); 80 if(chess[i][j] == ‘Q‘) 81 white_KQRBN.push_back( "Q" + column[ (j/4)+1 ] + num2str(i/2) ); 82 if(chess[i][j] == ‘R‘) 83 white_KQRBN.push_back( "R" + column[ (j/4)+1 ] + num2str(i/2) ); 84 if(chess[i][j] == ‘B‘) 85 white_KQRBN.push_back( "B" + column[ (j/4)+1 ] + num2str(i/2) ); 86 if(chess[i][j] == ‘N‘) 87 white_KQRBN.push_back( "N" + column[ (j/4)+1 ] + num2str(i/2) ); 88 if(chess[i][j] == ‘P‘) 89 white_P.push_back( column[ (j/4)+1 ] + num2str(i/2) ); 90 91 if(chess[i][j] == ‘k‘) 92 black_KQRBN.push_back( "K" + column[ (j/4)+1 ] + num2str(i/2) ); 93 if(chess[i][j] == ‘q‘) 94 black_KQRBN.push_back( "Q" + column[ (j/4)+1 ] + num2str(i/2) ); 95 if(chess[i][j] == ‘r‘) 96 black_KQRBN.push_back( "R" + column[ (j/4)+1 ] + num2str(i/2) ); 97 if(chess[i][j] == ‘b‘) 98 black_KQRBN.push_back( "B" + column[ (j/4)+1 ] + num2str(i/2) ); 99 if(chess[i][j] == ‘n‘) 100 black_KQRBN.push_back( "N" + column[ (j/4)+1 ] + num2str(i/2) ); 101 if(chess[i][j] == ‘p‘) 102 black_P.push_back( column[ (j/4)+1 ] + num2str(i/2) ); 103 } 104 } 105 sort(white_KQRBN.begin() , white_KQRBN.end() ,cmp_white_KQRBN); 106 sort(black_KQRBN.begin() , black_KQRBN.end() ,cmp_black_KQRBN); 107 sort(white_P.begin() , white_P.end() ,cmp_white_P); 108 sort(black_P.begin() , black_P.end() ,cmp_black_P); 109 110 cout << "White: "; 111 for(int i = 0;i < white_KQRBN.size();i++){ 112 cout << white_KQRBN[i] << ","; 113 } 114 for(int i = 0;i < white_P.size();i++){ 115 if(i == (white_P.size() - 1)) 116 cout << white_P[i] << endl; 117 else 118 cout << white_P[i] << ","; 119 } 120 121 cout << "Black: "; 122 for(int i = 0;i < black_KQRBN.size();i++){ 123 cout << black_KQRBN[i] << ","; 124 } 125 for(int i = 0;i < black_P.size();i++){ 126 if(i == (black_P.size() - 1) ) 127 cout << black_P[i] << endl; 128 else 129 cout << black_P[i] << ","; 130 } 131 return 0; 132 }
这道题里主要学习了sort函数、sort的cmp函数写法、C++的map用法(其实和数组一样)
以上是关于Tju_Oj_2312Help Me with the Game的主要内容,如果未能解决你的问题,请参考以下文章
POJ-2996 Help Me with the Game---模拟棋子