POJ2993&&POJ2996

来源:互联网 发布:pc端护眼软件 编辑:程序博客网 时间:2024/05/01 01:39
Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4413 Accepted: 2785

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

CTU Open 2005

题目大意:

一个8*8的棋盘,上面有白棋和黑棋,分别用大写字母和小写字母表示,类似于数学上的直角坐标系,左下角为原点,横坐标为a b c d e f g h(对应12345678),纵坐标为12345678,  白棋黑棋各有6种。

输出要求:白棋从纵坐标小的开始输出(即从最底下开始输出),若在同一行,按照列序输出,若棋子类型为P,则只输出坐标;

                 黑棋从纵坐标大的开始输出(即从最顶上开始输出),若在同一行,按照列序输出,若棋子类型为p,则只输出坐标;

思路:

把棋子储存在与棋盘相对应的二维数组中,然后按照要求遍历就可以。。。。


/*POJ 2996*/
#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <cstring>using namespace std;char white[10] = {'K','Q','R','B','N','P'};      //初始化白棋遍历顺序char black[10] = {'k','q','r','b','n','p'};        //初始化黑棋遍历顺序int main(){    int mp[20][20];    memset(mp,0,sizeof(mp));    char s[100];    gets(s);                           //读入棋盘最顶上边界,不进行处理    for( int i = 7;i >= 0;i-- )    {        getchar();              //读入第一个竖线;        for( int j = 0;j <= 7;j++ )        {            char a, b, c, d;            cin>>a>>b>>c>>d;  //b表示棋子种类            mp[i][j] = b;        }        getchar();   //读入末尾的回车        gets(s);       //读入边界,不进行处理    }    cout<<"White: ";    int flag = 0;    //标志变量,控制逗号    for( int k = 0;k < 6;k++ )          //控制输出顺序    {        for( int i = 0;i <= 7;i++ )       //从最底下遍历        {            for( int j = 0;j <= 7;j++ )            {                if( mp[i][j]==white[k] )                {                    if( flag )   //首次输出不输出逗号                        cout<<",";                    if( white[k]!='P' )     //若棋子种类不为P,输出棋子种类,否则只输出坐标                        printf("%c",white[k]);                    printf("%c%c",'a'+j,i+'0'+1);    //将横坐标的数字转化为字母坐标输出                    flag = 1;      //修改标志变量,表示不是首次输出                }            }        }    }    cout<<endl;    cout<<"Black: ";    flag = 0;    for( int k = 0;k < 6;k++ )    {        for( int i = 7;i >=0;i-- )     //从顶端遍历        {            for( int j = 0;j <= 7;j++ )            {                if( mp[i][j]==black[k] )                {                    if( flag )                        cout<<",";                    if( black[k]!='p' )                        printf("%c",black[k]-'a'+'A');    //将小写转化为大写                    printf("%c%c",'a'+j,i+'0'+1);                    flag = 1;                }            }        }    }    return 0;}


POJ2993为2996的逆过程,涉及到信息的提取与转化。。。



/*POJ 2993*/
#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <cstring>using namespace std;int main(){    int mp[10][10];      //存放棋子的ASCII码值    memset(mp,0,sizeof(mp));   //初始化  对于后面的遍历会用到    char s1[110], s2[110];    gets(s1);       //读入白棋序列    gets(s2);       //读入黑棋序列    int len1 = strlen(s1);    int len2 = strlen(s2);    for( int i = 0; i < len1; i++ )    {        if( s1[i]>='1'&&s1[i]<='9' )      //数字为棋子信息的终止标志        {            if( s1[i-2]!=',' )           //表示棋子类型不是P                mp[s1[i]-'1'][s1[i-1]-'a'] = s1[i-2];        //白棋不需要进行大小写转化            else                mp[s1[i]-'1'][s1[i-1]-'a'] = 'P';        }    }    for( int i = 0; i < len2; i++ )    {        if( s2[i]>='1'&&s2[i]<='9' )        {            if( s2[i-2]!=',' )                mp[s2[i]-'1'][s2[i-1]-'a'] = s2[i-2] - 'A' + 'a';        //黑棋需要进行大小写转化            else                mp[s2[i]-'1'][s2[i-1]-'a'] = 'p';        }    }    cout<<"+---+---+---+---+---+---+---+---+"<<endl;    for( int i = 7;i >= 0;i-- )    {        for( int j = 0;j <= 7;j++ )        {            cout<<"|";            if( (i+j)%2==1 )   //控制输出  .  还是    :            {                cout<<".";                if( mp[i][j] )      //若存放数据  则输出,  否则输出符号                    printf("%c",mp[i][j]);                else cout<<".";                cout<<".";            }            else            {                cout<<":";                if( mp[i][j] )                    printf("%c",mp[i][j]);                else cout<<":";                cout<<":";            }        }        cout<<"|"<<endl;        cout<<"+---+---+---+---+---+---+---+---+"<<endl;    }    return 0;}





0 0
原创粉丝点击