Help Me with the Game(模拟法)

来源:互联网 发布:重庆快乐十分遗漏数据 编辑:程序博客网 时间:2024/05/17 03:22

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,e4Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6

Source

题解:找规律是poj2993的对调推算,运用结构体排序,先以K,Q,R,B,N,P顺序排,白色再按数字从小到大,黑色再按数字从大到小,然后黑白都按小字母从小到大
#include <iostream>#include <algorithm>using namespace std;struct node{   char x,y;//x是大写字母,y是第几列   int h,l;//h是第几行}w[100],b[100];char mp[100][100];bool cmp1(struct node a,struct node b)//白棋{    if(a.l!=b.l)        return a.l<b.l;    else if(a.h!=b.h)        return a.h<b.h;    else if(a.y!=b.y)    return a.y<b.y;}bool cmp2(struct node a,struct node b)//黑棋{    if(a.l!=b.l)        return a.l<b.l;    else if(a.h!=b.h)        return a.h>b.h;    else if(a.y!=b.y)    return a.y<b.y;}int main(){    int i,j;    for(i=0;i<17;i++)        for(j=0;j<33;j++)    {        cin>>mp[i][j];    }    int k=0,k1=0;    for(i=0;i<17;i++)    {        for(j=0;j<33;j++)        {            if(mp[i][j]=='K')            {               w[k].x='K';               w[k].y=((j+2)/4)-1+'a';//以poj2993题反推               w[k].h=((17-i)/2);               w[k].l=1;               k++;            }            if(mp[i][j]=='Q')            {               w[k].x='Q';               w[k].y=((j+2)/4)-1+'a';               w[k].h=((17-i)/2);               w[k].l=2;               k++;            }            if(mp[i][j]=='R')            {               w[k].x='R';               w[k].y=((j+2)/4)-1+'a';               w[k].h=((17-i)/2);               w[k].l=3;               k++;            }            if(mp[i][j]=='B')            {               w[k].x='B';               w[k].y=((j+2)/4)-1+'a';               w[k].h=((17-i)/2);               w[k].l=4;               k++;            }            if(mp[i][j]=='N')            {               w[k].x='N';               w[k].y=((j+2)/4)-1+'a';               w[k].h=((17-i)/2);               w[k].l=5;               k++;            }            if(mp[i][j]=='P')            {  w[k].x='#';//x位置没东西               w[k].y=((j+2)/4)-1+'a';               w[k].h=((17-i)/2);               w[k].l=6;               k++;            }            if(mp[i][j]=='k')            {               b[k1].x='K';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=7;               k1++;            }            if(mp[i][j]=='q')            {               b[k1].x='Q';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=8;               k1++;            }            if(mp[i][j]=='r')            {               b[k1].x='R';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=9;               k1++;            }            if(mp[i][j]=='b')            {               b[k1].x='B';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=10;               k1++;            }            if(mp[i][j]=='n')            {               b[k1].x='N';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=11;               k1++;            }            if(mp[i][j]=='p')            {  b[k1].x='#';               b[k1].y=((j+2)/4)-1+'a';               b[k1].h=((17-i)/2);               b[k1].l=12;               k1++;            }        }    }    sort(w,w+k,cmp1);    sort(b,b+k1,cmp2);    cout<<"White: ";    for(i=0;i<=k-1;i++)        {           if(w[i].x!='#')            cout<<w[i].x;           cout<<w[i].y<<w[i].h;           if(i!=k-1)            cout<<",";        }    cout<<endl;    cout<<"Black: ";    for(i=0;i<=k1-1;i++)        {           if(b[i].x!='#')            cout<<b[i].x;           cout<<b[i].y<<b[i].h;           if(i!=k1-1)            cout<<",";        }    cout<<endl;    return 0;}


0 0
原创粉丝点击