Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483279
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 18:23:33


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. typedef struct
  4. {
  5.    char typestyle;
  6.    char column;
  7.    int row;
  8. }type;

  9. int exchg = 0;
  10. char? chess[17][35]=
  11. {
  12.        "+---+---+---+---+---+---+---+---+",
  13.        "|...|:::|...|:::|...|:::|...|:::|",
  14.        "+---+---+---+---+---+---+---+---+",
  15.        "|:::|...|:::|...|:::|...|:::|...|",
  16.        "+---+---+---+---+---+---+---+---+",
  17.        "|...|:::|...|:::|...|:::|...|:::|",
  18.        "+---+---+---+---+---+---+---+---+",
  19.        "|:::|...|:::|...|:::|...|:::|...|",
  20.        "+---+---+---+---+---+---+---+---+",
  21.        "|...|:::|...|:::|...|:::|...|:::|",
  22.        "+---+---+---+---+---+---+---+---+",
  23.        "|:::|...|:::|...|:::|...|:::|...|",
  24.        "+---+---+---+---+---+---+---+---+",
  25.        "|...|:::|...|:::|...|:::|...|:::|",
  26.        "+---+---+---+---+---+---+---+---+",
  27.        "|:::|...|:::|...|:::|...|:::|...|",
  28.        "+---+---+---+---+---+---+---+---+",
  29. },str[2][300];

  30. void? PrintChess()
  31. {
  32.    int? i = 0;
  33.    while(i < 17)
  34.    {
  35.        printf("%sn", chess[i++]);
  36.    }
  37. }

  38. void? Fill(char search[][7], type *t,int j,char? *p)
  39. {
  40.    int index = 5;
  41.    *p = 'X'; //立即逗号换成‘X’,以区别‘p’或‘P’与其它字母
  42.    p -= 3; //指针指到逗号前的第三个字符
  43.    
  44.    if(*p != 'X') //KQRBN
  45.    {

  46.        index = (char *)memchr(search[exchg], *p, 5) - search[exchg];
  47.        t[index].typestyle = search[j][index];
  48.    }
  49.    else //P
  50.    {
  51.        t[index].typestyle = search[j][5];
  52.    }
  53.    
  54.    t[index].column = (*(p + 1) - 'a') * 4 + 2; //棋盘行列与chess数组行列之间的转换
  55.    t[index].row = 17 - (*(p + 2) - '0') * 2;
  56.    chess[t[index].row][t[index].column] = t[index].typestyle;
  57. }

  58. int main()
  59. {
  60.    int? j = 0, len, index;
  61.    char search[2][7]={"KQRBNP","kqrbnp"}, s[7], *p, *q, *z, *end;
  62.    type t[6];
  63.    
  64.    scanf("%s%s%s%s", s, &str[0][1], &str[1][1], &str[1][1]);
  65.    if(strcmp("White:", s)) //若题目先输入Black,则要交换
  66.    {
  67.        strcpy(search[0], "kqrbnp");
  68.        strcpy(search[1], "KQRBNP");
  69.        exchg = 1;
  70.    }
  71.    str[0][0] = str[1][0] = 'X';
  72.    j = 0;
  73.    while(1)
  74.    {
  75.        len = strlen(str[j]);
  76.        end = str[j] + len; //指向Whilte或者Black串的串末,第一次就是WA在这
  77.        if(7 == strlen(str[j]) || 1 == strlen(str[j])) //任何一个为空,即只有White或Black,后面没有棋子说明
  78.        {
  79.            j++;
  80.            if(2 == j)
  81.            {
  82.                break;
  83.            }
  84.            continue;
  85.        }
  86.        
  87.        index = 5;
  88.        if(p = q = (char *)memchr(str[j], ',', len))
  89.        {
  90.            Fill(search, t, j, p);
  91.            //只需要在剩下的end-q个字符中找,要不然会从str[0]找到str[1]的逗号中导致第二次j循环不会经过上面的if判断
  92.            while(p = (char *)memchr(q, ',', end - q))
  93.            {
  94.                q = p;
  95.                Fill(search, t, j, p);
  96.            }
  97.            z = q + 1; //剩下的一个为非P棋子,指向column字符
  98.        }
  99.        else
  100.        {
  101.            z = &str[j][1];//剩下的一个为P棋子,指向column字符
  102.        }
  103.        
  104.        if(memchr(search[exchg], *z, strlen(search)))
  105.        {
  106.            index = (char? *)memchr(search[exchg], str[j][1], 5) - search[exchg];
  107.            t[index].typestyle = search[j][index];
  108.            t[index].column = (*(z+1) - 'a') * 4 + 2;
  109.            t[index].row = 17 - (*(z+2) - '0') * 2;
  110.            chess[t[index].row][t[index].column] = t[index].typestyle;
  111.        }
  112.        else
  113.        {
  114.            t[index].typestyle = search[j][5];
  115.            t[index].column = (*z - 'a') * 4 + 2;
  116.            t[index].row = 17 - (*(z+1) - '0') * 2;
  117.            chess[t[index].row][t[index].column] = t[index].typestyle;
  118.        }
  119.        j++;
  120.        if(2 == j)
  121.        {
  122.            break;
  123.        }
  124.    }
  125.    
  126.    PrintChess();
  127.    return 0;
  128. }

Problem: 2993 User: angrad
Memory: 148K Time: 0MS
Language: C Result: Accepted

tip:

价值千金的批处理:


点击(此处)折叠或打开

  1. @echo off
  2. :again
  3. r > input
  4. a < input > output.a
  5. b < input > output.b
  6. fc output.a output.b > nul
  7. if? not errorlevel 1 goto? again

2011-04-09 15:26 发表于百度空间,今搬至CU。

阅读(1844) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~