`
Coco_young
  • 浏览: 126506 次
  • 性别: Icon_minigender_1
  • 来自: 湖南长沙
社区版块
存档分类
最新评论

POJ_2996 Help Me with the Game 模拟题

阅读更多
问题来源:http://poj.org/problem?id=2996
Help Me with the Game
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 2084  Accepted: 1352

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


较为恶心的模拟题,注意快排比较函数的写法。

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<map>
using namespace std;
char maze[9][9];
char temp[50];
typedef char str[4];
str white[65];
str black[65];

int priority(char a,char b)
{
    if(a=='K')return -1;
    if(b=='K')return 1;
    if(a=='Q')return -1;
    if(b=='Q')return 1;
    if(a=='R')return -1;
    if(b=='R')return 1;
    if(a=='B')return -1;
    if(b=='B')return 1;
    if(a=='N')return -1;
    if(b=='N')return 1;
    return 0;
}

int cmp_w(const void *a,const void *b)
{
    str *A = (str*)a;
    str *B = (str*)b;
    if(isupper((*A)[0])&&isupper((*B)[0]))
    {
        if((*A)[0]!=(*B)[0])
        {
            return priority((*A)[0],(*B)[0]);
        }
        else if((*A)[2]!=(*B)[2])
        {
            return (*A)[2]-(*B)[2];
        }
        else
        {
            return (*A)[1]-(*B)[1];
        }
    }
    else if(isupper((*A)[0])&&islower((*B)[0]))
    {
        return -1;
    }
    else if(islower((*A)[0])&&isupper((*B)[0]))
    {
        return 1;
    }
    else if(islower((*A)[0])&&islower((*B)[0]))
    {
        if((*A)[1]!=(*B)[1])
        {
            return (*A)[1]-(*B)[1];
        }
        else
        {
            return (*A)[0]-(*B)[0];
        }
    }
    return 0;
}

int cmp_b(const void *a,const void *b)
{
    str *A = (str*)a;
    str *B = (str*)b;
    if(isupper((*A)[0])&&isupper((*B)[0]))
    {
        if((*A)[0]!=(*B)[0])
        {
            return priority((*A)[0],(*B)[0]);
        }
        else if((*A)[2]!=(*B)[2])//r
        {
            return (*B)[2]-(*A)[2];
        }
        else
        {
            return (*A)[1]-(*B)[1];
        }
    }
    else if(isupper((*A)[0])&&islower((*B)[0]))
    {
        return -1;
    }
    else if(islower((*A)[0])&&isupper((*B)[0]))
    {
        return 1;
    }
    else if(islower((*A)[0])&&islower((*B)[0]))
    {
        if((*A)[1]!=(*B)[1])
        {
            return (*B)[1]-(*A)[1];
        }
        else
        {
            return (*A)[0]-(*B)[0];
        }
    }
    return 0;
}

int main()
{
    int cnt = 0;
    int row = 0;
    //freopen("datain.txt","r",stdin);
    //freopen("dataout.txt","w",stdout);
    while(gets(temp)!=NULL)
    {
        if(cnt==16)
        {
            int ct_w = 0;
            int ct_b = 0;
            for(int i=0;i<8;i++)
            {
                for(int j=0;j<8;j++)
                {
                    if(isupper(maze[i][j]))
                    {
                        if(maze[i][j]=='P')
                        {
                            white[ct_w][0] = 'a'+j;
                            white[ct_w][1] = '0'+(8-i);
                            white[ct_w++][2] = '\0';
                        }
                        else
                        {
                            white[ct_w][0] = toupper(maze[i][j]);
                            white[ct_w][1] = 'a'+j;
                            white[ct_w][2] = '0'+(8-i);
                            white[ct_w++][3] = '\0';
                        }
                    }
                    else if(islower(maze[i][j]))
                    {
                        if(maze[i][j]=='p')
                        {
                            black[ct_b][0] = 'a'+j;
                            black[ct_b][1] = '0'+(8-i);
                            black[ct_b++][2] = '\0';
                        }
                        else
                        {
                            black[ct_b][0] = toupper(maze[i][j]);
                            black[ct_b][1] = 'a'+j;
                            black[ct_b][2] = '0'+(8-i);
                            black[ct_b++][3] = '\0';
                        }
                    }
                }
            }
            qsort(white,ct_w,sizeof(str),cmp_w);
            qsort(black,ct_b,sizeof(str),cmp_b);
            printf("White: ");
            for(int i=0;i<ct_w;i++)
            if(i!=0)printf(",%s",white[i]);
            else printf("%s",white[i]);
            printf("\n");
            printf("Black: ");
            for(int i=0;i<ct_b;i++)
            if(i!=0)printf(",%s",black[i]);
            else printf("%s",black[i]);
            printf("\n");
            cnt = 0;
            row = 0;
            continue;
        }
        else if(cnt%2==1)
        {
            int count = 0;
            for(int i=2;i<33;i+=4)
            {
                maze[row][count++] = temp[i];
            }//save the chessboard
            row++;
        }
        cnt++;
    }
    return 0;
}

0
1
分享到:
评论

相关推荐

    POJ2996-Help Me with the Game

    【标题】"POJ2996-Help Me with the Game"是一道源自北京大学在线判题系统POJ的编程竞赛题目。这道题目的主要目标是编写程序来解决一个特定的游戏策略问题,要求参赛者具备扎实的算法基础和编程能力。 【描述】...

    poj_1699.rar_1699_poj_poj1699

    【标题】"poj_1699.rar_1699_poj_poj1699" 提供的是一个关于POJ(编程在线判题系统)第1699题的解决方案,其中包含了该问题的代码实现和解题思路。POJ是一个流行的在线编程竞赛平台,它为参赛者提供了各种算法题目,...

    poj2775.rar_poj_poj 27_poj27_poj2775

    标签"poj poj_27 poj27 poj2775"进一步确认了这是一道关于POJ平台的编程挑战,其中"poj_27"可能是表示第27类问题或者某种分类,而"poj27"可能是对"poj2775"的简写。 压缩文件中的"www.pudn.com.txt"可能是一个链接...

    poj_2682(3).rar_C++ 数组扩充_poj 26_poj 2682_poj26

    标题中的"poj_2682(3).rar"是一个指向编程竞赛问题的引用,通常这类问题在网站如POJ(Programming Online Judge)上出现,供程序员解决并提交代码进行测试。这个问题的编号是2682,可能涉及到特定的数据结构或算法...

    poj_3613解题报告

    2遍dp poj_3613解题报告 poj_3613解题报告

    POJ_3131.zip_POJ 八数码_poj

    标题中的“POJ_3131.zip_POJ 八数码_poj”指的是一个与编程竞赛网站POJ(Problem Set Algorithm)相关的项目,具体是针对3131号问题的解决方案,该问题涉及到了八数码游戏。八数码游戏,又称滑动拼图,是一个经典的...

    Poj_1102_.rar_poj11

    标题"Poj_1102_.rar_poj11"暗示了这是一个关于解决Poj_1102问题的资源包,通常在编程竞赛或在线判题系统如POJ(Problem Set of Judge Online)上遇到。POJ是中国的一个在线编程训练平台,提供了各种算法题目供用户...

    poj_3310.rar_3310_poj

    【标题】"poj_3310.rar_3310_poj"是一个与编程竞赛相关的压缩包,其中包含了对POJ(Problem Online Judge)3310问题的解决方案和详细解析。POJ是一个著名的在线编程竞赛平台,提供各种算法题目供参赛者练习和提交...

    ACM.zip_ACM_poj_poj3187_poj3669

    标题 "ACM.zip_ACM_poj_poj3187_poj3669" 提供的信息表明,这个压缩包包含的是与ACM(国际大学生程序设计竞赛)相关的编程题目解决方案,具体是POJ(Programming Online Judge)平台上的两道题目,编号分别为poj3187...

    1010_stamps.zip_1010_POJ 1010_poj_poj stamps_poj10

    《POJ 1010 Stamps:解题思路与陷阱分析》 POJ 1010,也被称为“邮票问题”,是编程竞赛中的一道经典题目,旨在考察参赛者的动态规划和数学思维能力。这个题目在编程爱好者中具有较高的知名度,因为它涉及到有趣的...

    pku_poj_2187.rar_poj 2187_凸包问题

    标题中的“pku_poj_2187.rar_poj 2187_凸包问题”表明这是一个关于北京大学(Peking University, PKU)编程竞赛平台POJ上的第2187题,题目主要涉及凸包问题。描述中的“O(nlogn)凸包问题 poj2187”提示我们解决这个...

    poj_1002_487.rar_poj 1002

    【标题】"poj_1002_487.rar_poj 1002"指的是北京大学在线编程平台上的第1002道题目,这道题目涉及到计算机科学中的算法设计与实现,特别是字符串处理和哈希映射。在这个问题中,我们需要编写一个程序,该程序能够...

    POJ3277.rar_3277 poj_poj3277_多个面积_线段树

    标题中的“POJ3277.rar_3277 poj_poj3277_多个面积_线段树”是指一个与编程竞赛相关的压缩文件,特别提到了POJ(Problemset Online Judge)的第3277号问题。POJ是一个著名的在线编程竞赛平台,参与者需要解决各种...

    C_(POJ_1854)(分治).cpp

    C_(POJ_1854)(分治).cpp

    poj1990.rar_POJ 19_poj_poj19

    《POJ 1990:树状数组解题策略详解》 在编程竞赛的世界里,POJ(Programming Online Judge)是一个备受瞩目的在线评测系统,它提供了丰富的编程题目供参赛者挑战。其中,编号为1990的题目是一道涉及数据结构与算法...

    POJ2996代码poj2996

    poj2996代码,欢迎下载 下载,下载

    jihe.rar_2289_POJ 3714_poj3714_poj3714 Ra_visual c

    描述中提到的"三道几何题:hdu 1007、hdu 2289、poj 3714"暗示了这是一个关于几何算法的题目集,涉及到三个不同的在线判题系统:HDU(杭州电子科技大学在线评测系统)的1007题和2289题,以及POJ的3714题。...

    D_(POJ_1723)(思维)(sort).cpp

    D_(POJ_1723)(思维)(sort).cpp

    POJ_keptsl6_C++_

    标题“POJ_keptsl6_C++_”表明这是一个关于编程竞赛(POT, Problem Set of Judges)的资源,特别关注C++语言的解决方案。这些竞赛通常涉及到算法设计和实现,以解决各种计算机科学问题。"keptsl6"可能是用户特定的标记...

    D_(POJ_1723)(思维)(第k大数).cpp

    D_(POJ_1723)(思维)(第k大数).cpp

Global site tag (gtag.js) - Google Analytics