题目要求
- 大写表示白色棋子
- 小写表示黑色棋子
- : 表示黑色方块
- . 表示白色方块
白色和黑色依次输出,输出的顺序有要求,相同棋子在棋盘同时出现,白色行数约低,横坐标越小表示越小,黑色棋子行数约高,横坐标越小表示越小。
一遍过题目
此处是我第一次在结构体里面用容器,注意的是自定义比较函数要定义小于函数,参数需要定义为const引用类型,另外sort函数属于库函数,需要包含头文件algorithm,与vector没有直接关系
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define TEST
using namespace std;
char line[50];
typedef struct POS
{
char x;
int y;
}Pos;
//定义小于函数
bool mycmp(const struct POS &x, const struct POS &y)
{
return (x.y < y.y || (x.y == y.y && x.x < y.x));
}
bool f_mycmp(const struct POS &x, const struct POS &y)
{
return (x.y > y.y || (x.y == y.y && x.x < y.x));
}
//play[0]代表白色玩家,play[1]代表黑色玩家
struct PLAYER
{
vector<Pos> king;
vector<Pos> queen;
vector<Pos> rook;
vector<Pos> bishop;
vector<Pos> knight;
vector<Pos> pawn;
}play[2];
void Prt(struct PLAYER &p)
{
//初始为0,表示第一个逗号“,”不打印
int start = 0;
for(int i = 0; i < p.king.size(); i++)
{
if(start) printf(",");
printf("K%c%d", p.king[i].x, p.king[i].y);
start = 1;
}
for(int i = 0; i < p.queen.size(); i++)
{
if(start) printf(",");
printf("Q%c%d", p.queen[i].x, p.queen[i].y);
start = 1;
}
for(int i = 0; i < p.rook.size(); i++)
{
if(start) printf(",");
printf("R%c%d", p.rook[i].x, p.rook[i].y);
start = 1;
}
for(int i = 0; i < p.bishop.size(); i++)
{
if(start) printf(",");
printf("B%c%d", p.bishop[i].x, p.bishop[i].y);
start = 1;
}
for(int i = 0; i < p.knight.size(); i++)
{
if(start) printf(",");
printf("N%c%d", p.knight[i].x, p.knight[i].y);
start = 1;
}
for(int i = 0; i < p.pawn.size(); i++)
{
if(start) printf(",");
printf("%c%d", p.pawn[i].x, p.pawn[i].y);
start = 1;
}
printf("\n");
}
int main()
{
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST
//读入第一行
scanf("%s", line);
for(int i = 8; i > 0; i--)
{
scanf("%s", line);
//这里每次需要加4
for(int j = 2; j < strlen(line); j += 4)
{
if(line[j] == '.' || line[j] == ':') continue;
Pos *temp = (Pos *)malloc(sizeof(Pos));
//计算横坐标
temp->x = (j-1)/4 + 'a';
temp->y = i;
switch(line[j])
{
//注意push_back的参数为Pos型,故需要用*号
case 'K': play[0].king.push_back(*temp);break;
case 'Q': play[0].queen.push_back(*temp);break;
case 'R': play[0].rook.push_back(*temp);break;
case 'B': play[0].bishop.push_back(*temp);break;
case 'N': play[0].knight.push_back(*temp);break;
case 'P': play[0].pawn.push_back(*temp);break;
case 'k': play[1].king.push_back(*temp);break;
case 'q': play[1].queen.push_back(*temp);break;
case 'r': play[1].rook.push_back(*temp);break;
case 'b': play[1].bishop.push_back(*temp);break;
case 'n': play[1].knight.push_back(*temp);break;
case 'p': play[1].pawn.push_back(*temp);break;
}
}
//吃掉分隔行
scanf("%s", line);
}
sort(play[0].king.begin(), play[0].king.end(), mycmp);
sort(play[0].queen.begin(), play[0].queen.end(), mycmp);
sort(play[0].rook.begin(), play[0].rook.end(), mycmp);
sort(play[0].bishop.begin(), play[0].bishop.end(), mycmp);
sort(play[0].knight.begin(), play[0].knight.end(), mycmp);
sort(play[0].pawn.begin(), play[0].pawn.end(), mycmp);
sort(play[1].king.begin(), play[1].king.end(), f_mycmp);
sort(play[1].queen.begin(), play[1].queen.end(), f_mycmp);
sort(play[1].rook.begin(), play[1].rook.end(), f_mycmp);
sort(play[1].bishop.begin(), play[1].bishop.end(), f_mycmp);
sort(play[1].knight.begin(), play[1].knight.end(), f_mycmp);
sort(play[1].pawn.begin(), play[1].pawn.end(), f_mycmp);
printf("White: ");
Prt(play[0]);
printf("Black: ");
Prt(play[1]);
return 0;
}