程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2996 Help Me with the Game (模擬)

POJ 2996 Help Me with the Game (模擬)

編輯:C++入門知識

題目鏈接:http://poj.org/problem?id=2996

POJ訓練計劃中的模擬都是很棒的模擬,也很有代表性。

這個題講的是給你一個國際象棋棋盤,寫程序打印出黑白雙方的棋子,以及棋子的坐標。

但是需要注意的國際棋盤的坐標問題

如下圖這個國際棋盤

\

可以看到數字軸和字母軸的方向以及增減關系。

所以在這個題的統計的時候需要進行坐標轉換,因為已經做過類似的方法了,這個也不是問題。

總之就是個模擬,代碼如下:

#include 
#include 
#include 
#include 

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved