题意 给你棋盘中棋子的位置,让你还原出一个棋盘
同样是模拟 感觉这题要比2996简单…
Emag eht htiw Em Pleh
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3499 | Accepted: 2298 |
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 corresponding input.
Input
according to output of
problem 2996.
Output
according to input of
problem 2996.
Sample Input
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
Sample Output
+---+---+---+---+---+---+---+---+ |.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.| +---+---+---+---+---+---+---+---+做这道题没用到什么算法 虽然代码可读性不强但加了些注释应该好理解,原谅我又把scanf和cout混在一起了
做这道题利用了输入的一个小技巧scanf("%*7s") 可以从控制台读取输入的七个字符并舍弃, 这样做可以只选取我们要想的字符串
还有一个就是strtok()方法头文件是string.h有兴趣可以查一下,类似于python中的split()函数,不过c语言中好像做切割字符串个人用到的不多…
strtok(char *s, const char *delim); s是要分割的字符串 delim是分割字符,函数在s中遇到分割字符会把其置成'\0'
第一次调用时指定s,以后调用时传入NULL即可依次返回下一个分割的字符串指针,如果没有了则返回NULL
源代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char pos[100],*temp,*div=",";
char map[8][8]={{0}};
bool flag;
scanf("%*7s%s",pos); //%*7s为读取前七个字符并舍弃 这样第一个字符就从K开始
temp = strtok(pos,div);
while(temp!=NULL){
char c;
int x,y;
if(temp[0]>='A'&&temp[0]<='Z'){
c = temp[0];
x = temp[1] - 'a';
y = 8 - (temp[2] - '0');
}else{
c = 'P';
x = temp[0] - 'a';
y = 8 - (temp[1] - '0');
}
map[y][x] = c;
temp = strtok(NULL,div);
}
scanf("%*7s%s",pos);
temp = strtok(pos,div);
while(temp!=NULL){
char c;
int x,y;
if(temp[0]>='A'&&temp[0]<='Z'){
c = temp[0]+32;
x = temp[1] - 'a';
y = 8 - (temp[2] - '0');
}else{
c = 'p';
x = temp[0] - 'a';
y = 8 - (temp[1] - '0');
}
map[y][x] = c;
temp = strtok(NULL,div);
}
flag = true;
for(int i=0;i<8;i++){
cout<<"+---+---+---+---+---+---+---+---+"<<endl;
for(int j=0;j<8;j++){
if(flag) cout<<"|.";
else cout<<"|:";
if(map[i][j]!=0){
cout<<map[i][j];
}else{
if(flag)cout<<".";
else cout<<":";
}
if(flag) cout<<".";
else cout<<":";
flag ^= true; //与运算特定相同为0不同为1 可以实现将flag从T->F 或 F->T
}
cout<<"|"<<endl;
flag ^= true; //经过观察上一排的最后一个与本排的第一个颜色相同
//所以要把flag再排尾时转换回去
}
cout<<"+---+---+---+---+---+---+---+---+"<<endl;
return 0;
}