POJ2993——Help Me with the Game

Help Me with the
Game

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 player are shown in upper-case letters, while the black
player‘s pieces are lower-case letters. The letters are one of "K" (King), "Q"
(Queen), "R" (Rook), "B" (Bishop), "N" (Knight), or "P" (Pawn). The chessboard
outline is made of plus ("+"), minus ("-"), and pipe ("|") characters. The black
fields are filled with colons (":"), white fields with dots
(".").
Output
The output consists of two lines. The first line consists of
the string "White: ", followed by the description of positions of the pieces of
the white player. The second line consists of the string "Black: ", followed by
the description of positions of the pieces of the black player.
The
description of the position of the pieces is a comma-separated list of terms
describing the pieces of the appropriate player. The description of a piece
consists of a single upper-case letter that denotes the type of the piece
(except for pawns, for that this identifier is omitted). This letter is
immediatelly followed by the position of the piece in the standard chess
notation -- a lower-case letter between "a" and "h" that determines the column
("a" is the leftmost column in the input) and a single digit between 1 and 8
that determines the row (8 is the first row in the input).
The pieces in the
description must appear in the following order: King("K"), Queens ("Q"), Rooks
("R"), Bishops ("B"), Knights ("N"), and pawns. Note that the numbers of pieces
may differ from the initial position because of capturing the pieces and the
promotions of pawns. In case two pieces of the same type appear in the input,
the piece with the smaller row number must be described before the other one if
the pieces are white, and the one with the larger row number must be described
first if the pieces are black. If two pieces of the same type appear in the same
row, the one with the smaller column letter must appear first.
Sample
Input

+---+---+---+---+---+---+---+---+
|.r.|:::|.b.|:q:|.k.|:::|.n.|:r:|
+---+---+---+---+---+---+---+---+
|:p:|.p.|:p:|.p.|:p:|.p.|:::|.p.|
+---+---+---+---+---+---+---+---+
|...|:::|.n.|:::|...|:::|...|:p:|
+---+---+---+---+---+---+---+---+
|:::|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|...|:::|...|:::|.P.|:::|...|:::|
+---+---+---+---+---+---+---+---+
|:P:|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|.P.|:::|.P.|:P:|...|:P:|.P.|:P:|
+---+---+---+---+---+---+---+---+
|:R:|.N.|:B:|.Q.|:K:|.B.|:::|.R.|
+---+---+---+---+---+---+---+---+

Sample Output
White:
Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,a2,c2,d2,f2,g2,h2,a3,e4
Black:
Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6

题目大意:

    给定一个棋盘,大写字母代表白子,小写字母代表黑子,输出白子和黑子在场上的分布情况(排序后)。

    (+,-,|,.,:)构成其他部分,请无视就好。

    PS:棋盘的左下角坐标为(a,1)。 这里假设为(a,1)而不是(1,a)与程序对应。

    输出Nxy,N属于(KQRBNP) 若N==P 不输出N。

结题思路:

    1.定义了结构体数组W[],B[]。 

1 struct Point{
2 char x,y,date;
3 }W[100],B[100];

    2.读取字符串,存入两个数组中

    3.根据题意对两数组进行排序(我先无视了KQRBNP这个排序,直接对(x,y)排序,再在输出字符串时,根据(KQRBNP)遍历数组来构造输出String.)

      白子排序规则:先按行号(1..8)从小到大排,在根据列号(a..h)从小到大排。

      黑子排序规则:先按行号(1..8)从大到小排,在根据列号(a..h)从小到大排。

    4.输出规定格式字符串(用的String类,感觉比C字符串方便很多)

Code:


 1 #include<string>
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5 struct Point
6 {
7 char x,y,date;
8 } W[100],B[100];
9 bool cmp1(struct Point a,struct Point b)//黑子排序cmp
10 {
11 if (a.y!=b.y) return a.y>b.y;
12 return a.x<b.x;
13 }
14 bool cmp2(struct Point a,struct Point b)//白子排序cmp
15 {
16 if (a.y!=b.y) return a.y<b.y;
17 return a.x<b.x;
18 }
19 int main()
20 {
21 string tmp,str;
22 cin>>tmp;
23 int i,j,k1=1,k2=1;
24 for (i=8; i>=1; i--)
25 {
26 cin>>str>>tmp;
27 char t=‘a‘;
28 for (j=2; j<=str.length()-1; j+=4,t++)
29 {
30 if (str[j]>=‘A‘&&str[j]<=‘Z‘)
31 {
32 W[k1].x=t;
33 W[k1].y=i+‘0‘;
34 W[k1++].date=str[j];
35 }
36 if (str[j]>=‘a‘&&str[j]<=‘z‘)
37 {
38 B[k2].x=t;
39 B[k2].y=i+‘0‘;
40 B[k2++].date=str[j]-32;
41 }
42 }
43 }
44 string White="White: ",Black="Black: ";
45 sort(B+1,B+k2,cmp1);
46 sort(W+1,W+k1,cmp2);
47 tmp="KQRBNP";
48 for (i=0; i<=tmp.length()-1; i++)
49 {
50 for (j=1; j<=k1-1; j++)
51 {
52 if (W[j].date==tmp[i])//String类可以直接写加号来在末尾添加元素
53 {
54 if (W[j].date!=‘P‘) White+=W[j].date;
55 White+=W[j].x;
56 White+=W[j].y;
57 White+=‘,‘;
58 }
59 }
60 }
61 White.erase(White.length()-1,1);//去除多余的逗号
62 for (i=0; i<=tmp.length()-1; i++)
63 {
64 for (j=1; j<=k1-1; j++)
65 {
66 if (B[j].date==tmp[i])
67 {
68 if (B[j].date!=‘P‘) Black+=B[j].date;
69 Black+=B[j].x;
70 Black+=B[j].y;
71 Black+=‘,‘;
72 }
73 }
74 }
75 Black.erase(Black.length()-1,1);//去除多余的逗号
76 cout<<White<<endl<<Black;
77 return 0;
78 }

POJ2993——Help Me with the Game,布布扣,bubuko.com

时间: 2024-10-26 04:05:06

POJ2993——Help Me with the Game的相关文章

POJ2993——Emag eht htiw Em Pleh

Emag eht htiw Em Pleh DescriptionThis 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 corresponding input.Inputaccording to output of problem 2996.Outputaccording to input of probl

POJ-2993 Emag eht htiw Em Pleh---棋盘模拟

题目链接: https://vjudge.net/problem/POJ-2993 题目大意: 输入和输出和这里相反. 思路: 模拟题,没啥算法,直接模拟,不过为了代码精简,还是花了一点心思的 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<queue> 7 #i

poj2993模拟

Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2994   Accepted: 1979 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

poj2993 翻转2996

Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2944   Accepted: 1949 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

poj2993(Emag eht htiw Em Pleh)

题目大意: 给你白棋子的位置: White: Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,a2,c2,d2,f2,g2,h2,a3,e4和黑棋子的位置:Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6模拟出棋盘的情况a-h代表横坐标,1-17代表纵坐标.后面的位置首歌字符没有大写字母的是P,其中白棋子p是大写,黑棋子p是小写.解题思路:一句话太恶心,模拟一小时,我都晕了.代码: 1 #include <algorit

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅

POJ题目分类

初期: 一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,he