兩道很有意思的題目POJ 2996,POJ2993

POJ 2996,POJ2993

Help Me with the Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5041   Accepted: 3148

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


Emag eht htiw Em Pleh
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3767   Accepted: 2442

Description

This problem is a reverse case of the problem 2996. You are given the output of the problem H and your task is to find the corresponding input.

Input

according to output of problem 2996.

Output

according to input of problem 2996.

Sample Input

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

Sample Output

+---+---+---+---+---+---+---+---+
|.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.|
+---+---+---+---+---+---+---+---+

這兩道題一個是給棋子序列求出棋子擺放,一個是給出棋子擺放求出棋子序列。相當nice的一道題。

代碼1:給出序列求棋盤

這裏比較複雜的地方就是0到32的i和0到8的位置的轉換,也就是個除以4。

讀入數據,存入vector,同時標記棋盤位置,1表示黑子,2表示白子,0表示沒子。

最後輸出棋盤即可。

#include <cstring>
#include <iostream>
#include<algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int wei[] = {2,6,10,14,18,22,26,30};
int flag[9][9];

struct chs{
    int x,y;
    char name;
    chs(){}
    chs(int o,int a,char b,char c)
    {
        x=16-a*2+1;
        y=wei[b-'a'];
        if(o==1)
            name=c;
        else
            name = c-'A'+'a';
        flag[x/2][y/4]=o;
    }
};
vector<chs> blac;
vector<chs> whi;
const int len  = 8;
char grond[33+2][18+2];
int bn=0,wn=0;
bool cmp(chs a,chs b){
    if(a.x!=b.x)
        return a.x>b.x;
    else return a.y>b.y;
}
void init(){
    wn=bn=0;
    memset(flag,0,sizeof(flag));
    scanf("White: ");
    char c;
    while(1){
        scanf("%c",&c);
        if(c>'A'&&c<'Z')
        {
            char b;int a;
            wn++;
            scanf("%c%d",&b,&a);
            whi.push_back(chs(1,a,b,c));
        }
        else if(c>='a'&&c<'z')
        {
            int a;
            scanf("%d",&a);
            whi.push_back(chs(1,a,c,'P'));
            wn++;
        }
        else if(c==',')
        continue;
        else
            break;
    }
    scanf("Black: ");//這段是重複上面的代碼,最好不要這麼寫太容易出錯了,本想這樣寫起來快點但是反而調試了我很久。
    while(1){
        scanf("%c",&c);
        if(c>'A'&&c<'Z')
        {
            char b;int a;
            scanf("%c%d",&b,&a);
            bn++;
            blac.push_back(chs(2,a,b,c));
        }
        else if(c>='a'&&c<'z')
        {
            int a;
            scanf("%d",&a);
            blac.push_back(chs(2,a,c,'P'));
            bn++;
        }
        else if(c==',')
        continue;
        else
            break;
    }
}
void gg(int i,int j){
    char c;
    if((i/2)%2==0){
    if((j/4)%2) c=':';
    else c='.';
    }
    else
    {
        if((j/4)%2) c='.';
    else c=':';
    }
    cout<<c;
    if(flag[i/2][j/4]==1){
        cout<<whi[wn-1].name;
        whi.pop_back();
        wn--;
    }
    else if(flag[i/2][j/4]==2){
        cout<<blac[bn-1].name;
        blac.pop_back();
        bn--;
    }
    else
        cout<<c;
    cout<<c;
}
int main(){
    init();
    sort(whi.begin(),whi.end(),cmp);
    sort(blac.begin(),blac.end(),cmp);
    for(int i = 0;i<=16;i++){
        if(i%2)
        {
            int j = 0;
            while(j<33){
                cout<<'|';
                if(j==32) break;
                j++;
                gg(i,j);
                j+=3;
            }
        }
        else
            cout<<"+---+---+---+---+---+---+---+---+";
        cout<<endl;
    }
    return 0;
}



代碼2:給出棋盤求序列

其中運用了一個映射,表示優先級,數字越小優先級越高。序列的存儲我用的是數組,其實可以考慮一下優先隊列,但是之前網絡賽的時候有一道模擬題模擬鏈表,我當時用了各種數據結構卻還是沒AC,後來看到網上的答案直接用幾個數組就搞定了,從此對STL產生了極大的抗拒。能用數組寫的就不用什麼列表隊列了。結果邏輯思維瘋狂提高。扯遠了。

這裏有兩個cmp函數,因爲黑子和白子的輸出優先序列不同。

首先遍歷一遍整個棋盤,把遇到的棋子存入數組,對數組進行排序,輸出,就這麼簡單。

#include <cstring>
#include <iostream>
#include<algorithm>
#include <cstdio>
#include <map>
using namespace std;

struct chs{
    int x,y;
    char name;
    chs(){}
    chs(int a,int b,char c)
    {
        x=a;y=b;name=c;
    }
};
map<char,int>  mp;
chs blac[16];
chs whi[16];
const int len  = 8;
char grond[33+2][18+2];
int wei[] = {2,5,9,13,17,21,25,29};
int bn=0,wn=0;
void init(){
    mp['K']=0;mp['Q']=1;mp['R']=2;mp['B']=3;
    mp['N']=4;mp['P']=5;mp['k']=0;mp['q']=1;
    mp['r']=2;mp['b']=3;mp['n']=4;mp['p']=5;
}
bool cmp1(chs a,chs b){
    if(a.name!=b.name)
    return mp[a.name]<mp[b.name];
    else if(a.x!=b.x)
    return a.x>b.x;
    else
        return a.y<b.y;
}
bool cmp2(chs a,chs b){
    if(a.name!=b.name)
    return mp[a.name]<mp[b.name];
    else if(a.x!=b.x)
    return a.x<b.x;
    else
        return a.y<b.y;
}
void deal(int i,int j){
    if(grond[i][j]>='a' && grond[i][j]<='z')
        blac[bn++]=chs(i/2,j/4,grond[i][j]);
    else if(grond[i][j]>='A' && grond[i][j]<='Z')
        whi[wn++]=chs(i/2,j/4,grond[i][j]);
}
int main(){
    init();
    for(int i = 16;i>=0;i--){
        if(i%2)
        {
            for(int j=0;j<33;j++){
                cin>>grond[i][j];
                deal(i,j);
            }
        }
        else
            for(int j=0;j<33;j++)
                cin>>grond[i][j];
    }
    sort(whi,whi+wn,cmp2);
    sort(blac,blac+bn,cmp1);
    printf("White: %c%c%d",whi[0].name,whi[0].y+'a',whi[0].x+1);
    for(int i=1;i<wn;i++){
        if(whi[i].name=='P')
        printf(",%c%d",whi[i].y+'a',whi[i].x+1);
        else
        printf(",%c%c%d",whi[i].name,whi[i].y+'a',whi[i].x+1);
    }
    printf("\nBlack: %c%c%d",blac[0].name-'a'+'A',blac[0].y+'a',blac[0].x+1);
    for(int i=1;i<wn;i++){
        if(blac[i].name=='p')
        printf(",%c%d",blac[i].y+'a',blac[i].x+1);
        else
        printf(",%c%c%d",blac[i].name-'a'+'A',blac[i].y+'a',blac[i].x+1);
    }
    cout<<endl;
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章