Help Me with the Game
Time Limit: 1000MS |
Memory Limit: 65536K |
|
Total Submissions: 2157 |
Accepted: 1404 |
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
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
Source
解题报告:虽然做了2993 Emag eht htiw Em Pleh这道题,但是做这道题时还是没有思路,想了半小时,也没有想出,在网上找了一个解法,借鉴了一下他们的处理方法;
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
char map[17][34];
struct Position
{
char x, y;
};
Position K, Q, R[2], B[2], N[2], P[8];//储存白棋的信息
Position k, q, r[2], b[2], n[2], p[8];//储存黑棋的信息
int Findx(int col)//找到棋子的行坐标
{
return 8 - (col - 1) / 2;
}
int Findy(int row)//找出棋子的列坐标
{
return (row + 1) /4;
}
void Judge(char qizi, int col, int row)
{
int i;
char colx, rowy;
colx = Findx(col) + '0';
rowy = Findy(row) + 'a';
switch(qizi)
{
case 'K': K.x = colx; K.y = rowy; return;
case 'k': k.x = colx; k.y = rowy; return;
case 'Q': Q.x = colx; Q.y = rowy; return;
case 'q': q.x = colx; q.y = rowy; return;
case 'R':
{
for (i = 0; i <= 1; ++i)
{
if(R[i].x == 0)//没有存储信息时
{
R[i].x = colx; R[i].y = rowy; return;
}
}
}
case 'r':
{
for (i = 0; i <= 1; ++i)
{
if(r[i].x == 0)
{
r[i].x = colx; r[i].y = rowy; return;
}
}
}
case 'B':
{
for (i = 0; i <= 1; ++i)
{
if(B[i].x == 0)
{
B[i].x = colx; B[i].y = rowy; return;
}
}
}
case 'b':
{
for (i = 0; i <= 1; ++i)
{
if(b[i].x == 0)
{
b[i].x = colx; b[i].y = rowy; return;
}
}
}
case 'N':
{
for (i = 0; i <= 1; ++i)
{
if(N[i].x == 0)
{
N[i].x = colx; N[i].y = rowy; return;
}
}
}
case 'n':
{
for (i = 0; i <= 1; ++i)
{
if(n[i].x == 0)
{
n[i].x = colx; n[i].y = rowy; return;
}
}
}
case 'P':
{
for (i = 0; i <= 7; ++i)
{
if(P[i].x == 0)
{
P[i].x = colx; P[i].y = rowy; return;
}
}
}
case 'p':
{
for (i = 0; i <= 7; ++i)
{
if(p[i].x == 0)
{
p[i].x = colx; p[i].y = rowy; return;
}
}
}
}
}
int main()
{
int i, j;
for (i = 0; i < 17; ++i)
{
scanf("%s", map[i]);
}
for (i = 15; i > 0; i -= 2)//白棋的处理
{
for (j = 2; j <= 30; j += 4)
{
if (map[i][j] >= 'A' && map[i][j] <= 'Z')
{
Judge(map[i][j], i, j);
}
}
}
for (i = 1; i <= 15; i += 2)//黑棋的处理
{
for (j = 2; j <= 30; j += 4)
{
if (map[i][j] >= 'a' && map[i][j] <= 'z')
{
Judge(map[i][j], i, j);
}
}
}
printf("White: ");//输出白棋的信息
if (K.x)
{
printf("K%c%c", K.y, K.x);
}
if (Q.x)
{
printf(",Q%c%c", Q.y, Q.x);
}
for (i = 0; i <= 1; ++i)
{
if (R[i].x)
{
printf(",R%c%c", R[i].y, R[i].x);
}
}
for (i = 0; i <= 1; ++i)
{
if (B[i].x)
{
printf(",B%c%c", B[i].y, B[i].x);
}
}
for (i = 0; i <= 1; ++i)
{
if (N[i].x)
{
printf(",N%c%c", N[i].y, N[i].x);
}
}
for (i = 0; i <= 7; ++i)
{
if (P[i].x)
{
printf(",%c%c", P[i].y, P[i].x);
}
}
printf("\n");
printf("Black: ");//输出黑棋的信息
if (k.x)
{
printf("K%c%c", k.y, k.x);
}
if (q.x)
{
printf(",Q%c%c", q.y, q.x);
}
for (i = 0; i <= 1; ++i)
{
if (r[i].x)
{
printf(",R%c%c", r[i].y, r[i].x);
}
}
for (i = 0; i <= 1; ++i)
{
if (b[i].x)
{
printf(",B%c%c", b[i].y, b[i].x);
}
}
for (i = 0; i <= 1; ++i)
{
if (n[i].x)
{
printf(",N%c%c", n[i].y, n[i].x);
}
}
for (i = 0; i <= 7; ++i)
{
if (p[i].x)
{
printf(",%c%c", p[i].y, p[i].x);
}
}
printf("\n");
return 0;
}