两道很有意思的题目POJ 2996,POJ2993

来源:互联网 发布:淘宝针织衫 编辑:程序博客网 时间:2024/05/29 08:21

POJ 2996,POJ2993

Help Me with the Game
Time Limit: 1000MS Memory Limit: 65536KTotal 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,e4Black: 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: 65536KTotal 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,e4Black: 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;}



原创粉丝点击