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

kewlgrl發表於2017-01-20
Help Me with the Game
Time Limit: 1000MS   Memory Limit: 65536K
Total 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,e4
Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6

Source


題目意思:

給出一個棋盤,大寫字母是白色棋子,小寫字母是黑色棋子。

先輸出白棋再輸出黑棋,格式為:型別+列數+行數。

按照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.|
+---+---+---+---+---+---+---+---+
*/


相關文章