POJ 2996-Help Me with the Game(模拟-描述棋盘中KQRBNP的位置)

来源:互联网 发布:iris数据集 编辑:程序博客网 时间:2024/06/14 06:16
Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4565 Accepted: 2887

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

题目意思:

给出一个棋盘,大写字母是白色棋子,小写字母是黑色棋子。

先输出白棋再输出黑棋,格式为:类型+列数+行数。

按照KQRBNP的顺序输出棋子的位置,其中‘P’类型不输出‘P’。

在输入中出现相同类型的情况下,如果棋子是白色,较小行号优先,如果是黑色,则较大行号优先。

如果相同类型出现在同一行中,则较小列号的必须首先输出。

列的范围是a~h,其中“a”是输入中最左边的列;行的范围是1~8,其中8是输入中的第一行。

解题思路:

模拟题,思路还是挺重要的,我一开始想着对结构体按照规则先排好序再输出,后来发现排序难以实现,改成手动规定顺序就直接1A了~~\(≧▽≦)/~啦啦啦
我为了方便满足升序降序的不同要求,使用了二维结构体数组分别存储每行每列的棋子情况。

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct Node{    int x,y;//行、列    char c;} ;void col(int i)//输出列{    if(i==2) cout<<"a";    else if(i==6) cout<<"b";    else if(i==10) cout<<"c";    else if(i==14) cout<<"d";    else if(i==18) cout<<"e";    else if(i==22) cout<<"f";    else if(i==26) cout<<"g";    else if(i==30) cout<<"h";}void row(int i)//输出行{    if(i==15) cout<<"1";    else if(i==13) cout<<"2";    else if(i==11) cout<<"3";    else if(i==9) cout<<"4";    else if(i==7) cout<<"5";    else if(i==5) cout<<"6";    else if(i==3) cout<<"7";    else if(i==1) cout<<"8";}char ma[17][33];int main(){#ifdef ONLINE_JUDGE#else    freopen("F:/cb/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    Node q[9][9];//棋子位置    int cnt=0;//q中的列数    int wc=0,bc=0;//白棋和黑棋的数量    int no=0;//q中的行数    for(int i=0; i<9; ++i)//初始化置0        for(int j=0; j<9; ++j)            q[i][j].c='0';    for(int i=0; i<17; ++i)//输入原棋盘    {        if(i%2==1) ++no,cnt=0;        for(int j=0; j<33; ++j)        {            cin>>ma[i][j];            if((ma[i][j]>='A'&&ma[i][j]<='Z')||(ma[i][j]>='a'&&ma[i][j]<='z'))//字母才是棋子            {                q[no][cnt].x=i,q[no][cnt].y=j;                q[no][cnt++].c=ma[i][j];                //cout<<ma[i][j]<<" ";                if(ma[i][j]=='P') ++wc;                else if(ma[i][j]=='p') ++bc;            }        }    }    ++cnt;    /*for(int j=8; j>=0; --j)//输出只有棋子的棋盘    {        for(int i=0; i<8; ++i)            cout<<q[j][i].c<<" ";        cout<<endl;    }    cout<<no<<" "<<cnt<<endl;*/    cout<<"White: ";    for(int j=no; j>=0; --j)    {        for(int i=0; i<cnt; ++i)        {            if(q[j][i].c=='K')            {                cout<<"K";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }        }    }    for(int j=no; j>=0; --j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='Q')            {                cout<<"Q";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=no; j>=0; --j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='R')            {                cout<<"R";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=no; j>=0; --j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='B')            {                cout<<"B";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=no; j>=0; --j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='N')            {                cout<<"N";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    int t=0,tt;    for(int j=no; j>=0; --j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='P')            {                ++t;                if(t==wc)                {                    t=i;                    tt=j;                    break;                }                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    col(q[tt][t].y);    row(q[tt][t].x);    cout<<endl;    cout<<"Black: ";    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='k')            {                cout<<"K";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='q')            {                cout<<"Q";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='r')            {                cout<<"R";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='b')            {                cout<<"B";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='n')            {                cout<<"N";                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    t=0;    for(int j=0; j<cnt; ++j)        for(int i=0; i<cnt; ++i)            if(q[j][i].c=='p')            {                ++t;                if(t==wc)                {                    t=i;                    tt=j;                    break;                }                col(q[j][i].y);                row(q[j][i].x);                cout<<",";            }    col(q[tt][t].y);    row(q[tt][t].x);    cout<<endl;    return 0;}/*+---+---+---+---+---+---+---+---+|.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.|+---+---+---+---+---+---+---+---+*/


0 0
原创粉丝点击