poj 2996 Help Me with the Game 模拟

题目链接:

  http://poj.org/problem?id=2996

题目描述:

  输入一个棋盘, white player的棋子用大写字母表示,black player's的棋子用小写字母表示,两方都有‘k’,‘q’,‘r’,‘b’,‘n’,‘p’这些棋子,棋盘由‘+’,‘-’,‘|’构成,黑色区域的空白用‘:’填充,白色区域的空白用'.'填充,统计双方的棋子,棋子按照‘k’,‘q’,‘r’,‘b’,‘n’,‘p’的顺序输出。相同棋子内部的排序,如果在同一行按照列的升序排,否则白棋按照行升序排,黑按照行降序排。

详细的输出格式:(我发誓:这个绝对是我目前见到的最别扭的模拟~~)。

解题思路:

  我用的是打标和模拟,有一点需要注意的是,给的棋牌最下面的一行才是第一行,所以要倒着输入才对。

代码:

  

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <iostream>
 6 using namespace std;
 7 #define maxn 70
 8 
 9 char dir[2][6]= {'K','Q','R','B','N','P', 'k','q','r','b','n','p'};//打标,在统计棋子个数和输出的时候用
10 struct node//由于储存棋子位置
11 {
12     int x, y;
13 };
14 int cmp (const void *a, const void *b)//由于遍历方式,不满足黑棋的排序方式,手动写快排,对黑棋进行排序;
15 {
16     node *c = (node *)a;
17     node *d = (node *)b;
18     if (c->x == d->x)
19         return c->y - d->y;
20     return d->x - c->x;
21 }//
22 int main ()
23 {
24     int i, j, k, l, a[2][10], n;//a[颜色][种类]数组用于储存对应类型棋子的个数
25     char str[maxn], map[10][10];
26     node dp[2][6][maxn];//dp[颜色][种类][个数]
27     memset (map, 0, sizeof(map));
28     memset (a[2], 0, sizeof(a[2]));
29     scanf ("%s", str);
30     for (i=8; i>0; i--)//翻转棋盘,并且把棋子抄到map中
31     {
32         scanf ("%s", str);
33         for (j=1, k=2; j<=8; k+=4, j++)
34             map[i][j] = str[k];
35         scanf ("%s", str);
36     }
37     memset (a, 0, sizeof(a));//统计不同棋子的数目
38     for (i=1; i<9; i++)
39         for (j=1; j<9; j++)
40             for (k=0; k<2; k++)
41                 for (l=0; l<6; l++)
42                     if (map[i][j] == dir[k][l])
43                     {
44                         dp[k][l][a[k][l]].x = i;
45                         dp[k][l][a[k][l] ++].y = j;
46                     }
47 
48 
49     for (k=0; k<2; k++)//输出对棋子的统计结果
50     {
51         int flag = 0;
52         if (k == 0)
53             printf ("White: ");
54         else
55             printf ("Black: ");
56         for (i=0; i<6; i++)
57         {
58             if (k == 1)
59                 qsort (dp[k][i], a[k][i], sizeof(dp[k][i][0]), cmp);
60             for (j=0; j<a[k][i]; j++)
61             {
62                 if (flag != 0)
63                     printf (",");
64                 if (i != 5)
65                     printf ("%c", dir[0][i]);
66                 printf ("%c%d", dp[k][i][j].y+'a'-1, dp[k][i][j].x);
67             flag = 1;
68             }
69         }
70         printf ("
");
71     }
72     return 0;
73 }

 在追加一道~~~~~poj 2993 Emag eht htiw Em Pleh

题目链接:

  http://poj.org/problem?id=2993

题目描述:

  这道题目就是上个题目的输入变输出,输出变输入。

代码:

  

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <iostream>
 6 using namespace std;
 7 #define maxn 70
 8 
 9 char map[maxn][maxn];
10 char str[maxn] = {"+---+---+---+---+---+---+---+---+"};
11 char str1[2][maxn] = {"|...|:::|...|:::|...|:::|...|:::|","|:::|...|:::|...|:::|...|:::|...|"};
12 
13 int main ()
14 {
15     int i, j, k;
16     char a[maxn*5];
17     memset (map, 0, sizeof(map));
18     for (i=0; i<17; i++)//先把棋盘存在map数组里面
19     {
20         if (i%2)
21             strcpy (map[i],str1[(i+1)/2%2]);
22         else
23             strcpy (map[i], str);
24     }
25     for (i=0; i<2; i++)//再根据输入棋子的位置摆放棋子,
26     {
27         gets (a);
28         for (j=7; a[j]; j+=3)//跳过本棋子的描述,到下一个棋子
29         {
30             int x, y;
31             if (a[j+3] == ',')//判断是不是‘P’棋子
32             {
33                 y = (a[j+1] - 'a') * 4 + 2;//不是p棋子,计算棋子的位置(x,y);
34                 x = (a[j+2] - '0') * 2 - 1;
35                 map[x][y] = a[j];
36                 j ++;//跳过逗号
37             }
38             else
39             {
40                 y = (a[j] - 'a') * 4 + 2;//是p棋子,计算棋子的位置(x,y);
41                 x = (a[j+1] - '0') * 2 - 1;
42                 map[x][y] = 'P';
43             }
44             if (i == 1)
45                 map[x][y] += 'a' - 'A';//黑棋子要大写变小写
46         }
47     }
48     for (i=16; i>=0; i--)//倒着输出棋盘,因为题目上给出left——down是(1,1);
49         puts(map[i]);
50     return 0;
51 }
本文为博主原创文章,未经博主允许不得转载。
原文地址:https://www.cnblogs.com/alihenaixiao/p/4176805.html