Help Me with the Game POJ - 2996

6 篇文章 1 订阅

Help Me with the Game POJ - 2996

题意:给你一个国际象棋的棋盘,输出黑棋和白棋的位置。
题目链接:https://vjudge.net/problem/POJ-2996
棋盘如下:
在这里插入图片描述
小写字母是黑色棋子,大写字母是白色棋子。
其中 K Q R B N P分别代表 K" (King), “Q” (Queen), “R” (Rook), “B” (Bishop), “N” (Knight), “P” (Pawn).类型的棋子。

按如下格式输出:
在这里插入图片描述
每一个棋子的描述为 [type] [column] [row],type为棋子类型,column是第几列,row是第几行。需要注意:列从左到右分别编号a,b,c…,行从下到上编号1,2,3…要求输出顺序K>Q>R>B>N>P。若棋子类型相同,对于白色,按row升序排列,若row也相同,按column升序排列。对于黑色,按row降序排列,若row相同,按column升序排列。

思路:用字符串数组存图,扫一遍记录即可。
重点:
1.棋子描述 [type] [column] [row]的结构体编写。
2.要求输出顺序K>Q>R>B>N>P,因此需要定义一个可以量化的指标。根据这个指标来排序。
3.找到棋盘上真实行列和输入字符串的行列对应关系。

具体见代码:

#include<iostream>
#include<map>
using namespace std;
map<char,int>t_to_int;//量化指标 
struct pos{// 位置描述
	int type_toint;
	char type;
	int x,y;
}p[107];
pos white[107];
pos black[107];      
string s[21];
string bs[21];
void print(char type,int yy,int xx){
	string s="";
	if(!(type=='P'||type=='p')){
		if(type>=97&&type<=122){
			type=char(int(type)-32);
		}
		s+=type;
	}
	s+=(char)((int)yy+96);
	s+=(xx+'0');
	cout<<s;
}
bool is_white(char c){
	if(c>=65&&c<=90)return true;
	return false;
}
bool is_black(char c){
	if(c>=97&&c<=122)return true;
	return false;
}
void swep(pos po[],int i,int j){
	//cout<<"chage!\n";
	pos p;
	p=po[i];
	po[i]=po[j];
	po[j]=p;
}
int main(){
	t_to_int['k']=6;//量化指标 K最大。
	t_to_int['q']=5;
	t_to_int['r']=4;
	t_to_int['b']=3;
	t_to_int['n']=2;
	t_to_int['p']=1;
	t_to_int['K']=6;
	t_to_int['Q']=5;
	t_to_int['R']=4;
	t_to_int['B']=3;
	t_to_int['N']=2;
	t_to_int['P']=1;
	int i;
	int cnt_w=0;
	int cnt_b=0;
	for(i=0;i<=16;i++){
		cin>>s[i];
		bs[i]=s[i];
	}
	for(i=0;i<=16;i++){
		s[i]=bs[16-i];
	}
	//cout<<"-----\n";
//	for(i=0;i<=16;i++)cout<<s[i]<<endl;
	int j;
	for(i=0;i<=16;i++){
		if(i%2==0)continue;
		for(j=0;j<s[i].size();j++){
			if(is_white(s[i][j])){
				cnt_w++;
				white[cnt_w].type=s[i][j];
				white[cnt_w].x=(i+1)/2;
				white[cnt_w].y=j/4+1;
				white[cnt_w].type_toint=t_to_int[s[i][j]];
			}
			else if(is_black(s[i][j])){
				cnt_b++;
				black[cnt_b].type=s[i][j];
				black[cnt_b].x=(i+1)/2;
				black[cnt_b].y=j/4+1;
				black[cnt_b].type_toint=t_to_int[s[i][j]];
			}
		}
	}
	//mysort();
	//sort white
	for(i=1;i<=cnt_w-1;i++){
		for(j=1;j<=cnt_w-i;j++){
			//j j+1
			if(white[j].type!=white[j+1].type){
				if(white[j].type_toint<white[j+1].type_toint)swep(white,j,j+1);
			}  
			else{
				if(white[j].x!=white[j+1].x){
					if(white[j].x>white[j+1].x)swep(white,j,j+1);
				}
				else{
					if(white[j].y>white[j+1].y)swep(white,j,j+1);
				}
			}   
		}
	}
	//sort black
	for(i=1;i<=cnt_b-1;i++){
		for(j=1;j<=cnt_b-i;j++){
			//j j+1
			if(black[j].type!=black[j+1].type){
				if(black[j].type_toint<black[j+1].type_toint)swep(black,j,j+1);
			}  
			else{
				if(black[j].x!=black[j+1].x){
					if(black[j].x<black[j+1].x)swep(black,j,j+1);
				}
				else{
					if(black[j].y>black[j+1].y)swep(black,j,j+1);
				}
			}   
		}
	}
	//white
	cout<<"White: ";
	for(i=1;i<=cnt_w;i++){
		print(white[i].type,white[i].y,white[i].x);
		if(i!=cnt_w)cout<<",";
		else cout<<"\n";
	}
	//black
	cout<<"Black: ";
	for(i=1;i<=cnt_b;i++){
		print(black[i].type,black[i].y,black[i].x);
		if(i!=cnt_b)cout<<",";
		else cout<<"\n";
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值