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!\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 (模拟)

时间: 2024-08-06 23:17:19

POJ 2996 Help Me with the Game (模拟)的相关文章

快速切题 poj 2996 Help Me with the Game 模拟暴力

Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3510   Accepted: 2251 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

POJ 2996 &amp; 2993 国际象棋布局 模拟

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 pl

POJ 2996 Help Me with the Game

Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3292   Accepted: 2122 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

POJ 3282 Ferry Loading IV(简单模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross

POJ 2993 Emag eht htiw Em Pleh 模拟

http://poj.org/problem?id=2993 模拟大法好. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char q[10],w[100]; 6 char e[10],r[100]; 7 char white[12]= {'K','Q','R','B','N','P'}; 8 char black[12]= {'k','q','r

POJ 3344 &amp; HDU 2414 Chessboard Dance(模拟)

题目链接: PKU:http://poj.org/problem?id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Description Another boring Friday afternoon, Betty the Beetle thinks how to amuse herself. She goes out of her hiding place to take a walk around the living r

【BZOJ3502/2288】PA2012 Tanie linie/【POJ Challenge】生日礼物 堆+链表(模拟费用流)

[BZOJ3502]PA2012 Tanie linie Description n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. Sample Input 5 2 7 -3 4 -9 5 Sample Output 13 题解:跟1150和2151差不多. 我们先做一些预处理,因为连续的正数和连续的负数一定是要么都选要么都不选,所以可以将它们合并成一个数,同时区间中的零以及左右两端的负数没有意义,可以将它们删掉.然后我们得到的序列就变成:正-

【POJ 3279 Fliptile】开关问题,模拟

题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意只扩散一圈,不是连锁反应. 求最少翻转几个位置能够使所有n*m个位置都变为白色.若有解,求字典序最小的翻转方案(给出每个位置的翻转次数). 数据范围:n, m 属于 [1, 15] 思路:我们把翻转方案中的翻转称为“主动翻转”,翻转过程中受邻居影响而发生的翻转称为“被动翻转”.观察例子可得出如下几个

快速切题 poj 2993 Emag eht htiw Em Pleh 模拟

Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2806   Accepted: 1865 Description This problem is a reverse case of the problem 2996. You are given the output of the problem H and your task is to find the correspond