首页 > 代码库 > POJ 2993 Emag eht htiw Em Pleh (模拟)(strtok 应用)

POJ 2993 Emag eht htiw Em Pleh (模拟)(strtok 应用)

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

这个题和POJ 2996属于姊妹题,POJ 2996解题报告见:http://blog.csdn.net/codehypo/article/details/30281829

就是给你黑白双方的每个棋子的信息(是哪种,以及坐标),然后填充这个棋盘,并打印。

 

其实这个题目比2996还要简单,不过这个题我尝试用了一下前几天某姑娘让我帮她敲代码用到的函数:strtok();

先说一下这个函数

char *strtok(char s[], const char *delim);


这是一个字符串分解函数,第一个参数是录入一个字符串,第二个参数是录入一个分隔符

这个函数会在这个字符串中以delim为分割的划分为若干小的字符串,并把其指针传递出来。

 

对于这个题,每个棋子都是以‘,’分割的,使用这个函数,还是很方便的。

 

做模拟题吗,我习惯于封装函数,加上这个函数的使用,让代码更有条理,后期debug起来,也很顺手。

 

模拟不必多说,代码如下:

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

char MAP[20][40] = {{"+---+---+---+---+---+---+---+---+"},
                    {"|...|:::|...|:::|...|:::|...|:::|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|:::|...|:::|...|:::|...|:::|...|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|...|:::|...|:::|...|:::|...|:::|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|:::|...|:::|...|:::|...|:::|...|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|...|:::|...|:::|...|:::|...|:::|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|:::|...|:::|...|:::|...|:::|...|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|...|:::|...|:::|...|:::|...|:::|"},
                    {"+---+---+---+---+---+---+---+---+"},
                    {"|:::|...|:::|...|:::|...|:::|...|"},
                    {"+---+---+---+---+---+---+---+---+"}};

char s[10000000];

int bj(char s[],int n)
{
    int x,y;
    char c;

    if (strlen(s) == 3)
    {
        x = 8 - (s[2] - '0');
        y = s[1] - 'a';
        c = s[0];

        if (n == 2)
            c += 32;
    }else
    {
        x = 8 - (s[1] - '0');
        y = s[0] - 'a';
        if (n == 1)
            c = 'P';
        else
            c = 'p';
    }
    int tx,ty;
    int i,k;

    for (i = 1,tx = 0;i < 17;tx++,i += 2)
    {
        for (k = 2,ty = 0;k < 33;k += 4,ty++)
        {
            if (tx == x && ty == y)
                MAP[i][k] = c;
        }
    }

     return 0;
}

int main()
{

    char *p;
    int i;
    gets (s);
    p = s;
    p += 7;

    p = strtok (s," ");

    while (p != NULL)
    {
        bj (p,1);
        p = strtok(NULL,",");
    }

    gets (s);
    p = s;
    p += 7;

    p = strtok (s," ");

    while (p != NULL)
    {
        bj (p,2);
        p = strtok(NULL,",");
    }

    for (i = 0;i < 17;i++)
        puts (MAP[i]);

    return 0;
}