zoukankan      html  css  js  c++  java
  • Help Me with the Game

    Help Me with the Game

    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
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<cstdlib>
      5 using namespace std;
      6 struct node
      7 {
      8     int hang;
      9     int lie;
     10     int data;
     11     int jb;
     12 } white[33],black[33];
     13 int sw(char p)//只是为了让级别好记,好排序
     14 {
     15     if(p=='K' || p=='k') return 1;
     16     if(p=='Q' || p=='q') return 2;
     17     if(p=='R' || p=='r') return 3;
     18     if(p=='B' || p=='b') return 4;
     19     if(p=='N' || p=='n') return 5;
     20     if(p=='P' || p=='p') return 6;
     21     return 0;
     22 }
     23 int cmpw(const void*a,const void *b)//这个排序很是坑爹,最初一直没想这样写,结果还是这样,又快又方便
     24 {
     25     if(((node*)a)->jb!=((node*)b)->jb)
     26         return ((node*)a)->jb-((node*)b)->jb;
     27     if(((node*)a)->hang!=((node*)b)->hang)
     28         return ((node*)a)->hang-((node*)b)->hang;
     29     return ((node*)a)->lie-((node*)b)->lie;
     30 }
     31 int cmpb(const void*a,const void *b)
     32 {
     33     if(((node*)a)->jb!=((node*)b)->jb)
     34         return ((node*)a)->jb-((node*)b)->jb;
     35     if(((node*)a)->hang!=((node*)b)->hang)
     36         return ((node*)b)->hang-((node*)a)->hang;
     37     return ((node*)a)->lie-((node*)b)->lie;
     38 }
     39 int main()
     40 {
     41     int wi=0,bi=0,i;
     42     char ch;
     43     for(i=8; i>0; i--)
     44     {
     45         scanf("+---+---+---+---+---+---+---+---+
    ");
     46         int sum=0;
     47         while(scanf("%c",&ch)&&ch!='
    ')
     48         {
     49             if('A'<=ch&&ch<='Z')
     50             {
     51                 white[wi].hang=i;
     52                 white[wi].lie=sum;//明显第几列和“|”的数目挂钩
     53                 white[wi].data=ch;
     54                 white[wi].jb=sw(ch);
     55                 wi++;
     56             }
     57             else if('a'<=ch&&ch<='z')
     58             {
     59                 black[bi].hang=i;
     60                 black[bi].lie=sum;
     61                 black[bi].data=ch;
     62                 black[bi].jb=sw(ch);
     63                 bi++;
     64             }
     65             else if(ch=='|')//再剩余的字符就不用看了
     66                 sum++;
     67         }
     68     }
     69     scanf("+---+---+---+---+---+---+---+---+");
     70     qsort(white,wi,sizeof(node),cmpw);//排序,这个让我纠结了好久,郁闷
     71     qsort(black,bi,sizeof(node),cmpb);
     72     wi--;
     73     bi--;
     74     printf("White: ");//这种坑爹的输出,好吧……只是很长,不复杂
     75     for(i=0; i<wi; i++)
     76     {
     77         if(white[i].data!='P')
     78             printf("%c",white[i].data);
     79         printf("%c",white[i].lie+'a'-1);
     80         printf("%d,",white[i].hang);
     81 
     82     }
     83     if(white[i].data!='P')
     84         printf("%c",white[i].data);
     85     printf("%c",white[i].lie+'a'-1);
     86     printf("%d
    ",white[i].hang);
     87     printf("Black: ");
     88     for(i=0; i<bi; i++)
     89     {
     90         if(black[i].data!='p')
     91             printf("%c",black[i].data-32);
     92         printf("%c",black[i].lie+'a'-1);
     93         printf("%d,",black[i].hang);
     94     }
     95     if(black[i].data!='p')
     96         printf("%c",black[i].data-32);
     97     printf("%c",black[i].lie+'a'-1);
     98     printf("%d
    ",black[i].hang);
     99     return 0;
    100 }
    View Code
  • 相关阅读:
    【Spring学习随笔】1. Spring的体系结构
    MySQL图形化安装相关总结(含软件分享)
    Spring Bean 基于注解的装配
    vue的三种图片引入方式
    wx.navigateTo({ url: '', }) 跳转无效问题
    JDK环境变量配置
    如何实现Vue底部按钮点击加载更多
    Java Web学习路线
    百度地图循环添加标注,并循环为鼠标悬停标注时信息窗口问题解决
    mvc jquery 修改 viewbag
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3240970.html
Copyright © 2011-2022 走看看