模拟,水
注意数组大小;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using std::map;
using std::memset;
using std::sort;
char charmap[18][34];
char chess[8][8];
map<char, int> prior;
char get(int row,int col)
{
row = 7 - row;
row = 2 * row + 1;
col = 4*col + 2;
return charmap[row][col];
}
struct point
{
char type;
int i,j;
}White[100],Black[100];
int nw,nb;
bool cmp1(const point & a, const point &b)
{
if(prior[a.type] == prior[b.type])
{
if(a.i == b.i)
{
return a.j < b.j;
}
return a.i < b.i;
}
else
{
return prior[a.type] < prior[b.type];
}
}
bool cmp2(const point & a, const point &b)
{
if(prior[a.type] == prior[b.type])
{
if(a.i == b.i)
{
return a.j < b.j;
}
return a.i > b.i;
}
else
{
return prior[a.type] < prior[b.type];
}
}
int main()
{
prior['K'] = 0;
prior['k'] = 0;
prior['Q'] = 1;
prior['q'] = 1;
prior['R'] = 2;
prior['r'] = 2;
prior['B'] = 3;
prior['b'] = 3;
prior['N'] = 4;
prior['n'] = 4;
prior['P'] = 5;
prior['p'] = 5;
for(int i = 0 ; i < 17; i++)
{
scanf("%s", charmap[i]);
}
for(int i = 0; i < 8; i++)
{
for(int j = 0 ; j < 8; j++)
{
if(get(i,j) == '.'|| get(i,j) == ':')
{
chess[i][j] = '0';
}
else
{
chess[i][j] = get(i,j);
if('a' <= chess[i][j] && chess[i][j] <= 'z')
{
Black[nb].type = chess[i][j];
Black[nb].i = i;
Black[nb++].j = j;
}
else
{
White[nw].type = chess[i][j];
White[nw].i = i;
White[nw++].j = j;
}
}
}
}
sort(White, White+nw, cmp1);
sort(Black, Black+nb, cmp2);
printf("White: ");
for(int i = 0 ; i < nw; i++)
{
if(White[i].type != 'P')
{
printf("%c%c%d",White[i].type, White[i].j+'a', White[i].i+1);
}
else
{
printf("%c%d", White[i].j+'a', White[i].i+1);
}
if(i != nw-1)
{
printf(",");
}
}
printf("\n");
printf("Black: ");
for(int i = 0 ; i < nw; i++)
{
if(Black[i].type != 'p')
{
printf("%c%c%d",Black[i].type+'A'-'a', Black[i].j+'a', Black[i].i+1);
}
else
{
printf("%c%d", Black[i].j+'a', Black[i].i+1);
}
if(i != nw-1)
{
printf(",");
}
}
printf("\n");
}