• POJ 2996 Help Me with the Game (模拟)


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

    id=2996

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

    这个题讲的是给你一个国际象棋棋盘,敲代码打印出黑白两方的棋子。以及棋子的坐标。

    可是须要注意的国际棋盘的坐标问题

    例如以下图这个国际棋盘

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

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

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

    #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!
    ",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 ("
    ");
        }
    
        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 ("
    ");
        }
    
        return 0;
    }
    


     

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

  • 相关阅读:
    CompletableFuture组合式异步编程
    java日志:slf4j
    Protobuffer生成java类
    变量快速变camel写法
    上海有线通下载exe会302转发请求
    ClickOnce添加自定义prerequisite
    International Conference for Smart Health 2015 Call for Papers
    IEEE/ACM ASONAM 2014 Industry Track Call for Papers
    Call for Papers IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM)
    Call for Papers International Conference for Smart Health (ICSH) 2014
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7346805.html
Copyright © 2020-2023  润新知