码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2996 Help Me with the Game (模拟)

时间:2014-06-15 20:16:37      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   http   tar   ext   

题目链接:http://poj.org/problem?id=2996

POJ训练计划中的模拟都是很棒的模拟,也很有代表性。

这个题讲的是给你一个国际象棋棋盘,写程序打印出黑白双方的棋子,以及棋子的坐标。

但是需要注意的国际棋盘的坐标问题

如下图这个国际棋盘

bubuko.com,布布扣

可以看到数字轴和字母轴的方向以及增减关系。

所以在这个题的统计的时候需要进行坐标转换,因为已经做过类似的方法了,这个也不是问题。

 

总之就是个模拟,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct node
{
    char x,y;
}zb[10000];

char MAP[20][40];

int cmp (const void * a,const void * b)
{
    struct node *ta = (struct node *)a;
    struct node *tb = (struct node *)b;

    if (ta->y == tb->y)
        return ta->x - tb->x;

    return ta->y - tb->y;
}

int cmp1 (const void * a,const void * b)
{
    struct node *ta = (struct node *)a;
    struct node *tb = (struct node *)b;

    if (ta->y == tb->y)
        return ta->x - tb->x;

    return tb->y - ta->y;
}

int fin(char c)
{
    int i,k;
    int s = 0;
    int x = 0,y = 0;;
    for (i = 1,y = 0;i < 17;y++,i += 2)
    {
        for (k = 2,x = 0;k < 33;x++,k += 4)
            if (MAP[i][k] == c)
            {
                zb[s].x = x;
                zb[s++].y = 8 - y;
                //printf ("!%d - %d!\n",x,8 - y);
            }
    }

    return s;
}

int main()
{
    int i,k;
    int n;
    for (i = 0;i < 17;i++)
        scanf ("%s",MAP[i]);

    printf ("White: ");

    n = fin('K');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
        printf ("K%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('Q');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
        printf ("Q%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('R');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
        printf ("R%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('B');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
        printf ("B%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('N');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
        printf ("N%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('P');
    qsort (zb,n,sizeof (zb[0]),cmp);
    for (i = 0;i < n;i++)
    {
        printf ("%c%d",zb[i].x + 'a',zb[i].y);

        if (i < n - 1)
            printf(",");
        else
            printf ("\n");
    }

    printf ("Black: ");

    n = fin('k');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
        printf ("K%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('q');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
        printf ("Q%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('r');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
        printf ("R%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('b');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
        printf ("B%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('n');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
        printf ("N%c%d,",zb[i].x + 'a',zb[i].y);

    n = fin('p');
    qsort (zb,n,sizeof (zb[0]),cmp1);
    for (i = 0;i < n;i++)
    {
        printf ("%c%d",zb[i].x + 'a',zb[i].y);

        if (i < n - 1)
            printf(",");
        else
            printf ("\n");
    }

    return 0;
}


 

我的博客:http://blog.csdn.net/codehypo

POJ 2996 Help Me with the Game (模拟),布布扣,bubuko.com

POJ 2996 Help Me with the Game (模拟)

标签:class   blog   code   http   tar   ext   

原文地址:http://blog.csdn.net/codehypo/article/details/30281829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!