Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4098 | Accepted: 2586 |
Description
Input
Output
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
表示这题是不是考的英语......理解了题目意思以后就主要是排序了
题目的意思是给一个有棋子的棋盘,大写字母表示White棋,小写字母表示Black棋
然后从左到右的列为a-h,从下到上的行为1-8
White和Black最后输出的顺序都是 King("K"), Queens ("Q"), Rooks ("R"), Bishops ("B"), Knights ("N"), and pawns("P")
然后对于W棋,row小的优先,row一样的,列小的优先
对于B棋,row大的优先,row一样的,也是列小的优先
采取上述的排序策略分别对W棋和B棋进行排序,先根据行列排序,然后采用桶排序的思想,对K Q R B N P用vector过滤一遍,然后输出最后结果就行了
在读入棋盘的时候再找找跟数组的映射规律,将棋子和坐标有序地提取出来
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <stack> #include <algorithm> #include <vector> using namespace std; struct chess { int x, y; char ch; chess() {} chess(int x, int y, char ch) : x(x), y(y), ch(ch) {} }; int n; char ru[100], a[100]; int lenw, lenb; chess w[65], b[65]; vector<chess> vw[26], vb[26]; char orderw[6] = { 'K', 'Q', 'R', 'B', 'N', 'P' }; char orderb[6] = { 'k', 'q', 'r', 'b', 'n', 'p' }; //横坐标小的优先,然后纵坐标小的优先 bool cmpw(chess a, chess b) { if (a.x == b.x) { return a.y < b.y; } else { return a.x < b.x; } } //横坐标大的优先,然后纵坐标小的优先 bool cmpb(chess a, chess b) { if (a.x == b.x) { return a.y < b.y; } else { return a.x > b.x; } } int main() { lenw = lenb = 0; for (int i = 0; i < 26; i++) { vw[i].clear(); vb[i].clear(); } scanf("%s", ru); //第一行是棋盘边界,缓冲掉 for (int i = 0; i < 8; i++) { //八行 scanf("%s", a); //每行从0开始,棋子的位置是个等差数列 //an = 4n + 2 -> n = (an - 2) / 4, n为从0开始第几个棋子 for (int j = 2; j <= 30; j += 4) { if (a[j] == 'K' || a[j] == 'Q' || a[j] == 'R' || a[j] == 'B' || a[j] == 'N' || a[j] == 'P') { w[lenw++] = chess(8 - i, (j - 2) / 4, a[j]); } if (a[j] == 'k' || a[j] == 'q' || a[j] == 'r' || a[j] == 'b' || a[j] == 'n' || a[j] == 'p') { b[lenb++] = chess(8 - i, (j - 2) / 4, a[j]); } } scanf("%s", ru); //棋盘边界,缓冲掉 } //对白黑棋分别进行结构体二级排序 sort(w, w + lenw, cmpw); sort(b, b + lenb, cmpb); //根据行列排好以后,用桶排序的思想,根据字母用vector滤一遍 for (int i = 0; i < lenw; i++) { vw[w[i].ch - 'A'].push_back(w[i]); } for (int i = 0; i < lenb; i++) { vb[b[i].ch - 'a'].push_back(b[i]); } printf("White: "); bool fw = true; //标记是否是第一个输出的,用于配对"," //然后根据事先准备好的顺序输出,这是后每个vector[i]里面都是排好序的了 for (int i = 0; i < 6; i++) { if (i != 5) { for (vector<chess>::iterator j = vw[orderw[i] - 'A'].begin(); j != vw[orderw[i] - 'A'].end(); j++) { if (!fw) { printf(","); } if (fw) fw = false; printf("%c%c%d", j->ch, j->y + 'a', j->x); } } else { for (vector<chess>::iterator j = vw[orderw[i] - 'A'].begin(); j != vw[orderw[i] - 'A'].end(); j++) { if (!fw) { printf(","); } if (fw) fw = false; printf("%c%d", j->y + 'a', j->x); } } } puts(""); printf("Black: "); fw = true; for (int i = 0; i < 6; i++) { if (i != 5) { for (vector<chess>::iterator j = vb[orderb[i] - 'a'].begin(); j != vb[orderb[i] - 'a'].end(); j++) { if (!fw) { printf(","); } if (fw) fw = false; printf("%c%c%d", j->ch + 'A' - 'a', j->y + 'a', j->x); } } else { for (vector<chess>::iterator j = vb[orderb[i] - 'a'].begin(); j != vb[orderb[i] - 'a'].end(); j++) { if (!fw) { printf(","); } if (fw) fw = false; printf("%c%d", j->y + 'a', j->x); } } } puts(""); return 0; }