poj2996 Help Me with the Game 结构体多级排序 桶排序

Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4098 Accepted: 2586

Description

Your task is to read a picture of a chessboard position and print it in the chess notation.

Input

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

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


表示这题是不是考的英语......理解了题目意思以后就主要是排序了

题目的意思是给一个有棋子的棋盘,大写字母表示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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值